SQL Azure – Convert UTC to your local time

Not unsurprisingly SQL Azure GetDate() stores datetime as a UTC value.

If your application is hosted in a specific country it is likely you will wish to return that value to the local user in the local time.
Here I host in the UK and want to display in the UK so my choice is simple enough.

The default CONVERT() statement in the SQL Azure library will do this for you.

An example of the statement with parameters is here (slightly confusingly here I have the field name as a reserved word but just ensure that your field name is in the square brackets)

CONVERT(datetime, 
SWITCHOFFSET([datetime], 
DATEPART(TZOFFSET, 
[datetime] AT TIME ZONE 'GMT Standard Time'))) 
as UKTime

And here is that statement included in a full query.

And here is an SQL statement to determine all time zones currently observing daylight saving on your server.
This query can also be used to identify what parameter you should have for your convert statement.

SELECT
  name,
  current_utc_offset
FROM sys.time_zone_info
WHERE is_currently_dst = 1;