SQL Server Timezone Change - sql-server

I have 2 databases on the same SQL Server. Is it possible to have one in PST and the other in EST?

No, The date/time is derived from the operating system of the computer on which the instance of SQL Server is running.
You could however have a custom UDF that you would call instead of getdate() and then do the timezone change in that UDF. You can also assign default values to columns with something like this
CREATE TABLE Test (Val DATETIME DEFAULT dateadd(hh,-3,GETDATE()))
Now when you do an insert it will use the default
INSERT test DEFAULT VALUES
SELECT * FROM test
....this of course won't work on updates and also someone could update that value
If you want to use GMT then use GETUTCDATE
SELECT GETUTCDATE()

This does not account for daylight savings time. The only way this can be accomplished is by having a table for the time zones as this example.
https://github.com/mattjohnsonpint/SqlServerTimeZoneSupport
In newer SQL versions it's a lot easier but in older versions (2014 or older) there is no straightforward way to accomplish this.

Related

MS Access Date() syntax in query to SQL Server

I have found several posts on using the GETDATE() function for SQL Server linked table while in an Access front-end VBA procedure. Those posts are focused on the WHERE clause of the query, but I have been unable to find corresponding information on use of GETDATE() for column assignment.
For example, I understand that in the WHERE clause, I would use something like this:
WHERE MyDate = CAST(GETDATE() AS DATE)
However, I am getting syntax errors in VBA when I try to assign the current date to a column, like this:
INSERT INTO MyTable ( SomeValue, TheDate ) SELECT 'Widget' AS Expr1, CAST(GETDATE() AS DATE) AS Expr2;
In this example, TheDate is defined as DateTime in SQL Server. Written like this, VBA reports "Syntax error (missing operator) in query expression 'CAST(GETDATE() AS DATE)'. I tried to surround the expression with Access-friendly # date delimiters, but no luck there.
After spending about 30 minutes searching stackexchange.com various ways for MS Access Date() in SQL, I have been unable to find this. However it is so simple I am sure it was already answered somewhere.
In MS Access you likely (not 100% sure for linked SQL, you have to experiment) should use Now() and Date() functions. First one is equivalent to getdate() in SQL, the second one returns current date without time.
If you run this in Access on a linked table (not a PT query), it should read:
INSERT INTO MyTable ( SomeValue, TheDate )
VALUES ('Widget', Date());
There seems to be some confusing here. If you building a Access query, then ZERO ZERO of the SQL server date functions and syntax matter. Your SQL MUST continue to be written to Access standards unless you using a pass-though query.
However, I seen this 100x times here.
What is the data type on sql server side?
Is it datetime, or datetime2?
And double, triple, qudadropes, (and more) check the linked table in desing mode.
If you link to SQL server using the standard legacy "SQL Server" driver. The one that been shipped for 20 years since windows 98SE?
You MUST check if Access is seeing those columns as text, or as date columns (which in Access always allow a time part if you want).
Access code, queries, forms and EVERYTHING should require ZERO changes if you migrate that data from Access to SQL server and link the table. Again: ZERO ZERO changes.
However, if you used datetime2 on the SQL server side? Then you CAN NOT use the legacy "SQL server driver" when linking table. The reason is they don't support the newer datetime2 format. As a result, Access will actually see, use, and process that column as a text column. You REALLY, but REALLY do not want that to occur.
Why?
Becuase then you spend the next week asking questions on SO about how some date code or column or query does not work.
again:
ZERO ZERO changes are required in Access. If your dates are starting to break, then the issue is not date formats, but that column is now being seen by access as a TEXT data type.
Soltuion:
Either change the sql side datetime2 columns to datetime, and re-link.
or
re-link your tables using a newer native 11 (or later - up to 18 now). that way, access will see/use/process the datetime2 as a correct date format in Access.
So, before you do anything? Open one of the Access tables linked to SQL server in design mode. (ignore the read only prmompt). Now, look at the data type assigned to the date columns. If they are text, then you have a royal mess.
You need to re-link using the newer ODBC drivers.
Zero of your existing code, sql and quires should be touched or even changed if you using a linked table to sql server. But then again, if you linked using the wrong SQL ODBC driver, then Access cannot see nor process those datetime2 columns as date - it will be using text, and you beyond really don't want to allow that to occur.
In summary:
Any date code, SQL updates, sorting, query, VBA code, form code, reports should continue to work with ZERO changes. If you are making changes to dates after a migration, then you done this all wrong, and those date columns are not being seen by access as date columns.
Either get rid of all datetime2 columns and then re-link (change them server side to datetime). Or re-link the tables using a native 11 or later ODBC driver. Either of these choices will fix this issue.
This is a fix that requires ZERO code, and zero changes to Access dealing with dates.

SQL Query - Change my long date format to DD-MM-YYYY

I have been researching a lot of similar questions on how to convert date format to DD-MM-YYYY but none work for the date format I'm using.
This is the date format I'm working with:
Wednesday, October 14, 2015 5:57 PM
And this IS a "date" field not a text field. I have a feeling that the inclusion of the day of the week is precluding my other attempts from working.
I'm new to SQL so forgive me if I'm overlooking something obvious.
Thank you!
On SQL Server, there are generally four ways to control date formatting.
Control it in your application. Most queries to the database return result sets that are typed. So a datetime column will be of data type datetime or timestamp in your application. You then apply the formatting from your application. The drawback to this is that, well, you wouldn't be asking this question if this were your problem.
Use the CONVERT() function, which allows limited formatting of datetime, datetime2, date, and time fields. SELECT CONVERT(VARCHAR(10),GETDATE(),103) will format the column to dd/mm/yyyy. The drawback to this is that you have to do it for every field you want to format every time you want to format them, and you lose the data typing since you're actually converting them to strings.
Use the SET DATEFORMAT command or the SET LANGUAGE command prior to executing your query. You can execute SET DATEFORMAT dmy; SELECT GETDATE() and your dates will be in dmy format. The drawback here is that you have to run it every time you run a query. Also note that since this returns your columns with data type intact, it's possible that your query analyzer will reformat the dates on you.
If you want to permanently set the date format, then ultimately you need to change the language away from us_english (or whatever the default was that was specified when you installed the server). You can see the list of available language configurations by running exec sp_helplanguage, and you can see the currently configured language for your session by running SELECT ##LANGUAGE. I know it's possible to set up your own languages if you need to, but be aware that's a custom configuration you'd have to deploy if you're running that kind of application.
Language configuration is potentially very complicated, since it's determined by the user, login, database or server, depending on where exactly it's specified. As far as I'm aware, logins inherit the server's default language, and users inherit the database's default language. User language is usually not specifed, but I believe it overrides the login language when present. I've only ever run in configurations where everything was defaulted to us_english, so I've not had much experience with this set up. You can modify it with ALTER LOGIN or ALTER USER, and you can set the default language on the server with sp_configure and on the database with ALTER DATABASE.

How to change date and time of SQL Server independent as windows same as nls_calendar in oracle?

I need to change date and time in sql server instance without and independent change windows date/time.
I can change date and time of oracle by nls_calendar command. Exists anyway to change this???
No, you can't. It is tied to OS date (ref).
But you can use a custom date function which will take a delta value and return GETDATE() + delta. If your code is going to be in production environment and would have to fallback to the real deal, then add another flag to the input (T for test and P for prod or something like that) and return GETDATE() when its production.

Data transfer between SQL Servers in different timezones

I transfer data from one SQL Server to another using SELECT * INTO .... statement (in Management Studio). Both databases have different timezone settings.
Is it possible to adjust all date/time fields to different timezone automatically? Maybe some query parameters or connection settings?
Since you are on SQL Server 2008, you could use the built-in functionality of the DATETIMEOFFSET datatype, and the SWITCHOFFSET function.
You could do something like:
INSERT INTO dbo.YourTargetTable( ...... )
SELECT
SWITCHOFFSET(TODATETIMEOFFSET(YourSourceTime, '+04:00'), '-09:00'),
.......
TODATETIMEOFFSET converts your "regular" date without any timezone information into a DATETIMEOFFSET type with a time zone (of your source location), and then you can apply a SWITCHOFFSET call to that value to "switch" your date/time to your target location's time zone.
This can be done in the scope of the INSERT....SELECT statement - no separate "row-by-row" updating necessary.
I think this will be helpful, please follow the link:
Effectively Converting dates between UTC and Local (ie. PST) time in SQL 2005
Shortly, you do need to build time zone and DST tables to get some help with time zones.
Thanks

CAST for Format() a DateTime to Date on both SQL Server and MS Access

I have an application that can use either SQL Server or MS Access as the data store.
In one table, I have a DATETIME column. I would like to retrieve the value as a DATE value (with the time part stripped off).
I can do that in SQLServer with CAST() and in MS Access with Format(). Ideally, I would like a single SQL query that could be applied against either database instead of sending a slightly different query to each database. Does anyone know a trick to do this?
Use DATEADD/DATEDIFF
Sql Server
SELECT DATEADD("d", DATEDIFF("d", 0, getdate()), 0) as someDateOnly
MS Access
SELECT DATEADD("d", DATEDIFF("d", 0, Now()),0) AS someDateOnly;
Just use a field name in place of GetDate() and Now()
E.g.
SELECT DATEADD("d", DATEDIFF("d", 0, [somefield]),0) AS someDateOnly;
as an aside DateAdd/DateDiff is the way you want to go if you want to strip the time in versions prior to 2008 anyway.
Sometimes you run access (e.g. in development) and sometimes a sql server and you want to make sure it works in both?
There are only two way I know (which don't pretend to be final :)).
create all selects as queries either in your sql or access database.
Then in your code you access the queries only
develop a database layer where you statements will be parsed and
handle each database.
Since the syntax is different there is no other way I know...

Resources