I'm doing a few SQL select queries and would like to convert my UTC datetime column into local time to be displayed as local time in my query results. Note, I am NOT looking to do this conversion via code but rather when I am doing manual and random SQL queries against my databases.
Sql – Convert Datetime column from UTC to local time in select statement
sqlsql server
Related Topic
- Sql – Exclude a column using SELECT * [except columnA] FROM tableA
- Sql – Select n random rows from SQL Server table
- Sql – How to UPDATE from a SELECT in SQL Server
- Sql-server – SELECT INTO a table variable in T-SQL
- Sql – Storing DateTime (UTC) vs. storing DateTimeOffset
- Mysql – ‘IF’ in ‘SELECT’ statement – choose output value based on column values
- Sql – Convert INT to VARCHAR SQL
Best Answer
You can do this as follows on SQL Server 2008 or greater:
You can also do the less verbose:
Whatever you do, do not use
-
to subtract dates, because the operation is not atomic, and you will on occasion get indeterminate results due to race conditions between the system datetime and the local datetime being checked at different times (i.e., non-atomically).Please note that this answer does not take DST into account. If you want to include a DST adjustment, please also see the following SO question:
How to create Daylight Savings time Start and End function in SQL Server