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.
Related
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 have a MS Access 2010 front end / SQL Server 2012 back end database, with a number of date fields in different tables. Sometimes I need to store the time, so I have used data types datetime or smalldatetime. However certain fields only need to store the date, so I used data type Date.
My problem is that in MS Access, my ODBC-linked table shows the Date data type fields as Text. This is then leading to problems with some dates being stored in the yyyy-dd-mm format and others in the yyyy-mm-dd format.
Is this a bug? Do I need to use smalldatetime?
Thanks for any assistance,
Jim
Had the same issue myself linking Access 2007 and SQL Server 2008.
See this question, if you look at Albert D. Kallal's comment to the first answer, it tells us that the problem is with an outdated driver connecting the front end to the back end.
If you aren't able to choose an up-to-date driver (and bear in mind that even if you can, other users of your database on different client machines may not be able to) the workaround is to use datetime data type in every case.
Just to expound on the comment given by #BiigJiim I actually had the Native client 11.0 driver already installed but as I was creating DSN-Less table connections I had to change my connection string formally to: Driver={SQL Server Native Client 11.0};
Also as an additional note, I do not believe the Date and DateTime2 data types are recommended for Microsoft Access integration. If memory serves me correctly it recommends either DateTime and SmallDateTime. I get not needing the Time in a lot of circumstances, but you can easily format it via the front end... especially within Access.
MSDN lists "TIME" as an available Data Type. This is good, since I need time variables and the ability to perform functions on them (in the form of 00:00:00). Datetime is not an option because my scheduling database cares about Monday - Sunday, not 11/13/14, etc... When i go to create a column with data type time, it doesn't exist. I also tried doing it via query:
ALTER TABLE dbo.DataSchedule
ALTER COLUMN StartClock TIME
And get the message:
Msg 2715, Level 16, State 6, Line 1
Column, parameter, or variable #6: Cannot find data type TIME.
How can I resolve this issue? Is this a problem with 2014 express as opposed to the standard edition?
Thanks for reading.
Per SQL Server documentation TIME datatype is available from SQL Server version 2008 and above. I am pretty much sure that, you connected to a SQL Server 2005 instance and tried using the TIME datatype.
Also, it doesn't matter which SSMS version you are using to connect to since that's just the client. Make sure that, you are actually connected to a server version 2008 and above.
I am writing a small utility to copy a database from a proprietary ODBC database into a SQL Server database. Everything is working great, except when I use SQLBulkCopy to copy over the data. It works in most cases, but not when it's trying to copy a TIME field into a SQL DATETIME. I get this error:
The given value of type TimeSpan from the data source cannot be converted to type datetime of the specified target column.
When I create the schema in SQL Server I have to make the DATE and TIME fields DATETIME types in SQL Server, so there is no way around this. I wanted to use SQLBulkCopy so I didn't have to manually read through every record in every table and wrap logic around the special cases. Before I go down that road, is there another way I can do this? I have no control at all on the source database schema or content.
I assume you are dealing with pre-SQL Server 2008. SQL Server 2008 has DATE and TIME data types.
I think you would have to use a DataTable which matched the SQL Server schema and load this from your source reader, appropriately changing any TIME to a DATETIME by adding date information (e.g. 1/1/1900). Then use WriteToServer(DataTable). You might want to do it in batches, since you may use a bunch of memory reading it all into a DataTable.
Any particular reason you can't use SSIS?