Convert timestamp to datetime changing format - sql

I have a timestamp 2020-01-08T16:06:00+00:00 format value, I want to convert it to datetime format as 2020-01-08 16:06:00.
I have tried to convert to datetime, but getting error says:
failed when converting date and/or time from character string.
I would like to convert timestamp to datetime.

If you don't care about the timezone, you can use:
select convert(datetime, left('2020-01-08T16:06:00+00:00', 19))

If you're simply interested in dropping the the offset, and not converting the timestamp to local time (as the offset sets the timestamp at UTC), try this:
DECLARE #timestamp AS DATETIMEOFFSET = '2020-01-08T16:06:00+00:00' ;
SELECT CAST ( #timestamp AS DATETIME2(0) ) AS formatted_timestamp ;
(I've assumed the data type for your timestamp is DATETIMEOFFSET. But this could also work if the data type is DATETIME or DATETIME2.)

Related

Convert Unixtime to MMddyyyy

I'm trying to convert a column which has unixtime (ex 1542862806000) to regular DTS
select unix_timestamp(column_name) from table;
But i get error:
AnalysisException: No matching function with signature: unix_timestamp(BIGINT).
My column type is bigint
You are looking for from_unixtime not unix_timestamp.
select from_unixtime(cast(column_name/1000 as bigint),'MMddyyyy')
from table
unix_timestamp converts a date/date format string to a bigint representing the number of seconds since 1970-01-01 00:00:00 UTC.
from_unixtime takes a bigint input and converts it to the required date format.

What is the difference between hh and HH in dateformatting in SQL?

When formatting my time i get different results depending on the use of hh or HH.
Can someone tell me why this happens?
The problem occurs with the following code, returning a NULL on the 2nd result.
declare #TIMEPART time = getdate()
declare #datetime datetime2(7) = getdate()
select format(#timepart, 'hh\:mm'), format(#timepart, 'HH\:mm'), format(#datetime, 'hh\:mm'), format(#datetime, 'HH\:mm')
The HH:mm format is not a valid conversion for the time datatype and for the format(time, time format) function it is not the correct syntax. Because of this the format function returns a null. HH:mm is the conversion type for 24 hour clock for datetime datatypes but is not compatible with the time datatype as the time datatype is already in the 24 hour clock format(not sure why this is the case but I think it is because the time datatype can do more than just represent 24h clock time). The AM\PM format type hh:mm is only valid with the datetime datatypes and for time it will just return the 24 hour time since this is all a time datatype can return. You would have to convert the time to a datetime if the input value datatype is time in order to display the AM/PM format as I have shown below.
declare #TIMEPART time = '21:20:20.0570000'
declare #datetime datetime2(7) = '21:20:20.0570000'
select format(#timepart, 'hh\:mm'), format(convert(datetime,#timepart), 'hh:mm'), format(#datetime, 'hh\:mm'), format(#datetime, 'HH\:mm')
If you still want the data type of the second column to be time then you would have to convert back to time
cast(format(convert(datetime,#timepart), 'hh:mm') as time)
However in this example the time would be 9:20 AM and not 9:20 PM since time is only stored as a 0 to 23 value for the hour. This is a good example of why is is advisable to always try to return any time value in the 0-23 hour format. If you do need to display the time in the 12 hour format you are better off converting it to a varchar.
convert(varchar(15), format(convert(datetime,#timepart), 'hh:mm'))
One other thing to note is that for datetimes the \ in the format is not needed, for example
format(#datetime, 'hh\:mm')
and
format(#datetime, 'hh:mm')
will return the same value. However the \ is needed for time datatype functions.
format(#timepart, 'hh\:mm')
will return a hh:mm time but
format(#timepart, 'hh:mm')
will return null

how to convert a timestamp to int in sql (vertica)

I have a timestamp as 2017-07-19 11:45:01and i want it to convert to int.
Query:
select cast(max(event_timestamp) as INT) from error_messages where error_level='ERROR' and user_name='git'
Error:
SQL Error [2366] [42846]: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
[Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
com.vertica.util.ServerException: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
You have to use TIMESTAMPDIFF() this way:
SELECT TIMESTAMPDIFF(SECOND,'001-01-01 00:00:00', '2015-02-23 03:12:35');
timestampdiff
---------------
63560257955
to get the number of time units you want (SECONDs here above) since the timestamp you want...
If you want to get Unix Timestamp of that date as int than search fort that.
One option would be to calculate the range from your date to '1970-01-01' in seconds as int. This is the Unix Timestamp.
Use JULIAN_DAY function in Vertica to convert the time stamp to a integer value or number.
For more details refer Vertica documentation link: https://my.vertica.com/docs/6.1.x/HTML/index.htm#16070.htm
To extract number from date time with 1 second interval.
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');

SQL Server Convert ISO 8601 not working as documented

Per MSDN convert should properly parse ISO 8601 dates with timezone using 127 as the style parameter.
The optional time zone indicator, Z, is used to make it easier to map XML datetime values that have time zone information to SQL Server datetime values that have no time zone. Z is the indicator for time zone UTC-0. Other time zones are indicated with HH:MM offset in the + or - direction. For example: 2006-12-12T23:45:12-08:00.
All of the following are valid ISO 8601 dates but return Conversion failed when converting date and/or time from character string.
select convert(datetime, N'2014-02-07T13:51:00+07:00', 127)
select convert(datetime, N'2014-02-07T13:51:00+07', 127)
select convert(datetime, N'2006-12-12T23:45:12-08:00', 127)
Anyone have a solution or workaround for this issue?
Workaround?: Use datetimeoffset:
select convert(datetimeoffset, N'2014-02-07T13:51:00+07:00', 127) --<-- This one works...
select convert(datetimeoffset, N'2014-02-07T13:51:00+07', 127)
select convert(datetimeoffset, N'2006-12-12T23:45:12-08:00') --<-- and this one works...
use datetimeoffset or datetime2 instead of datetime

How can I convert a Sql Server 2008 DateTimeOffset to a DateTime

I'm hoping to convert a table which has a DATETIMEOFFSET field, down to a DATETIME field BUT recalculates the time by taking notice of the offset. This, in effect, converts the value to UTC.
eg.
CreatedOn: 2008-12-19 17:30:09.0000000 +11:00
that will get converted to
CreatedOn: 2008-12-19 06:30:09.0000000
or
CreatedOn: 2008-12-19 06:30:09.0000000 + 00:00 -- that's a `DATETIMEOFFSET`, but `UTC`.
Cheers :)
Converting using almost any style will cause the datetime2 value to be converted to UTC.
Also, conversion from datetime2 to datetimeoffset simply sets the offset at +00:00, per the below, so it is a quick way to convert from Datetimeoffset(offset!=0) to Datetimeoffset(+00:00)
declare #createdon datetimeoffset
set #createdon = '2008-12-19 17:30:09.1234567 +11:00'
select CONVERT(datetime2, #createdon, 1)
--Output: 2008-12-19 06:30:09.12
select convert(datetimeoffset,CONVERT(datetime2, #createdon, 1))
--Output: 2008-12-19 06:30:09.1234567 +00:00
I'd use the built in SQL option:
select SWITCHOFFSET(cast('2008-12-19 17:30:09.0000000 +11:00' as datetimeoffset),'+00:00')
I know this is an old question but, if you want to convert DateTimeOffset to a DateTime, I think you need to take into account the timezone of the server you are converting on. If you just do a CONVERT(datetime, #MyDate, 1) you will simply lose the time zone, which likely results in an incorrect conversion.
I think you first need to switch the offset of the DateTimeOffset value, then do the conversion.
DECLARE #MyDate DATETIMEOFFSET = '2013-11-21 00:00:00.0000000 -00:00';
SELECT CONVERT(DATETIME, SWITCHOFFSET(#MyDate, DATEPART(tz,SYSDATETIMEOFFSET())));
The result of converting '2013-11-21 00:00:00.0000000 -00:00' to a DateTime on a server who's offset is -7:00 will be 2013-11-20 17:00:00.000. With the above logic it doesn't mater what the time zone of the server or the offset of the DateTime value, it will be converted to DateTime in the servers time zone.
I believe you need to do this because a DateTime value includes an assumption that the value is in the time zone of the server.
DateTimeoffset (Timezone) conversion in SQL Server.
SQL Server 2016 (13.x) and later
Exmample
Select GETUTCDATE()
Select Convert(DATETIME, GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Central European Standard Time')
Select Convert(DATETIME, GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'India Standard Time')
Result will be
2020-08-18 08:22:21.640
2020-08-18 10:22:21.640
2020-08-18 13:52:21.640
Note: The timezone information is discarded in conversion if no style ("126" here) is specified. It might also be discarded in some of the other styles, I don't know -- in any case the following correctly adjusts for the TZ information. See CAST and CONVERT.
select convert(datetime, cast('2008-12-19 17:30:09.0000000 +11:00' as datetimeoffset), 126) as utc;
Happy SQL'ing.
Edit
Not sure if it matters but ... datetime Can't actually store that level of precision/accuracy. If the above is run the fractional seconds will be truncated to 3 digits (and accuracy is less than that). The same-same with datetime2 (and datetimeoffset(7)) produces a non-truncated value:
select convert(datetime2, cast('2008-12-19 17:30:09.1234567 +11:00' as datetimeoffset(7)), 126) as utc;
In order to account for daylight savings time, I used the following:
CONVERT(
DateTime,
SWITCHOFFSET(
CONVERT(
DateTimeOffset,
CONVERT(
DateTime,
[time_stamp_end_of_interval],
120
)
),
DATENAME(
TzOffset,
CONVERT(
DateTime,
[time_stamp_end_of_interval],
120
) AT TIME ZONE 'Pacific Standard Time'
)
)
)
AS GOOD_PST
Note: time_stamp_end_of_interval is a varchar