Datediff function in hive - hive

Regarding DateDiff function
select datediff(current_date, '-2018-01-21');
what is - here as I know datediff(enddata,startdate)
if we mention minus for startdate it getting number values 1474138
can please help to understand

Below query confirms that a negative date is similar to a negative integer. If you subtract a negative number to a positive number, it is the same as adding their absolute values (ignoring the signs). For example; 8 - (-4) = 8 + 4
Thus, since the minimum date value for date type is '0000-01-01', we measure the number of days from year -2018 to 0000 and add to the number of days from 0000 to 2018. Then, we get 1474137 ( = 737122 + 737015). Hope this helps. Thanks.
Query:
select datediff('2018-03-02', '0000-01-01'), datediff('0000-01-01', '-2018-03-01'), datediff('2018-03-02', '-2018-03-01')
Result:
737122 737015 1474137
Again, 737122 + 737015 = 1474137. There are 1474137 days since 2018-Mar-01 BC.

Related

Converting "3H 30m" into 3.5

I am creating a SQL report for someone in which exists a column called Labour Hours of Engineer. In the column, the answers are shown as "3H 30M".
I want to change it to decimal. For example, "3H 30M" becomes 3.5.
Or another example is if an engineer works for 23 minutes, in the column, the answer should be 0.38 (rounded to 2 decimal places). 0.38 is the answer when you divide 23 mins by 60.
My current Formula is:
CONVERT(VARCHAR(40), CAST(Labour AS INT)%(24*60)/60) + '.' + CONVERT(VARCHAR(40), CAST(Labour AS INT)%60)
Any way to achieve this, please share.
Assuming that the 'H' and 'M' are always there, one method would be some use of CHARINDEX to find the 'H' and return the "first" characters and also strip those first characters. For minutes, you can just use simple division to get the decimal value
DECLARE #Time varchar(7) = '3H 30M';
SELECT #Time,
CONVERT(int,LEFT(#Time,CHARINDEX('H',#Time)-1)) + (CONVERT(decimal(2,0),REPLACE(STUFF(#Time,1,CHARINDEX('H',#Time),''),'M',''))/60);
This code will cover Hour minute or only minute
select Convert(decimal(26,2),
IIF(Labour like '%H%',(Convert(int, substring(Labour,0,(CHARINDEX('H',Labour)))) * 60 )+IIF(Labour like '%M%',Convert(int, substring(Labour,CHARINDEX('H',Labour)+1,CHARINDEX('M',Labour)-CHARINDEX('H',Labour)-1)),0)
,IIF(Labour like '%M%',Convert(int, substring(Labour,0,CHARINDEX('M',Labour))),0)) /60.00)

Extracting date in SQL

I have a column due date in the format 20210701 (YYYYMMDD), using SQL I want to extract all the dates apart from 5th of particular month ( As highlighted in the pic below )
I used the below code:
SELECT Due_Date_Key
FROM Table
WHERE Due_Date_Key <>20210705
However the error in the above code is it will exclude only the month of jul but not for other months.
How can extract the dates apart from 5th from the entire column.
Help would be much appreciated.
Note that column DUE_DATE_KEY is numeric.
A more SQLish way would be to convert string to date and then check if day is not 5
SELECT * FROM Table
WHERE DATE_PART('day', to_date(cast(DUE_DATE_KEY as varchar), 'YYYYMMDD')) != 5
Using modulo operator to determine whether the last two digits of DUE_DATE_KEY are 05.
select * from T where DUE_DATE_KEY % 100 <> 5
Using your sample data, the above query returns the following:
due_date_key
20210701
20210708
20210903
Refer to this db fiddle

MS Sql extract the day value in a date

In MS SQL I am trying to get the value of day in the current date, eg: today is 25/08/2016, so this value would be the number 25. I would also like to find the value of day in an earlier date (eg: 06/06/2016 so this number would be 06). I am then looking to subtract the second value from the first and determin if the result is a positive or negative value. If it is positive it should do one thing, eg print test A and if it is a negative value it should do something else, eg print test B.
I am new to MS sql and really have no clude how to implement this in the language. Does anyone have any pointers? Much appreciated. Please see my pseudo code:
change = value of days in current date - value of days in previous date
if change is a positive value : print "testA"
if change is 0 or a negaitve value: print "testB"
I am doing this in excel 2010. I have three columns with dates in colA & colB and testA or test B should be printed in colC depending if the value is positive or negative.
Eg data:
colA: 12/02/2016, 06/06/2016, 12/02/2016
colB: 12/05/2016, 12/02/2015, 28/06/2016
If you can able to change your dates format to MM/DD/YYYY then below script may help you
DECLARE #D1 DATETIME ='08/25/2016'
DECLARE #D2 DATETIME ='06/06/2016'
IF DATEPART(D,#D1) - DATEPART(D,#D2) > 0
PRINT 'testA'
ELSE
PRINT 'testB'
Use the below script :
SELECT CASE WHEN (DATEPART(dw,GETDATE())+DATEPART(dw,GETDATE()-1)) > 0 THEN 'testA'
WHEN (DATEPART(dw,GETDATE())+DATEPART(dw,GETDATE()-1)) =0 THEN 'testB' END Result
some additional info.
IF you need a particular DAY/Day Value use the below scripts
SELECT DATENAME(dw,'12/22/2016') --Thursday
SELECT DATEPART(dw,'12/22/2016') --5
IF you need a DAY of month value use the below script
SELECT DATEPART(dd,'12/22/2016') --22

How to show rows where date is equal to every 7 days from a specific day

I'd like to show every row where date_added equals '2015-02-18' and every seven days after, so '2015-02-25' and '2015-03-04' etc..
here's what I have so far
select * from table
where ((to_char(date_added, 'j')) /
((select to_char(d,'j') from (select date '2015-02-18' d from dual)))) = 1
That gets me the first desired date, however I'm stuck as to how to express it to show the next 7 days as a step additive function.
Any help is appreciated. Thank you.
One way to do this is with mod():
select *
from table
where mod(date_added - date '2015-02-18', 7) = 0;
Note: this assumes that the dates have no time component. If they do, then use trunc() to get rid of it.
I like Gordon's "mod()" solution but he's missing part of the requested solution.
In this case, I have a "calendar" table that includes a series of dates:
http://www.perpendulum.com/2012/06/calendar-table-script-for-oracle/
select *
from calendar
where date_time_start >= to_date('01-Jan-2013')
and mod(trunc(date_time_start) - to_date('01-Jan-2013'), 7) = 0;
Per the original question, you want records where the dates are equal to a given date and every seven days thereafter.

Plus 2 date in SQL

I really need help on this those function DATEDIFF OR DateAdd , because I have no idea on those things.
Declare #EstDate as Date,
#Shipdate as Date,
#Workingday as Int,
I need to do like this:
#EstDate(Date) = #ShipDate(DATE) - #WorkingDay(int) - 2days
Example: #EstDate(date) = '6/11/2011' - 5days - 2days.
How can I do this formula in SQL ? >_< In PHP its easy, but in SQL 2000 up, I have no ideas.
I think this will do it. Basically this adds a negative number of days to #ShipDate. The negative number of days will equal #WorkingDay + 2.
I really don't know what #WorkingDay is supposed to represent. This example assumes it's just a number that represents something to you.
#EstDate = DateAdd(dd,-(#WorkingDay + 2),#ShipDate)
It would look something like this, given your example:
#EstDate(date) = DateAdd(dd, -(5 days + 2 days), '6/11/2011')