How to convert a DateTime to an ISO String (UTC) in T-SQL

Jump to: navigation, search

declare @myDT DateTime set @myDT = GETUTCDATE() declare @myDTStr varchar(30) set @myDTStr = cast(DATEPART(year, @myDT) as varchar(4)) + '-' + right(N'0' + cast(DATEPART(month, @myDT) as varchar(2)), 2) + '-' + right(N'0' + cast(DATEPART(day, @myDT) as varchar(2)),2) + 'T' + right(N'0' + cast(DATEPART(hour, @myDT) as varchar(2)),2) + ':' + right(N'0' + cast(DATEPART(minute, @myDT) as varchar(2)),2) + ':' + right(N'0' + cast(DATEPART(second, @myDT) as varchar(2)),2) + 'Z' print @myDTStr

See also

Use your Android as a Genie garage door opener remote

Personal tools
Namespaces
Variants
Views
Actions
Navigation