Convert datetime format to ISO8601 format in sqlserver 2008 - sql-server

I'm having a table IMAGEDATA in which the column VALUE stores datetime format in en-US (for ex. 06/08/2012 02:10:36 p.m.). I need to convert the datetime format to ISO8601 format (ex. 2012-08-22T13:10:39) for all the rows that are currently in the table. I'm novice in these topics. Could you please me some better approach it will be more helpful.Thanks.

Internally Datetime values are stored as 2 integers . They are not stored in the specific format in which they are inserted .In order to convert to 8601 format try the below code
Declare #date datetime
set #date = '06/08/2012 02:10:36 '
Select convert(varchar(30),#date,126)

Related

Convert Varchar date format from yyyy-MM-dd to yyyyMMdd in SQL

Hi I am trying to convert Varchar date format from yyyy-MM-dd to yyyyMMdd in SQL.I tried the below approaches but nothing is working.
Declare #doj VARCHAR(10)
Set #doj='2022-01-01'
Select convert (VARCHAR,#doj,112)
select format(#doj,'yyyyMMdd')
SQL engine is not converting to the required format.If I declared doj variable to date then it is working as expected.How to make it work if the doj is varchar?
You can just convert to datetime and back again using explicit style numbers:
Declare #doj VARCHAR(10) = '2022-01-01';
SELECT CONVERT(varchar(10), CONVERT(datetime, #doj, 126), 112);
db<>fiddle
Quite why you are storing dates in varchar in the first place is another question...

Converting a varchar date to date datatype in mssql

My column is storing value as 201909(i.e September 2019), I need to convert it to format 2019-09-01 and store in it another column in a MSSQL .Please help
You can add a statinc '01' string with your current string and then try to convert as below-
DECLARE #D VARCHAR(20)
SET #D = '201909'
SELECT CAST(#D+'01' AS DATE)
Output is-
2019-09-01

QUOTENAME alters date format

I've got a datetime field in my table which shows dates like this:
2017-01-18 10:20:19.617
I'm using QUOTENAME to output the table with the fields wrapped in quotes so it will be compatible to import into a legacy CSV based app.
SELECT QUOTENAME(P.CreatedOn,'"') as CreatedOn
That changes the output to
"Jan 18 2017 10:20AM"
Ideally I need
"2017-01-18 10:20:19.617"
Is that possible?
This should do the trick. The CONVERT function allows you to explicitly control the format of your date conversion.
declare #d datetime = '20170119 20:21:22'
select QUOTENAME(CONVERT(varchar(23), #d, 121), '"')
Some guidance notes:
The style parameter 121 indicates the date/time must be formatted in the ODBC format which is yyyy-mm-dd HH:MM:ss[.ttt[tttt]][{+|-}offset]. It always includes the fractions of a second at 3 or 7 digits depending on the date type of the value to be converted.
This supports all datatypes combining date & time: datetime, datetime2 (7 digit fraction of a second), smalldatetime, datetimeoffset
date and time datatypes exclude time a date portions from the string respectively.
Refer to the linked page to experiment with other conversion styles. E.g. style 120 is similar, but excludes the optional fractions of a second from the format.
Note that if the the varchar(...) target type is to short to hold the full string, it will be truncated from the right.
QUOTENAME returns nvarchar (258). When you are supplying DATE to the function it does conversion to nvarchar.
If you convert to varchar or nvarchar first (as #CraigYoung suggesting), then this function will not do conversion itself and will not change the string.
In CONVERT you can specify any format you want for your date to be in using list available formats.

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

SQL Server Varchar to Datetime conversion

I have a varchar representing a datetime in this form :
dd-MM-yyyy HH:MI:SS AM
How can I convert it to datetime type ?
I have checked the list of standards of datetime here But I see no corresponding one to my format above.
Cast it directly
select CAST('10-02-2014 05:10:22 AM ' AS DATETIME)
Try this too..
select CONVERT(varchar(15),GETDATE(),105)+' '+CONVERT(varchar(30),CONVERT(VARCHAR,GETDATE(),108))+' '+SUBSTRING(CONVERT(varchar(30),CONVERT(VARCHAR,GETDATE(),100)),18,2)

Resources