How to parse SQL Server binary date format in mysql? - mysql

How to convert CAST(0xE3350B00 AS DATE) to mysql ?
I used various forum supports and have following results
0xE3350B00 => 2059-04-03 22:56
0x0000A17F00000000 => 2013-03-12
But both dates belong to same line of data so i am confident that 0xE3350B00 should convert to nearby 2013-03-12 but not finding it technically? Can anyone help if conversion of 0xE3350B00 date??
I used following code in sql function:
return date_add(date_add("1900-01-01 00:00:00", interval conv(substr(HEX(raw_data), 5, 4), 16, 10) DAY), interval conv(substr(HEX(raw_data), 1, 4), 16, 10) MINUTE);

The code you have looks as though it parses SQL Server datetime values (though incorrectly if they have any time part).
This looks like a SQL Server binary date format.
These are stored differently. They occupy three bytes and represents the number of days since 0001-01-01 as a byte reversed integer.
You can use
SELECT
CAST(
'0001-01-01 00:00:00' +
INTERVAL CAST(CONV(
CONCAT(substr(HEX(BinaryData),5,2),
substr(HEX(BinaryData),3,2),
substr(HEX(BinaryData),1,2))
, 16, 10) AS SIGNED) DAY
AS DATE) AS converted_date
FROM
(
SELECT 0xE3350B00 AS BinaryData
) d
Which evaluates to 2012-07-08.
SQL Fiddle

Related

MySQL handling over 24h date format

I'm currently working with a data set that has time formatted over 24h, for example, 27:20:01 (which is 03:20:01). I've been using a query like this STR_TO_DATE(stop_times.arrival_time, "%H:%i:%s") >= STR_TO_DATE("03:14:46", "%H:%i:%s")
to get dates that are not older than current time, but since some dates are formatted over 24h STR_TO_DATE doesn't work. What is the best approach for time formatted like this?
STR_TO_DATE(
CONCAT(
SUBSTRING(stop_times.arrival_time, 1, 2) % 24,
SUBSTRING(stop_times.arrival_time, 3, 8)), "%H:%i:%s")
>= STR_TO_DATE("03:14:46", "%H:%i:%s")

Selecting dynamic Unix Epoch time with SQL

I'm attempting to build a "last 30 days" dynamic SQL date filter for a user application. The date column is a unix epoch millisecond timestamp.
Previous iterations of the tool allowed the user to choose a date range, I'm now just changing it to choose the last 30.
The data is stored in Redshift, which does not support from_unixtime.
I have two challenges:
The data is stored in UTC and needs to be filtered with dates in EST (UTC -5).
"Choosing the last 30 days" means cutting off at midnight yesterday, and taking yesterday minus 29.
Previously, my code looked like this:
"datecol" >= DATEDIFF(millisecs, '1969-12-31 19:00:00', ''start date' 00:00:00')
AND "datecol" <= DATEDIFF(millisecs, '1969-12-31 19:00:00', ''end date' 23:59:59')
The application would update the start and end dates as described by the user. This code is adjusted for the time difference.
How can I use GETDATE() and DATEADD() on a Unix timestamp, using the constraints of Redshift SQL?
Thanks.
extract('epoch' from ts) gives you unix timestamps and you just add 5 hours to query UTC as if it is EST (if EST is UTC-5 then UTC is EST+5)
between extract('epoch' from ('<<date1>>' + interval '5 hour'))
and extract('epoch' from ('<<date2>>' + interval '29 hour' - interval '1 second'))
also, from_unixtime can be expressed in Redshift as the following:
select timestamp 'epoch' + unix_ts_column * interval '1 second'
a bit ugly but works just like that
I think you want to write a User Defined Function (UDF) for your Redshift database using Python and the python standard datetime module. See http://docs.aws.amazon.com/redshift/latest/dg/user-defined-functions.html
Follow the section titled Creating a Scalar Python UDF.
I don't quite understand your query or the context, but I think you can figure out how to get what you want using UDLs.
For example to get the milliseconds between two datetimes (one in UTC, one in EST) you would write is like the following (not tested):
CREATE FUNCTION datediff_py(a datetime, b datetime)
returns float
stable
as $$
#python code goes here between the $$
from datetime import datetime
FMT = '%Y-%m-%d %H:%M:%S' #dates like '2016-12-24 23:59:59'
tdelta = datetime.strptime(a + " UTC", FMT + " %Z") - datetime.strptime(b + " EST", FMT + " %Z")
return tdelta.total_seconds()*1000
$$ language plpythonu;
This computes the milliseconds between an SQL datetime a that is in UTC and b that is in EST. The %Z format is used for timezones. A usage would be:
"datecol" >= datediff_py('1969-12-31 19:00:00', user_date)
Of course Unix epoch is actually '1970-01-01 00:00:00'.
There are plenty of other date functions in the Python standard library datetime module, so you can write other UDLs if you need things like GETDATE() or DATEADD(), for exampling using timedelta

Epoch Date Conversion to SQL Server

Good Day,
I have a question. What type of epoch date's are these and how would I convert them into a SQL Server datetime format.
37564691530
37564704499
37564708633
37564721033
37564743361
37564746236
I have googled for 2 days and cant find anything except this formula which gives me an arithmetic overflow message when i try to convert it.
select
DATEADD(ss, 37564691530 - 3600 * 5, CONVERT(DATETIME, '1900-01-01 00:00:00', 102))
Any help would really be appreciated.
13 digit epoch represent total milliseconds 10 digit epoch represent total seconds. You have first one - milliseconds.
And sql DateAdd function accept second parameter(Increment) as Integer. You try to pass a bigint value. Thats why throw Arithmetic overflow error.
try this
DECLARE #MS BIGINT
SET #MS = 37564746236
select DATEADD(SECOND, #MS / 1000, '1970-01-01')

How to use MS Access Dateserial, Month, Weekday in SQL Server

I'm trying to convert my MS Access query That uses Dateserial, Month, and Weekday functions to work in SQL Server 2008.
With the following values in a record:
[dbo_TBL_TEST].[MFG_YYYY] = "2012"
[dbo_TBL_TEST].[MFG_WW] = "43"
The result from the following MS Access Query expression will be 82.
MFG_Test_INDEX: (Month(DateSerial(Val([dbo_TBL_TEST].[MFG_YYYY]),1,1)-Weekday(DateSerial(Val([dbo_TBL_TEST].[MFG_YYYY]),1,3))+(Val([dbo_TBL_TEST].[MFG_WW])*7))+(Val([dbo_TBL_TEST].[MFG_YYYY])-2006)*12)
Is there a way to do this in SQL Server 2008?
T-SQL has a MONTH() function. It doesn't have a direct DateSerial() equivalent, but you could "glue together" the date string and then use CAST() to convert it to the appropriate date type. And in place of Weekday() you can use DATEPART(dw, datevalue). Details on these and other T-SQL date functions are available here.
I found out how to do it in SQL-Server 2008....
This returns the number of months from the current year,work week (MFG_YYYY,MFG_WW) since Jan 2006 using the ISO 8601 standard.
DATEPART ( Month, Dateadd(weekday,+4,Dateadd(day,-1,DATEADD(DAY, 7 * MFG_WW,1,2) + DATEDIFF(DAY, 4, DATEADD(YEAR, MFG_YYYY - 1900, 7)) / 7 * 7, 1 - 8))))+((DATEPART ( Year, Dateadd(weekday,+4,Dateadd(day,-1,DATEADD(DAY, 7 * MFG_WW + DATEDIFF(DAY, 4, DATEADD(YEAR, MFG_YYYY - 1900, 7)) / 7 * 7, 1 - 8))))-2006)*12) AS MFG_Index

SQL - Converting datetime format to 12 hours format with hh:mm:ss

I have a query like,
SELECT CONVERT(VARCHAR, getdate(), 100) AS TwelveHH
output of the above query is Sep 26 2012 6:02PM & i want to get the Hours from the above query means, i will use the query like,
SELECT RIGHT(CONVERT(VARCHAR, getdate(), 100), 7) AS getTime
output of the above query is 6:02PM.
But, i want output with seconds like 6:02:19PM - (hh:mm:ss) format.
How to i get hh:mm:ss format?
Several years ago I wrote this handy guide which still helps me today - mostly because there is no way I am ever going to memorize what all of the different style numbers mean:
SELECT LTRIM(RIGHT(CONVERT(CHAR(20), CURRENT_TIMESTAMP, 22), 11));
In my time zone this returns:
9:12:55 AM
This isn't precisely what you want - you can use REPLACE or STUFF if it's really important to remove that space. e.g.
SELECT REPLACE(LTRIM(RIGHT(CONVERT(CHAR(20), CURRENT_TIMESTAMP, 22), 11)), ' ', '');
In SQL Server 2012, you will be able to do this easier - you can format using parity with C#:
SELECT FORMAT(CURRENT_TIMESTAMP, 'h:mm:ss tt');
EDIT Based on new and changed requirements in the comments below:
SELECT CONVERT(CHAR(11), CURRENT_TIMESTAMP, 109)
+ ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), CURRENT_TIMESTAMP, 22), 11));
Result:
Sep 26 2012 10:03:52 AM
You should do this:
SELECT CONVERT(VARCHAR(10),GETDATE(),108) AS getTime