Can I apply timezone changes to my UTC date in SqlServer? - sql

So in SQL server I have been storing my dates as UTC, I also have a localized timezone in the format of 'AUS Eastern Standard Time', for example.
Is it possible to calculate the correct datetime with just information in Sql Server?

You didn't provide you environment info so I am proposing this for sql server and windows server.
There is no simple method to do this. As far as I know SQL server does not have time zones it can access. However, Windows does have this information. You could access this in SQL with a SQL CLR user defined table valued function using the time zone info class from .net. Then you just have to add the time zone offset minutes from the CLR function to the UTC date.

Related

CURRENT_TIMESTAMP showing wrong timezone in SQL Azure database

I have an SQL database connected to an Azure server, the timezone is set to UTC and the location is set to UK south, when I run the CURRENT_TIMESTAMP command on the SQL database it returns a time at UTC-1:00. I assume this is an Azure based issue since when I run the same command on a locally hosted SQL server I get the correct timezone. Any advice on the matter would be greatly appreciated. All I can think is the location being set to UK south assumes I'm in some overseas UK territory hence giving the wrong timezone so my next step is to make a new database on azure to host it but change the location since you cant change it once it has been created.
This is the query on the azure server
This is the query on my local server. Both of these queries were made at 17:43 UTC
Here is the sample procedure to get CURRENT_TIMESTAMP in AZURE SQL DATABASE.
Creating MySql server with the UK south location according to the the timezone is set to UTC.
Currently, the default time zone on Azure SQL DB is UTC. Unfortunately, there is not possible to change by server configuration or database configuration.
Azure SQL Database does not support time zone settings; it always follows UTC.
Use AT TIME ZONE in SQL Database if you need to interpret date and time information in a non-UTC time zone.

Dateformat in .net Framework

In my application rarely I am getting an error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
I am aware this error is from SQL Server, that query I have formed from C# code.
Convert.ToDateTime(userDate).AddMonths(1)
Here both application server and database server date formats involved, hence I had doubt with date format difference between two servers.
Basically I want to know which date format will be considered for .net and SQL.
I am using .net framework 3.5 and SQL Server 2012.
Thanks in Advance.
Hari
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.
The 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're using 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

Default format DateTime SQL in Oracle, SQL Server and Firebird

I'm doing a manual INSERT where I have to format a DateTime value in C#. The resulting string is put into a a SQL query. I need a cross-platform format that can be hendled by Oracle, SQL Server and Firebird.
Example:
With yyyy-mm-dd you save the date correctly without relying on the language of the database.
I need the format not to uncouple from language but from vendor quirks.

TIME datatype in SQL Server 2008 will not accept time from Access form

I am using an Access form on the front end, bound to a SQL Server 2008 table. I have an Arrival Time column of datatype Time.
But I get an ODBC error every time I try to save a record. Error states:
Invalid character value for cast specification.
Time displays as 10:00:00 AM. I have tried with and without a time format in the properties of the field, and with/without an input mask of various types. Does anyone know how to avoid this error?
When I linked a 'Time' Field from SQL Server 2012 Express to an access front end (2010 accdb) using the old 'SQL Server' ODBC driver, it converted it to a Text field, and would not allow an updates. You might consider using a datetime field, or trying a newer ODBC driver. I believe the 'Time' field was first introduced in SQL 2008, so I'm guessing older ODBC drivers don't know how to handle it.

Why doesn't SQL Server have a pure date and pure time data type?

I have a few SQL Server tables with a datetime field that really only has to contain dates... and the fact that it's a datetime object violates data integrity, because it's possible for data to be inserted with a time - which isn't really valid for this field. The result is that we have to convert it and strip off the time every single time we use it. We've considered adding triggers to make sure that no data can even get in with a time attached... but it seems to me that this should really be part of the database software, and shouldn't need to be specifically programmed each time.
Why doesn't SQL Server have separate date and time data types? Is this going to change in the near future? Do other database platforms have this functionality?
SQL Server 2008 has DATE and TIME data types.
What version are you using?
SQL Server has a pure date and time data type since version 2008, they also added a bunch of new datetime types, now it also goes all the way back to year 1 instead of 1753