Convert String to Datetime or Datetime 2 - sql-server-2008-r2

I need help because I can't convert it by myself. I got the following String:
20160803093000000
Inside the String there is the Date and Time: 2016-08-03 09:30:00:00
I need it as Datetime or Datetime2.

Here it is with the milliseconds (I entered another date, so you can see the change in the milliseconds):
DECLARE #t VARCHAR(50)
SET #t = '20160803093012345'
SELECT CONVERT(DATETIME,STUFF(STUFF(STUFF(STUFF(#t,9,0,' '),12,0,':'),15,0,':'),18,0,'.'))
I read something about loss of precision at the millisecond level, though. So there is still some need for research on the topic.
If you need the precision of the milliseconds, you can use DATETIME2 and just do:
SELECT CONVERT(DATETIME2,STUFF(STUFF(STUFF(STUFF(#t,9,0,' '),12,0,':'),15,0,':'),18,0,'.'))
I just read a great article regarding the precision. It says:
If you want to store the exact same value you had in DATETIME, just
choose DATETIME2(3), you get the same precision but it only takes 7
bytes to store the value instead of 8.
Here is the link for more information.

SELECT CONVERT(DATETIME2,STUFF(STUFF(STUFF(STUFF(#t,9,0,' '),12,0,':'),15,0,':'),18,0,'.'))
Works fine! Thank you a lot.

Related

TSQL convert numbers to time

I have a time field which is storing numbers in '49235062'. Can these be converted to an actual readable time?
This does not look anything like a time stamp.
Thanks
Just a guess, but is this milliseconds from midnight?
Example
Select dateadd(MILLISECOND,49235062,0)
Returns
1900-01-01 13:40:35.063 -- 1:40 PM
If so, then it is a small matter to convert to time or format as time
Assuming that this is a UNIX timestamp (number of seconds since 1/1/1970), try the following:
DECLARE #timeStamp varchar(10) = '49235062'
SELECT #timeStamp, CONVERT(TIME, dateadd(S, CAST(#timeStamp AS int), '1970-01-01'))
Produces the following
49235062 20:24:22.0000000
So your time is 20:24:22 which is the answer that #pac0 suggested.

Converting bigint to timestamp in presto

I have a column in my dataset that has a datatype of bigint:
Col1 Col2
1 1519778444938790
2 1520563808877450
3 1519880608427160
4 1520319586578960
5 1519999133096120
How do I convert Col2 to the following format:
year-month-day hr:mm:ss
I am not sure what format my current column is in but I know that it is supposed to be a timestamp.
Any help will be great, thanks!
Have you tried to use functions like from_unixtime? You could use it to convert unix time to timestamp, then you could use date_format to display it in way you want. Notice that in your example your unix time is with microseconds, so you might want to convert it first to milliseconds.
I have not tested that but I am assuming that your code should look like:
date_format(from_unixtime(col2/1000), '%Y-%m-%d %h:%i:%s')
Notice that from_unixtime accepts also a time zone.
Please visit this page to see the more details about date related functions: https://docs.starburstdata.com/latest/functions/datetime.html
I believe the denominator should be 1000000 not 1000. Probably a typo. Anyways juts adding the test results here for others reference.
-- Microseconds
select date_format(from_unixtime(cast('1519778444938790' as bigint)/1000000), '%Y-%m-%d %h:%i:%s');
2018-02-28 12:40:44
If you need to filter the data where the column is in BIGINT Unix format, then you can use the following snippet to compare : from_unixtime(d.started_on /1000) >= CAST('2022-05-10 22:00:00' AS TIMESTAMP )
Accepted answer is a bit misleading. You should divide by 1000.0 otherwise you'll lose ms precision and be limited to second precision:
date_format(from_unixtime(col2/1000.0), '%Y-%m-%d %h:%i:%s')

Converting string timestamp into date

I have dates in a postgres database. The problem is they are stored in a string field and have values similar to: "1187222400000" (which would correspond to 07.08.2007).
I would like to convert them into readable dates usind some SQL to_date() expression or something similar but can't come up with the correct syntax to make it work.
There really isn't enough information here for a conclusion, so I propose this 'scientific-wild-ass-guess' to resolve your puzzle. :)
It appears this number is UNIX 'epoch time' in milliseconds. I'll show this example as if your string field had the arbitrary name, 'epoch_milli'. In postgresql you can convert it to a time stamp using this statement:
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + epoch_milli * INTERVAL '1 millisecond';
or using this built-in postgresql function:
SELECT to_timestamp(epoch_milli / 1000)
either of which, for the example '1187222400000', produces the result
"2007-08-15 17:00:00-07"
You can do some of your own sleuthing with quite a few values selected similarly to this:
SELECT to_timestamp(epoch_milli/1000)::DATE
FROM (VALUES (1187222400000),(1194122400000)) AS val(epoch_milli);
"Well, bollocks, man. I just want the date." Point taken.
Simply cast the timestamp to a date to discard the excess bits:
SELECT to_timestamp(epoch_milli / 1000)::DATE
Of course its possible that this value is a conversion or is relative to some other value, hence the request for a second example data point.

How to convert a 5 digit INT to date

I apologise if this question has been asked but I cannot find an answer that quite fits.
I have a database with dates stored as 5 digit integers. I can covert these to datetime, however the dates are showing in the future.
For example,
select convert(datetime,StartDate,103)
from dpm.Schedule
where ScheduleID like 50003;
Gives the results of
2107-05-31 00:00:00:000 but this date should actually be 26/05/2008.
I am pretty new to T-SQL and have looked for sometime to find the answer to this but I am reaching the end of my sanity.
We cannot answer because you are doing a lot of confusion. To start 5003 is the Id of the record and [StartDate] is the value you are trying to convert.
Also drop using operator LIKE in the Id
Example:
select convert(datetime,50003,103)
returns:
2036-11-26 00:00:00.000
but you are trying to convert the value returned by
select StartDate
from dpm.Schedule
where ScheduleID = 50003;
Please edit your question and show us a nice example using SQL Fiddle

T-SQL Conversion Failed when converting date/or time from character string

Perfmon isn't so kind with the way it creates the database when logging directly to SQL:
select top 1 Convert(datetime, CounterDateTime) from CounterData
returns
Conversion failed when converting date and/or time from character string.
The value of that cell is "2012-01-25 14:12:10.802". What is the proper way to convert this to a datetime field during selection?
CONVERT(DATETIME, SUBSTRING(CounterDateTime, 1, 23), 102)
I figured out that the following works (instead of the CHAR(24) it is):
select top 1 Cast(Cast(CounterDateTime as CHAR(23)) as datetime) from CounterData
Hoping there is a better solution though.
No better answer but this makes the taste a trifle less bitter.
CAST(CAST(CounterData.CounterDateTime AS CHAR(NN)) AS DATETIME) AS CounterDateTime
Also truncates the value so that additional T-SQL DateTime truncation to the minute, hour, etc is not needed.