Converting string with microseconds to DATE - sql

I get a string as input from an application that is supposed to be a date. For some asinine reason the developers of the application decided provide precision to the microsecond. Well, actually, to the tenth of a microsecond.
The string is in the format: 2014-08-15T17:38:22.2930000
Before they changed this input format I was using the following to convert the date.
select DATEADD(dd, 30, #Date)
I know I could just do a substring on the input and lop off the last 4 characters, however, I'm wondering if there is some way that I can use CONVERT to just convert the date, or if SQL just doesn't support dates with this type of precision.

Either of these should work:
SELECT CONVERT(DATE, '2014-08-15T17:38:22.2930000', 101)
SELECT CONVERT(DATETIME2, '2014-08-15T17:38:22.2930000', 101)

Whoops, the following assumes that a date and time are desired. Substituting in DATE for DATETIME2 will work, just as it does with CONVERT and the time component is simply dropped.
A CAST to DATETIME2 works (this is for SQL Server 2008 R2 - YMMV elsewhere),
select CAST('2014-08-15T17:38:22.2930001' AS DATETIME2) t;
as DATETIME2 has a maximum precision of 100ns, or 7 digits after the decimal.
Then it can be converted to a normal DATETIME (either explicitly or implicitly), although this loses precision,
select CAST(CAST('2014-08-15T17:38:22.2930001' AS DATETIME2) AS DATETIME) t;
However such lost of precision is not allowed directly in a CHAR -> DATETIME cast, and there are precision tolerances for the other casts.
select CAST('2014-08-15T17:38:22.293' AS DATETIME) t; -- OK*
select CAST('2014-08-15T17:38:22.2930' AS DATETIME) t; -- fail
-- although a cast to DATETIME2 is happy to lose some precision
select CAST('2014-08-15T17:38:22.2930001' AS DATETIME2) t; -- OK, no loss
select CAST('2014-08-15T17:38:22.29300014' AS DATETIME2) t; -- OK, loss
select CAST('2014-08-15T17:38:22.293000144' AS DATETIME2) t; -- OK, loss
select CAST('2014-08-15T17:38:22.2930001444' AS DATETIME2) t; -- fail
-- and a cast to DATE works up to 8 digits after the decimal
select CAST('2014-08-15T17:38:22.29300014' AS DATE) t; -- OK, date only
select CAST('2014-08-15T17:38:22.293000144' AS DATE) t; -- fail
*DATETIME has a precision of 3 decimal digits, but it does not have millisecond accuracy.
As far as I am aware, the same rules for CHAR -> DATETIME/DATETIME2 apply to COVERT and losing maximum precision still results in an error.
The ISO 8601 standard provides no limit to precision, but "the number of decimal places needs to be agreed to by the communicating parties".

SELECT CAST('2014-08-15T17:38:22.2930000' AS DATETIME2)
RESULT: 2014-08-15 17:38:22.2930000
Now since it is a valid datetime value in sql server, you have access to all the datetime functions, like you have mentioned in your question you wanted to use DATEADD()
Datetime2 gives you more precision than simple datetime data type, Read more about DATETIME2 data type here.

Related

converting datetime to float in sql

converting datetime to float in sql
Hello, I have a question, how do I convert the difference of the following dates, for example: (2023-01-02 18:00:00)-(2023-01-01 17:00:00) into a decimal number?
I know how the conversion to an integer happens(cast(cast(test as date) - cast(testtwo as date) as int), but here I am confused.
You man look at DATEDIFF function. It allows you to:
returns the count (as a signed integer value) of the specified
datepart boundaries crossed between the specified startdate and
enddate.
Then, convert the seconds to a day:
SELECT DATEDIFF(SECOND, '2023-01-01 17:00:00', '2023-01-02 18:00:00') * 1.0 / (60*60*24) -- 1.041666666666
The DATETIME datatype is incredibly useful and flexible for things like this, although the rounded 3.3 millisecond resolution causes some folks pain.
If you want "Decimal Days" where the fractional part to the right of the decimal point represents time as a partial day, then the following will do the trick.
--===== Setup test values for clarity and ease of testing.
-- These variables could be changed out for columns in a table.
DECLARE #StartDT DATETIME = '2023-01-01 17:00:00'
,#EndDT DATETIME = '2023-01-02 18:00:00'
;
--===== Calulate the difference (duration) in decimal days.
SELECT DecimalDays = CONVERT(FLOAT,#EndDT-#StartDT)
;

Explicit conversion from data type date to bigint is not allowed

This used to work with a column type of DATEIME but now it won't with DATE.
CONVERT(BIGINT,ev.StartDate) * -1
Is there anyway to get a BIGINT value from a DATE column?
You can cast the startdate as datetime for conversion.
CONVERT(BIGINT,CAST(ev.StartDate as DATETIME)) * -1
Yet another option. This will even flip the sign for you
Example
Declare #YourTable table (StartDate date)
Insert Into #YourTable values ('2017-05-30')
Select DateDiff(DAY,StartDate,-1)
From #YourTable
Returns
-42884
First, dates in SQL Server are counted by days from the year 1900. A big int starts to be useful at about 2.1 billion. That corresponds to a year in the range of 5.8 million. Do you really have dates that large?
Of course, casting to an int is not permitted. You can cast datetime values . . . but are there other ways?
One simple way is:
select 1 + datediff(day, 0, datecol)
The "+ 1" is needed so the value matches the actual conversion. (You can use "-1" instead of "0" instead.)
Or, perhaps you want Unix time in seconds or milliseconds. For that:
select datediff_big(ms, '1970-01-01', datecol)
You might require to convert to varchar and then bigint
select Convert(bigint,convert(varchar(10),ev.StartDate,112))*(-1)

datediff() of 2 timestamps that contain decimal values

I've been stumbling with this issue for a couple days now, and cannot seem to figure out why, when my getdate() insert into the columns are providing a millisecond decimal to the military time format, I still cannot seem to be able to pull a decimal format datediff() result. Does it have to do with the engine not recognizing the decimal due to the surrounding '' characters?
When I use:
select datediff(s,'2013-06-01 21:59:59.141','2013-06-01 23:59:59.997')
It returns:
7200
And when I use:
select cast(datediff(s,'2013-06-01 21:59:59.141','2013-06-01 23:59:59.997') as float);
It returns:
7200
I am at a loss as to what I am missing in order to result in a decimal value.
Thanks
If you are trying to get the milliseconds of the difference, and you want to convert the units to seconds, you can try using something like the following:
SELECT DATEDIFF(MS,'2013-06-01 21:59:59.141','2013-06-01 23:59:59.997') / 1000.0
That'll produce: 7200.856000.
Please note that DATEDIFF(MS, ...) requires guarding for long time spans or it will give an overflow:
SELECT datediff(MS, '2013-06-30 23:59:59.997', '2013-06-01 21:59:59.141')
-- FAILURE: The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large
You can use following method which is overflow-safe and gives you a float result:
SELECT cast(cast('2013-06-01 23:59:59.997' as datetime)-cast('2013-06-01 21:59:59.141' as datetime) as float) * 24.0
-- Returned 2.00023796296296
SELECT cast(cast('2013-06-30 23:59:59.997' as datetime)-cast('2013-06-01 21:59:59.141' as datetime) as float) * 24.0
-- Returned 698.000237962963
I'm not sure how this method works when time zone changed during the measured date period.

Convert datetime to specific format?

I want to convert a datetime to a specific format. The conversion should result in a variable of type datetime and not char or varchar. How do I do this in SQL server 2000, 2005 and 2008 ?
select CONVERT(varchar(30),getdate(),120)
I tried this, but it gives me a string. I want a datetime without the milli-seconds. SS 2012 has an option for this, but not previous versions.
http://blog.sqlauthority.com/2012/11/21/sql-server-display-datetime-in-specific-format-sql-in-sixty-seconds-033-video/
You can't have it both ways ... a variable of type "datetime" is defined as:
Defines a date that is combined with a time of day
with fractional seconds that is based on a 24-hour clock
You can CONVERT and DISPLAY in whatever format you choose, but the datetime data type will always have the milliseconds, even if you set them to zero.
You can remove the milliseconds from the datetime like this:
DATEADD(ms, -DATEPART(ms, date), date) > '2013-11-18 03:21:52'
Also check SQL Server Date Formats
or may be try like this to remove the millisecond part:-
declare #str datetime
set #str = '2013-11-18 17:24:05.784'
select convert(datetime, convert(char(19), #str, 126))

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')