How to convert datetimeoffset(7) to datetime in SQL Server.? - sql-server

I want to convert datetimeoffset(7) to datetime in SQL Server.
For example: my datetimeoffset(7) is: 2014-11-07 00:00:00.0000000 +05:30
I want convert to Datetime like this: 20141107 (Style 112) without using varchar.

i want convert to Datetime like this: 20141107 (Style 112) without
using varchar.
Datetime and datetimeoffset data types are stored in SQL Server in binary format. Data representations like '2014-11-07 00:00:00.0000000 +05:30' and '20141107' are in fact strings, so in T-SQL, you must convert to varchar in order to format the data as desired for display purposes so that the client application renders the data as the returned formatted string.
It is generally best to format data for display purposes in the presentation layer instead of T-SQL. Client applications typically have more robust formatting capabilities.

Related

Export sql convert datetime to yyyy-mm-ddTHH:MM:SS

I'm using SQL Server 2014.
I am required to export data via SQL. One of the fields needs to look like the below: (Not sure what the 'T' is). The source field is in datetime format.
2019-08-15T08:30:00
Is there a specific datatype for this, or will I have to convert to varchar etc and concat the 'T'? As it stands my SQL is pretty simple:
SELECT [Jobf_DteLog] as DateLogged,
FROM [tblJobs]
2019-08-15T08:30:00 is the ISO 8601 date format.
To get the desired output you can use ether FORMAT() or CONVERT() function.
select format(Jobf_DteLog, 'yyy-MM-ddThh:mm:ss')
, convert(varchar(30), Jobf_DteLog, 126)
Demo

Converting string to datetimeoffset in SQL Server and Postgresql

In SQL server I have a column that stores dates of this format:
2017-06-22T00:43:15+0300
as strings.
What I'm trying to do is cast these strings into actual datatimeoffset data in order to handle them as dates.
So far I have found multiple function for converting from datetimeoffset to other timestamp formats but none of these refer to strings.
https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetimeoffset-transact-sql
Exactly the same problem arises on a postgresql database. The best I have managed to achieve so far is just truncate the timezone and convert the rest of the string into timestamp. Ideally, I would like not to use such a lossy transformation.
Time Zone Offset in Sql Server is represented like this
[+|-] hh:mm:
In your data semicolon is missing in the offset part
DECLARE #stringtime VARCHAR(100)= '2017-06-22T00:43:15+0300'
SET #stringtime = Reverse(Stuff(Reverse(#stringtime), 3, 0, ':'))
SELECT Cast(#stringtime AS DATETIMEOFFSET(4))
Result : 2017-06-22 00:43:15.0000 +03:00
Postgres :
to_timestamp('2017-06-22T00:43:15+0300', 'YYYY-MM-DD HH24:MI:SS')
you can try using that postgres function.

How can I convert a JSON date with timezone to a SQL Server datetime?

I saved the output of a JSON object in a CSV file. I want to import the data into SQL Server. I have tried to cast the JSON date column to a SQL Server datetime data type.
select cast('2009-06-18T16:44:20+0000' as datetime)
This causes an error to be raised: Conversion failed when converting date and/or time from character string.
How can I convert a JSON date with timezone to a SQL Server datetime?
As far as I know, SQL Server recognizes timezone with colon. You have to reformat timezone part as follows:
SELECT CONVERT(datetime2, STUFF('2009-06-18T16:44:20+0000', 23, 0, ':'))
According to MSDN ISO 8601 has YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm] format.
I had a similar issue; I was getting the same error though the date format for my dates are a little different. The solution for me was to convert it to a DATETIME2 value, instead of just DATETIME. Had I not seen Pawel's answer, I would not have tried converting to DATETIME2.
The dates that I was getting were from a C# object that was serialized to JSON using JSON.Net. Below is a sample SQL script showing what the datetime values were and how to convert it.
DECLARE #effectiveDateJSON VARCHAR(MAX) =
'{"EffectiveDate":"2017-02-07T00:00:00-06:00", "CreateDate":"2017-02-07T16:32:12.9130892-06:00"}';
SELECT *
FROM OPENJSON(#effectiveDateJSON)
WITH (EffectiveDate DATETIME2 '$.EffectiveDate',
CreateDate DATETIME2 '$.CreateDate'
);
For a timezone aware conversion
declare #offset datetimeoffset = JSON_VALUE('{"d": "2021-01-01T00:00:00+03:00"}', '$.d')
declare #inCurrentTimezone datetime = #offset AT TIME ZONE CURRENT_TIMEZONE_ID()
select #inCurrentTimezone
Docs:AT TIME ZONE, datetimeoffset

Timestamp confusion in SQL Server

I have a TimeStamp (varchar(50),null) column in my SQL Server 2008 table which looks misleading by the name TimeStamp. I mean it appears as if it's a datatype timestamp but it's varchar.
But it has values like 201403240004 which looks like a date. Can I convert it into date and use?
Read online that timestamp is only a sequence of numbers and has nothing to do with date and time.
You can.
Providing that the format is YYYYMMDDHHmm, a simple way to do that would be:
SELECT CONVERT(DATETIME,
SUBSTRING([TimeStamp],1,4)+'-'+SUBSTRING([TimeStamp],5,2)+'-'
+SUBSTRING([TimeStamp],7,2)+' '+SUBSTRING([TimeStamp],9,2)+':'
+SUBSTRING([TimeStamp],11,2)+':00.000')
FROM Table
This will take this "timestamp" and first transform it to SQL-readable datetime string, i.e. for your example it would be 2014-03-24 00:04:00.000. Then, it will be easily converted to datetime.
Yes, your column should be convertible to DATETIME, but you may have to do the converison yourself if CONVERT() does support the format.
I can't tell from you example what the time format really is.
If it is YYYYMMDDHHMM them
SELECT CONVERT(DATETIME,LEFT('201403240004',8),112)
+CONVERT(DATETIME,SUBSTRING('201403240004',9,2)+ ':' + RIGHT('201403240004',2)+':00' ,108)

Database and DataTable DateTime format confusion

I want to store a DateTime.Now from my WPF C# application to a DateTime column in SQL CE Database.
I am inserting DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss") then I get the following error:
String was not recognized as a valid DateTime.Couldn't store <2011-54-06 18:54:47> in DateTime Column. Expected type is DateTime.
How to store DateTime in a specific format to SQL CE?
[EDIT]
Correction made to: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
However, after I insert and populate the data to a DataGrid. It shows 2011-54-06 18:54:47 in stead of yyyy-MM-dd HH:mm:ss format. I specify the format to be insert to database but it seem SQL CE has its own way of storing. Why?
You just change the yyyy-mm-dd HH:mm:ss in to yyyy-MM-dd HH:mm:ss. Obviously it will show like that.Because, you are not inserting the Month but a minute. So, the Datetime is not converted from string format due to wrong format.

Resources