3

I am connected to SQLServer on network which is in other location. But I am executing the GetDate function in same server but in my system which is in different location from SQL server location.

All I need is to get the current system date even if i execute the GetDate function in network SQL server.

Beri
  • 11,470
  • 4
  • 35
  • 57
techV
  • 935
  • 3
  • 23
  • 41
  • So, current date of local machine? – shree.pat18 Feb 06 '15 at 08:51
  • @shree yes exactly. – techV Feb 06 '15 at 08:52
  • 1
    possible duplicate of [Getdate() function to get date for my timezone](http://stackoverflow.com/questions/20086189/getdate-function-to-get-date-for-my-timezone) or possibly a duplicate of [this question](http://stackoverflow.com/questions/12173408/store-current-datetime-according-to-client-side-timezone-in-sqlserver) Either way the Server will have no knowledge of the client so this isn't possible. – codingbadger Feb 06 '15 at 08:54
  • if server will have no knowledge of my location then is there no way to get local machine date ? – techV Feb 06 '15 at 09:00
  • 1
    SQL can't get the client application/connection time. You should perform that check client side. – simon_dmorias Feb 06 '15 at 09:09
  • If you're in a different time zone then just add or subtract the hours from GETDATE() accordingly – Christian Barron Feb 06 '15 at 09:14
  • you can also use GETUTCDATE() if you do not know the server timezone. – Nicolas M Feb 06 '15 at 09:14

4 Answers4

0

GetDate will return the local date/time for the machine where the database is - assuming that the server time is set to local time. GetUtcDate will return the UTC time (GMT). The server has no way of knowing what the time is at the calling system. If you need to record that at the SQL Server end, you will have to pass the local time to the database.

Rikalous
  • 4,514
  • 1
  • 40
  • 52
0

Probably too late but I, being in Indian Time Zone (GMT +05:30) resolved this as following:

DATEADD(HOUR, 5, DATEADD(MINUTE, 30, GETUTCDATE()))

GETUTCDATE() always gives you GMT time. Adding hours and minutes of your timezone would help. Hope this helps.

Chaos Legion
  • 2,730
  • 1
  • 15
  • 14
0

I'm in Eastern' Standard Time, so

SELECT GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' as Local_DateTime

The above query gets the local date time. The view sys.time_zone_info may be useful in getting the exact name of your time zone for this purpose.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
-1
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
Immu
  • 746
  • 5
  • 10
  • This is server date/time - the OP wants the client date/time. – Rhys Jones Feb 06 '15 at 09:17
  • `SYSDATETIMEOFFSET()` -- this is server datetime...... `SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')` -- client datetime. provided he should 'change UTC time accordingly'.. **Please check it will work** – Immu Feb 06 '15 at 09:40