Forum Discussion

ajupulickal's avatar
ajupulickal
Copper Contributor
May 10, 2022

SQL Date Formatting

Hi all,

 

I have a requirement to do an OpenQuery to an Oracle database and the accepted time format in Oracle is "2022-05-01T06:00:00Z". I am wondering how do we convert a normal datetime in SQL server to this format .?

 

select CONVERT(varchar,GETDATE(),127) this gives - >2022-05-10T06:26:21.740 but not in the above format. Can someone help me to convert this .?

 

Aju

2 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    ajupulickal 

     

    Hey, Aju.

     

    As you've already noted, there's no pre-canned identifier that will produce that exact format. Rather, you have to do it yourself using FORMAT.

     

    Here's an example - noting that you should probably also give preference to using GETUTCDATE() over GETDATE():

     

    SELECT FORMAT(GETUTCDATE(), 'yyyy-MM-ddTHH:mm:ssZ')

     

    Cheers,

    Lain

Resources