I have a column in a table with time stamps from Germany (Utc + 6) in their local time (Utc -1). How do I convert all those datetimes in my local time?
-
What is your dbms?– HadiFeb 10, 2017 at 19:12
-
what version of sql?– Sash SinhaFeb 10, 2017 at 19:12
-
3This post shows no research or effort on your part. What have you looked up, tried to implement, and found to not work?– dfundakoFeb 10, 2017 at 19:13
-
sequel server 2014– Val KFeb 10, 2017 at 19:13
-
What is your local culture info?– HadiFeb 10, 2017 at 19:17
3 Answers
I think the correct way of converting UTC datetime into local datetime is using CLR function. You can find an example below.
https://www.mssqltips.com/sqlservertip/2339/converting-utc-to-local-time-with-sql-server-clr/
There are -7 hour between germany and central time zone in us
CT (-6)
Berlin (+1)
If your column is datetime USE THE FOLLOWING
Select DATEADD(HOUR,-7,[DATECOLUMN])
Update 1
Consider that @bdate and @edate are the begin and end date of the daylight savings so you can use this query
Select case when DATEADD(HOUR,-7,[DATECOLUMN]) between @bdate and @edate Then
DATEADD(HOUR,-8,[DATECOLUMN]) else
DATEADD(HOUR,-7,[DATECOLUMN]) end
-
It won't work because of day light savings in the US... when we change the clock I would have to adjust the code– Val KFeb 10, 2017 at 19:24
-
-
Germany doesn't have day light savings though. It is 8:30 pm in Germany now and 1:30 pm in the US. When the US changes its clock one hour backward, it will be 12:30 pm in the US and still 8:30 pm in Germany.– Val KFeb 10, 2017 at 19:32
-
@ValK That s mean that your destination date time has daylight savings not the timestamp in ur data. So you are the one that must set that option.– HadiFeb 10, 2017 at 20:42
-
@ValK: Germany does have daylight savings time (just not now in February) Feb 10, 2017 at 20:53
If your data is in a string format like "dd.mm.yy," then you would use the code page 104 to tell SQL how to parse your date from string to a DateTime format.
CONVERT(DateTime, DateField, 104)
If your string is in some other format, then look up the correct codepage in the table here (CAST and CONVERT in T-SQL):