how to get to_epoch(sysdate-90) in Hive - hive

I have a query which runs good in Oracle, but i want to use the same query in Hive.
query:
select count(mem_id)
from mem
where cobrand_id = '10001372'
and user_type_id =1
and status_ind <>3
and LAST_ACCESSED >= to_epoch(sysdate-90);
Also, I have LAST_ACCESSED coulmn in double.example value of LAST_ACCEESSED is : 1.554386487E9, not sure what value this is i am guessing this could be seconds.
tried:
UNIX_TIMESTAMP( string date, string pattern )
FROM_UNIXTIME( bigint number_of_seconds [, string format] )
No luck. Can someone help me. Thanks in advance.

It seems 1.554386487E9 is the same unix epoch time stored in double, displayed in engineering notation, and it can be casted to BIGINT.
Checking your example:
select from_unixtime(cast(1.554386487E9 as bigint));
OK
2019-04-04 07:01:27
Does this timestamp look good?
If yes, then use unix_timestamp(concat(date_sub(current_date,90),' 00:00:00')) to get epoch time for current date - 90 days.
Your query fixed:
select count(mem_id)
from mem
where cobrand_id = '10001372'
and user_type_id =1
and status_ind <>3
and cast(LAST_ACCESSED as BIGINT) >= unix_timestamp(concat(date_sub(current_date,90),' 00:00:00'))

Related

SQL convert single int to a time value

I am trying to convert a single integer which represents the hour value into a time. I've tried using cast but this converts the value to a date
cast(datepart(hh,tstart) as datetime) as test
I've also tried casting it as a time and the conversion is not allowed.
The numbers I am working with are 7,8,9,10,11,12,13,...,23, 0
The format I would like is convert 7 to 7:00, 23 to 23:00, etc
Thank you
There are many ways to do that:
WITH table_name AS
(
SELECT * FROM (VALUES
(1),(2),(3),(10),(23)
) T(H)
)
SELECT DATEADD(HOUR, H, 0) DateValue,
CONVERT(time, DATEADD(HOUR, H, 0)) TimeValue,
CONVERT(varchar(2), H)+':00' TextValue
FROM table_name T1
I would recomend storing value as TIME datatype.
You can try this:
select cast(dateadd(hour,3,'00:00:00') as time)
which gives result:
03:00:00.0000000
Use your int values in place of 3 in above statement
Use convert datetime to varchar datatype and use case statement
DECLARE #date datetime ='2016-05-01 10:00:000'
SELECT CASE cast(DATEPART(hh,#date)as varchar(10))
WHEN '10'then '10:00'
WHEN '11'then '11:00'
WHEN '12'then '12:00'
.
.
.
WHEN '23' then '23:00'
END 'Hourpart'
You can concatenate a minute to the time and cast it to time
select cast(concat(20,':00') as time(7)) as 'time'
If you wanted to display like 'HH:MM',use the below query.
SELECT CAST(23 AS VARCHAR(50))+':00'
If you wanted to get the result as time format,use the below query.
SELECT CAST(CAST(23 AS VARCHAR(50))+':00:00' AS TIME)
OR
SELECT CAST(DATEADD(HOUR,23,'00:00:00') as time)
here is the sample output :
Assuming all of your potential values are integers between 1 and 24, I think FORMAT is the simplest way.
FORMAT(tstart*100,'00:00')

Firebird cast integer as Time or Date

I have a table which stores an amount of seconds as integer.
I want to display it and use it as Time or Date.
If I write this:
Select Cast(ColAmountofSeconds as Time) as ThisTime From MyTable;
same with:
Select Cast(ColAmountofSeconds as Date) as ThisTime From MyTable;
I get the following error:
Overflow occurred during data type conversion. conversion error from
string "14".
Note "14" is the value of the first row in the ColAmountofSeconds column.
This is so natural in SQL Server, that I can't believe the amount of time I've spent on figuring this out.
EDIT
I can't believe this is the answer:
Update MyTable
Set TIMESPENT = time '00:00:00' + ColAmountOfSeconds;
Firebird cast function does not support converting a numeric value to date, time or timestamp.
You can take advantage of the fact that Firebird supports arithmethic between dates and numeric values, so you can write your query like this:
select dateadd(second, ColAmountOfSeconds, cast('00:00:00' as time))
from myTable;
--or the equivalent:
select cast(cast('2013-01-01' as timestamp) + cast(ColAmountofSeconds as double precision) / 86400 as TIME)
from myTable;

convert Excel Date Serial Number to Regular Date

I got a column called DateOfBirth in my csv file with Excel Date Serial Number Date
Example:
36464
37104
35412
When i formatted cells in excel these are converted as
36464 => 1/11/1999
37104 => 1/08/2001
35412 => 13/12/1996
I need to do this transformation in SSIS or in SQL. How can this be achieved?
In SQL:
select dateadd(d,36464,'1899-12-30')
-- or thanks to rcdmk
select CAST(36464 - 2 as SmallDateTime)
In SSIS, see here
http://msdn.microsoft.com/en-us/library/ms141719.aspx
The marked answer is not working fine, please change the date to "1899-12-30" instead of "1899-12-31".
select dateadd(d,36464,'1899-12-30')
You can cast it to a SQL SMALLDATETIME:
CAST(36464 - 2 as SMALLDATETIME)
MS SQL Server counts its dates from 01/01/1900 and Excel from 12/30/1899 = 2 days less.
tldr:
select cast(#Input - 2e as datetime)
Explanation:
Excel stores datetimes as a floating point number that represents elapsed time since the beginning of the 20th century, and SQL Server can readily cast between floats and datetimes in the same manner. The difference between Excel and SQL server's conversion of this number to datetimes is 2 days (as of 1900-03-01, that is). Using a literal of 2e for this difference informs SQL Server to implicitly convert other datatypes to floats for very input-friendly and simple queries:
select
cast('43861.875433912' - 2e as datetime) as ExcelToSql, -- even varchar works!
cast(cast('2020-01-31 21:00:37.490' as datetime) + 2e as float) as SqlToExcel
-- Results:
-- ExcelToSql SqlToExcel
-- 2020-01-31 21:00:37.490 43861.875433912
this actually worked for me
dateadd(mi,CONVERT(numeric(17,5),41869.166666666664)*1440,'1899-12-30')
(minus 1 more day in the date)
referring to the negative commented post
SSIS Solution
The DT_DATE data type is implemented using an 8-byte floating-point number. Days are represented by whole number increments, starting with 30 December 1899, and midnight as time zero. Hour values are expressed as the absolute value of the fractional part of the number. However, a floating point value cannot represent all real values; therefore, there are limits on the range of dates that can be presented in DT_DATE. Read more
From the description above you can see that you can convert these values implicitly when mapping them to a DT_DATE Column after converting it to a 8-byte floating-point number DT_R8.
Use a derived column transformation to convert this column to 8-byte floating-point number:
(DT_R8)[dateColumn]
Then map it to a DT_DATE column
Or cast it twice:
(DT_DATE)(DT_R8)[dateColumn]
You can check my full answer here:
Is there a better way to parse [Integer].[Integer] style dates in SSIS?
Found this topic helpful so much so created a quick SQL UDF for it.
CREATE FUNCTION dbo.ConvertExcelSerialDateToSQL
(
#serial INT
)
RETURNS DATETIME
AS
BEGIN
DECLARE #dt AS DATETIME
SELECT #dt =
CASE
WHEN #serial is not null THEN CAST(#serial - 2 AS DATETIME)
ELSE NULL
END
RETURN #dt
END
GO
I had to take this to the next level because my Excel dates also had times, so I had values like this:
42039.46406 --> 02/04/2015 11:08 AM
42002.37709 --> 12/29/2014 09:03 AM
42032.61869 --> 01/28/2015 02:50 PM
(also, to complicate it a little more, my numeric value with decimal was saved as an NVARCHAR)
The SQL I used to make this conversion is:
SELECT DATEADD(SECOND, (
CONVERT(FLOAT, t.ColumnName) -
FLOOR(CONVERT(FLOAT, t.ColumnName))
) * 86400,
DATEADD(DAY, CONVERT(FLOAT, t.ColumnName), '1899-12-30')
)
In postgresql, you can use the following syntax:
SELECT ((DATE('1899-12-30') + INTERVAL '1 day' * FLOOR(38242.7711805556)) + (INTERVAL '1 sec' * (38242.7711805556 - FLOOR(38242.7711805556)) * 3600 * 24)) as date
In this case, 38242.7711805556 represents 2004-09-12 18:30:30 in excel format
In addition of #Nick.McDermaid answer I would like to post this solution, which convert not only the day but also the hours, minutes and seconds:
SELECT DATEADD(s, (42948.123 - FLOOR(42948.123))*3600*24, dateadd(d, FLOOR(42948.123),'1899-12-30'))
For example
42948.123 to 2017-08-01 02:57:07.000
42818.7166666667 to 2017-03-24 17:12:00.000
You can do this if you just need to display the date in a view:
CAST will be faster than CONVERT if you have a large amount of data, also remember to subtract (2) from the excel date:
CAST(CAST(CAST([Column_With_Date]-2 AS INT)AS smalldatetime) AS DATE)
If you need to update the column to show a date you can either update through a join (self join if necessary) or simply try the following:
You may not need to cast the excel date as INT but since the table I was working with was a varchar I had to do that manipulation first. I also did not want the "time" element so I needed to remove that element with the final cast as "date."
UPDATE [Table_with_Date]
SET [Column_With_Excel_Date] = CAST(CAST(CAST([Column_With_Excel_Date]-2 AS INT)AS smalldatetime) AS DATE)
If you are unsure of what you would like to do with this test and re-test! Make a copy of your table if you need. You can always create a view!
Google BigQuery solution
Standard SQL
Select Date, DATETIME_ADD(DATETIME(xy, xm, xd, 0, 0, 0), INTERVAL xonlyseconds SECOND) xaxsa
from (
Select Date, EXTRACT(YEAR FROM xonlydate) xy, EXTRACT(MONTH FROM xonlydate) xm, EXTRACT(DAY FROM xonlydate) xd, xonlyseconds
From (
Select Date
, DATE_ADD(DATE '1899-12-30', INTERVAL cast(FLOOR(cast(Date as FLOAT64)) as INT64) DAY ) xonlydate
, cast(FLOOR( ( cast(Date as FLOAT64) - cast(FLOOR( cast(Date as FLOAT64)) as INT64) ) * 86400 ) as INT64) xonlyseconds
FROM (Select '43168.682974537034' Date) -- 09.03.2018 16:23:28
) xx1
)
For those looking how to do this in excel (outside of formatting to a date field) you can do this by using the Text function https://exceljet.net/excel-functions/excel-text-function
i.e.
A1 = 132134
=Text(A1,"MM-DD-YYYY") will result in a date
This worked for me because sometimes the field was a numeric to get the time portion.
Command:
dateadd(mi,CONVERT(numeric(17,5),41869.166666666664)*1440,'1899-12-31')

SQL: retrieve records between dates in all databases

I would like to select all the records in a table between two dates. I have a query like:
SELECT * FROM <table> WHERE (thedate BETWEEN DATE('2012-04-01') AND DATE('2012-06-30'))
This is fine for HSQL, Oracle, PostgreSQL, but it doesn't work in SQL Server due to the lack of the Date function. Is there a way to get this to work generally for all databases (including SQL Server) or do I need to use an ORM like Hibernate (I know I should, but I'm specifically asking if this can be done in SQL)?
There's no need for the Date(...) as far as i can tell. This example seems to work
DECLARE #TheDate Date = '2012-07-01';
SELECT 'hello' WHERE (#TheDate BETWEEN '2012-04-01' AND '2012-06-30')
--None returned
SET #TheDate = '2012-05-01'
SELECT 'hello' WHERE (#TheDate BETWEEN '2012-04-01' AND '2012-06-30')
--selects hello
Edit Btw worth looking at This Question with the date time answer (will post here just to save effort)
The between statement can cause issues with range boundaries for dates as
BETWEEN '01/01/2009' AND '01/31/2009'
is really interpreted as
BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 00:00:00'
so will miss anything that occurred during the day of Jan 31st. In this case, you will have to use:
myDate >= '01/01/2009 00:00:00' AND myDate < '02/01/2009 00:00:00' --CORRECT!
or
BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 23:59:59' --WRONG! (see update!)
UPDATE: It is entirely possible to have records created within that last second of the day, with a datetime as late as 01/01/2009 23:59:59.997!!
For this reason, the BETWEEN (firstday) AND (lastday 23:59:59) approach is not recommended.
Use the myDate >= (firstday) AND myDate < (Lastday+1) approach instead.
in sql-server I think you may want to look into the
DATEADD ( datepart , number, date )
DATEDIFF ( datepart , startdate , enddate )
but I think you're looking for
CAST ( expression AS data_type )
or
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
usage:
Select * from myTable where thedateToCompare >= CONVERT ( datetime , "dd/mm/yy hh:mi:ss:mmmAM" ) and thedateToCompare <= CONVERT ( datetime , "dd/mm/yy hh:mi:ss:mmmAM" )
something that may change your approach is weather your source 'thedate' column type datetime, smalldatetime or is it stored in a column defined as string?
You may want to check out a link! for more detial on datetime constants that you could use for your string variations.

The result of [Datetime + 420./1440] (7 hours) is wrong (precision in ms is lost)

Could pls explain why I' not able to get valid result ( with precision to miliseconds ) after doing this (adding 7hrs to date):
select getdate() as dt into #t1
select dt, dt+(7.*60/1440) as dt_new from #t1
I got:
dt dt_new
2010-10-25 04:56:33.640 2010-10-25 11:56:33.580
As you can see there is difference in 60ms for dt_new - WHY?
AFAIR I WAS able to do this sort of things in Oracle 10g - even with minutes:
date + 5/1440 -to produce 5-min periods of datetime.
UPD:
My mistake - the CORRECT way of doing this is:
select getdate(), getdate()+(convert(float, 5)/1440) as dt_new
I suppose there is wrong implicit convertion choosen by Ms Sql server.
UPD2:
Thanks for replies, but my point was "arifmetic" method - because there are cases when calculating dates without dateadd really improves Query Plan - by eliminating Table scan that is the case with Dateadd and other functions.
Anyway it works - thanks!
You could also use the inbuilt functions, such as
select dt, DATEADD(hour, 7, dt) as dt_new from #t1
My guess as to the imprecision in using + is that its converting from datetime to integer, then back again.
Actually, the even correcter way of adding 7 hours to a datetime is to use DATEADD:
select getdate() as dt into #t1
select dt, dateadd(hh, 7, dt) as dt_new from #t1
gives
dt dt_new
----------------------- -----------------------
2010-10-25 13:16:35.067 2010-10-25 20:16:35.067
You could achieve better result with dateadd() function (like previous posters noted). If you want use arithmetics, use better datatype, like float - cast(7 as float) instead of 7. is working well.