May 23, 2017 at 5:13 am
Hi
I am trying to convert a "calendar date" column that is a float to date
the "date" looks like 201601 should be 01-2016 or year 2016, month 01
I have tried to use date part and cast with no joy
please help
May 23, 2017 at 5:23 am
What does your float look like?
I normally use CONVERT to get a datetime value e.g.
SELECT CONVERT(DATETIME,42811.5145744599)
May 23, 2017 at 5:27 am
deleted ....code window failing
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
May 23, 2017 at 5:30 am
DECLARE @d FLOAT= 201601;
SELECT CONVERT( DATE, CONVERT(VARCHAR(8), CONVERT(INT, @d * 100 + 1)));
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
May 23, 2017 at 5:47 am
Or this:DECLARE @date float = 201601;
SELECT DATEFROMPARTS(CAST(@date/100 AS int),CAST(@date AS int)%100,1)
John
May 23, 2017 at 6:29 am
Chipping in, float and datetime are compatible, in fact the conversion between the two is one of the fastest date/time operations in SQL Server
😎
USE TEEST;
GO
SET NOCOUNT ON;
DECLARE @FDATE FLOAT = CONVERT(FLOAT,GETDATE(),0);
DECLARE @TDATE DATE = CONVERT(DATE,CONVERT(DATETIME,(FLOOR(@FDATE)),0),0);
SELECT @TDATE AS RESULT_DATE;
May 23, 2017 at 8:03 am
Hi
thanks team , amazing !
May 23, 2017 at 8:27 am
J Livingston SQL - Tuesday, May 23, 2017 5:30 AMDECLARE @d FLOAT= 201601;
SELECT CONVERT( DATE, CONVERT(VARCHAR(8), CONVERT(INT, @d * 100 + 1)));
Careful here, add the format parameter to the convert, otherwise it might fail
😎
May 23, 2017 at 8:37 am
Eirikur Eiriksson - Tuesday, May 23, 2017 8:27 AMJ Livingston SQL - Tuesday, May 23, 2017 5:30 AMDECLARE @d FLOAT= 201601;
SELECT CONVERT( DATE, CONVERT(VARCHAR(8), CONVERT(INT, @d * 100 + 1)));Careful here, add the format parameter to the convert, otherwise it might fail
😎
would this be better Eirikur?DECLARE @d FLOAT= 201613;
SELECT TRY_CONVERT( DATE, CONVERT(VARCHAR(8), CONVERT(INT, @d * 100 + 1)));
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
May 23, 2017 at 8:44 am
Eirikur Eiriksson - Tuesday, May 23, 2017 6:29 AMChipping in, float and datetime are compatible, in fact the conversion between the two is one of the fastest date/time operations in SQL Server
😎
USE TEEST;
GO
SET NOCOUNT ON;DECLARE @FDATE FLOAT = CONVERT(FLOAT,GETDATE(),0);
DECLARE @TDATE DATE = CONVERT(DATE,CONVERT(DATETIME,(FLOOR(@FDATE)),0),0);SELECT @TDATE AS RESULT_DATE;
+1 to THAT! Conversion to Float and using Floor is actually faster than the DATEADD/DATEDIFF trick for "dropping time" from dates (among other things). I don't post it because it's not worth the argument with all the "Never do direct date math" purists out there.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 23, 2017 at 8:51 am
J Livingston SQL - Tuesday, May 23, 2017 8:37 AMEirikur Eiriksson - Tuesday, May 23, 2017 8:27 AMJ Livingston SQL - Tuesday, May 23, 2017 5:30 AMDECLARE @d FLOAT= 201601;
SELECT CONVERT( DATE, CONVERT(VARCHAR(8), CONVERT(INT, @d * 100 + 1)));Careful here, add the format parameter to the convert, otherwise it might fail
😎would this be better Eirikur?
DECLARE @d FLOAT= 201613;
SELECT TRY_CONVERT( DATE, CONVERT(VARCHAR(8), CONVERT(INT, @d * 100 + 1)));
Skipping the character conversion and convert straight to datetime is safer, when I change the collation/locale then your query either errors or returns NULL
😎
DECLARE @d FLOAT= CONVERT(FLOAT,GETDATE(),0);
SELECT CONVERT( DATE, CONVERT(VARCHAR(20), CONVERT(DATETIME, @d,12),12),12);
May 23, 2017 at 9:24 am
Jeff Moden - Tuesday, May 23, 2017 8:44 AMEirikur Eiriksson - Tuesday, May 23, 2017 6:29 AMChipping in, float and datetime are compatible, in fact the conversion between the two is one of the fastest date/time operations in SQL Server
😎
USE TEEST;
GO
SET NOCOUNT ON;DECLARE @FDATE FLOAT = CONVERT(FLOAT,GETDATE(),0);
DECLARE @TDATE DATE = CONVERT(DATE,CONVERT(DATETIME,(FLOOR(@FDATE)),0),0);SELECT @TDATE AS RESULT_DATE;
+1 to THAT! Conversion to Float and using Floor is actually faster than the DATEADD/DATEDIFF trick for "dropping time" from dates (among other things). I don't post it because it's not worth the argument with all the "Never do direct date math" purists out there.
Thanks Jeff, will try to find the last test harness I did on this, IIRC it's around 70% faster than using the DATEXX functions
😎
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply