Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server date in the format yyyy-MM-ddThh:mm:ssZ

Tags:

I need to format a set of dates in SQL server to the following format..

yyyy-MM-ddThh:mm:ssZ

I cant seem to find how to format the date with the T and Z parts included in the string

Any ideas how to achieve this format in a SQL query?

like image 725
carrot_programmer_3 Avatar asked May 24 '11 10:05

carrot_programmer_3


People also ask

What is Z in timestamp SQL?

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.

How do I get UTC date in SQL?

SQL Server GETUTCDATE() Function The GETUTCDATE() function returns the current database system UTC date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.

What does Datetimeoffset mean in SQL?

The SQL Server DateTimeOffset data type stores the date & time along with the Time Zone Offset. It is similar to both DateTime & DateTime2 data types. Except that the DateTime & DateTime2 does not store the Time Zone Offset.

How do I get Currentdate in SQL?

MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.


1 Answers

According to the SQL Server 2005 books online page on Cast and Convert you use date format 127 - as per the example below

CONVERT(varchar(50), DateValueField, 127)

SQL Server 2000 documentation makes no reference to this format - perhaps it is only available from versions 2005 and up.

Note on the time zone added to the end (from note 7 in the docs): 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.

Thanks to Martin for this note: You should be able to use STUFF to remove the miliseconds as these will be in a fixed position from the left of the string. i.e.

SELECT STUFF(CONVERT(VARCHAR(50),GETDATE(), 127) ,20,4,'')
like image 188
Bork Blatt Avatar answered Oct 23 '22 21:10

Bork Blatt