I already asked a similar question but I didn't get the answer I want. (It's my mistake!)
I have a website with a SQLServer 2008R2 database located in one country (US for example).
Many clients from different countries access to this website. All datetime datatypes are datetimeoffset. The datetime are saved using sql server datetime in US.
What should I do to convert the dates according to the client datetime zone and do I need to save the time zone different in a table to be able to convert the datetime from the database to the client?
I would store the data as UTC (and in fact I mandate that our servers are all set to UTC time with no DST adjustments - not only does this prevent gaps and overlaps twice a year, but it also makes job scheduling etc. much more predictable and reliable).
You can easily convert UTC data to any other time zone. You can use a calendar table to get the offset correctly and to account for things like DST. See how to convert all datetime columns in a sql server 2005 express database with data to UTC, Where to set a UTC datetime value in n-tier application: Presentation Layer, Domain, or Database?, How do I handle the timezones for every Chat message and http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html
Important: DATETIMEOFFSET is not DST-aware!
Related
I am working on a new application which already having data. The dates in database are stored in local timezone. I set webAPI formatters as DateTimeZoneHandling.Local.
The issue is, users can be in different time-zones. when they receive the dates, there are in the server's timezone.
On the client, whatever changes they do in dates, the Angular transformers change the dates to UTC before sending that to server. This time, server changes that back to it's local time and saves back to the database.
This time, the date stored is not what user's actually selected but the local date of the server and is causing inconsistency in the data.
I want this conversion to preserve the datetime at both the ends. Any suggestion?
Isn't there a way to configure Sql Server Management Studio to, in query results, display the value of a DateTimeOffset column in the client's local timezone, instead of UTC?
Example: I'm in timezone +02:00. A value is stored as 2016-07-27 22:00:00.0000000 +00:00. Currently, this value is displayed as such in query results. I want it to be displayed (formatted) as 2016-07-28 00:00:00.0000000 +02:00 when executing this query on SSMS on my machine.
Currently I'm manually using something like SWITCHOFFSET(CONVERT(datetimeoffset, <DateColumn>), DATENAME(TzOffset, SYSDATETIMEOFFSET())) which is obviously very cumbersome.
If I'm not mistaken, in Oracle the NLS parameters on session level could be used for this. Is there something similar in Sql Server?
Neither SQL Server nor SSMS have any ability to work with the concept of a "session time zone" (like Oracle, MySql, etc. do).
Also, it wouldn't make sense to automatically convert the offset anyway, as the offset is actually part of the stored datetimeoffset value.
If you're using SQL Server 2016, or Azure SQL Database, you can use the new AT TIME ZONE statement, as in:
SELECT yourdto AT TIME ZONE #thetimezoneid
Otherwise, consider using my SQL Server Time Zone Support project.
Neither of which can give you the system's time zone ID. You must know which time zone you want to work with.
We have just put an application into production which is hosted on AWS using their SQL Server RDS.
We have discovered that the database is set to UTC time, where our local development database isn't.
For example. I'm trying to insert a date of 2016-06-27 08:00. This works fine on our local server, but on AWS, the date is set to 2016-06-26 22:00
I'm trying to replicate the problem on our local server, and I changed the server's timezone to UTC, but the date gets inserted as 2016-06-27 08:00, but I should now expect it to be inserted as 2016-06-26 22:00.
Is there some SQL Server setting I need to change to have a date automatically converted to insert UTC?
The Sql Server DateTime datatype doesn't know anything about timezone. The time inserted is determined by the application posting the time. So this issue is going to be in your application, not in the database.
My recommendation would be to either:
Store all DateTime's as UTC converting them in the application before saving
Use the DateTimeOffset type which stores the timezone information as well as the time
We recently moved our database from SQL Server 2005 to 2014. But now we are having an issue with how the server converts datetime from varchar.
Previously, a date string 2017-06-30 was converted correctly as July 30, but now even with the user and the database with the language set to Spanish, it still converts the date format as yyyy-dd-MM.
Is there some way to permanently change the configuration to fit the original one? One work around we found was to add SET LANGUAGE ymd at the beginning, but the amount of procedures doesn't make this feasible in a timely manner.
I am developing a VB.Net application that has operations which rely heavily on dates & times. As there could be conflicts on date formats if the application date format doesn't match the server date format what is the best practice to resolve this issue. I know that SQL Server datetime format depend on the server language & VB.Net will use the local machine datetime format. Which means if a user changes the datetime format it will cause problems when inserting.
My idea is to use SELECT GETDATE() on application startup, then identify its datetime format using VB.Net & whenever I try to insert a datetime value I will convert it to the datetime format identified at the application startup.
Is there a better approach to having a consistent datetime formats between the application & SQL Server to avoid comflicts. eg: Mixing a day with a month.
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.
The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.
There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.
So if ever possible, don't convert dates between DateTime and string all the time! Leave it as DateTime in .NET, use DATE or DATETIME2(n) in SQL Server, and let the dates be in their native format. Use parametrized queries that support the native DateTime datatype so you don't need to convert dates to string and back!
If you must use strings to represent dates for whatever reason, the only reliable way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.
The ISO-8601 format is supported by SQL Server comes in two flavors:
YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!
or:
YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.
Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.
The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible