Server is in NY and date column (DateTime) is being filled as EST. How do users in California view this DateTime as the local PST?
I tried:
declare @dte DateTime = '20190919 10:01:01'
select @dte EST, @dte At Time Zone 'Pacific Standard Time' As PST
And I get
EST
2019-09-19 10:01:01.000
PST
2019-09-19 10:01:01.000 -07:00
I am looking for 2019-09-19 07:01:01.000 -07:00
Jeroen already said this in his comment, but I'm making the code concrete so you can see what he means.
Take your initial date, that you know the time zone for, but SQL Server doesn't yet, and tell it the time zone explicitly:
DECLARE @dte datetime = '20190919 10:01:01';
SELECT
@dte AT TIME ZONE 'Eastern Standard Time';
That returns a DATETIMEOFFSET
value:
2019-09-19 10:01:01.000 -04:00
Next, convert that value to Pacific time by wrapping it in another AT TIME ZONE
:
DECLARE @dte datetime = '20190919 10:01:01';
SELECT
(@dte AT TIME ZONE 'Eastern Standard Time') AT TIME ZONE 'Pacific Standard Time' AS datetime;
That returns another DATETIMEOFFSET
:
2019-09-19 07:01:01.000 -07:00
Last but not least, for your presentation purposes, wrap that in a CAST
so that you only get a DATETIME
for your end users:
DECLARE @dte datetime = '20190919 10:01:01';
SELECT
CAST((@dte AT TIME ZONE 'Eastern Standard Time') AT TIME ZONE 'Pacific Standard Time' AS datetime);
Results:
2019-09-19 07:01:01.000
And Bob's your uncle.
Of course, if you want to take the lazy approach, DATEADD
will get you where you want to be, too:
DECLARE @dte datetime = '20190919 10:01:01';
SELECT
DATEADD(HOUR, -3, @dte);
2019-09-19 07:01:01.000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.