0

If I do a select GetDate() today (October 9, 2017) against a SQL Server 2008 instance the function will return 10/10/2017 i.e. tomorrow's date. However a select convert(date, getDate()) will return 9/10/2017. I don't understand why.

Possibly related: the Microsoft documentation says

Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.

What exactly does without the database time zone offset mean? My SQL Server is currently in GMT+11.

GBC
  • 35
  • 6

2 Answers2

0

Try to look out base on given examples here

GetDate() function doesn't check to current time zone it gives yo the computer time.

Try to use CURRENT_TIMESTAMP might be help full

Best regards..

Emre
  • 83
  • 2
  • 10
0

Try using

SELECT CURRENT_TIMESTAMP;

OR

SELECT SYSDATETIME();

For system date and time.

Also, get UTC and local time using:

SELECT GETUTCDATE() As UTCDateTime

And

SELECT CONVERT( DATETIME, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()), DATENAME(TzOffset, SYSDATETIMEOFFSET()))) AS LocalDateTime
ViKiNG
  • 1,294
  • 2
  • 19
  • 26
  • I use the last one to convert UTC saved time in cloud-hosted databases to local time. Please suggest better. – ViKiNG Oct 09 '17 at 22:47
  • 1
    What you wrote is basically `SYSDATETIMEOFFSET()`. But if you're saying that instead of the inner `GETUTCDATE()` you'd use a database field, then you're incorrectly assuming that the *current* offset is the same for the one at the time of the value in that field. To convert correctly, you need either `AT TIME ZONE` (SQL 2016+) or for older SQL use my [SQL Server Time Zone Support](https://github.com/mj1856/SqlServerTimeZoneSupport) project. – Matt Johnson-Pint Oct 10 '17 at 00:01