The datetime columns in all the tables on SQL server have shifted by a few hours - sql

I've created a few tables all of which had a column of datatype datetime. Recently, I think after the daylight savings adjustment, for some reason all the dates have shifted by a few hours i.e. '2022-01-01 00:00:00' has changed to '2021-12-31 18:00:00'.
My guess is that it has something to do with the daylight savings and some server setting related to that but I can't figure out how to revert it back.
Edit:
As suggested in the comments, it might have something to do with the sqoop-export i.e. what I'm using to transfer data from hdfs to sql server. So, the question really is how could I look at what is being inserted into the table. Is there a way I can redirect all insert statements to a log file before being sent over to sql server?

Related

What is standard date time string format for SQL query for insering data in datetime columns?

Today I faced a problem while porting an SQL server database to H2DB. The SQL queries I had written to insert date time, wasn't working on H2DB. i.e '26-Jun-2019 01:00:00' gave exception on H2DB. My question is what is standard format for inserting date time which is database independent?
The standard format as defined by the SQL standard is the ISO format, prefixed with the keyword DATE, e.g.
DATE '2019-06-26'
or for a timestamp:
TIMESTAMP '2019-06-26 17:42:00'
However not all database products support the SQL standard in that regard, so you will most probably not find one single format that will work across all database products.

why "CURRENT_TIMESTAMP" showing wrong time in SQL?

I am using "CURRENT_TIMESTAMP" for automatic date setup inside the column for each row in SQL. It shows date and time but time is wrong. please help.
CURRENT_TIMESTAMP will show you the server timestamp not client side timestamp from where you are querying.
Please check in which timezone your database server is located.

SQL GetDate() returns wrong time

I am having an issue while using GetDate(), for some reason is not returning the right time (it is 7 hours ahead from the actual time) I am using AZURE and the Database is configured with the right location (West US). I will appreciate any help!
I tried to run this script:
SELECT id,
status,
AcceptedDate,
Getdate(),
Datediff(hour, AcceptedDate, Getdate())
FROM orderoffers
WHERE status = 'Accepted'
Azure SQL Databases are always UTC, regardless of the data center. You'll want to handle time zone conversion at your application.
In this scenario, since you want to compare "now" to a data column, make sure AcceptedDate is also stored in UTC.
Reference
The SQL databases on the Azure cloud are pegged against Greenwich Mean Time(GMT) or Coordinated Universal Time(UTC) however many applications are using DateTime.Now which is the time according to the regional settings specified on the host machine.
Sometimes this is not an issue when the DateTime is not used for any time spanning or comparisons and instead for display only. However if you migrate an existing Database to SQL Azure using the dates populated via GETDATE() or DateTime.Now you will have an offset, in your case it’s 7 hours during Daylight Saving Time or 8 hours during Standard Time.
I created a simple function that returns the correct UK time whether in DST or not.
It can be adapted for other time zones where DST kicks in.
CREATE FUNCTION [dbo].[f_CurrentDateTime]() RETURNS DATETIME AS
BEGIN
RETURN DATEADD(HOUR,CONVERT(INT,(SELECT is_currently_dst FROM sys.time_zone_info WHERE 1=1 AND NAME = 'GMT Standard Time')),GETDATE())
END
In this modern times where infrastructure is scaled globally, it is good idea to save data in UTC and convert to a timezone based on users location preference.
Please refere: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.utcnow?view=netframework-4.7.2

Oracle time comparison returning older instead of newer times

The end of my select query is:
and logoff_time > to_date('2013-11-27 14:18:42','yyyy-mm-dd hh24:mi:ss');
yet this is returning (in SQL Developer) rows with times in logoff_time before the specified time - e.g. 3am.
Is this problem something to do with timezones? I'm in GMT and so is the server, although I don't know what timezone it thinks it is in.
This logoff_time column is an Oracle system audit column, by the way. (Actually, I've attempted to reconstruct it by a subquery directly on the underlying audit table.)
My mistake. The times were in fact newer. I was just confused by the fact that I had set them to display as HH:MI:SS instead of HH24:MI:SS in the SQL Developer preferences, so I was looking at 03 and thinking that meant 3am instead of 3pm.

SQL Server, strange DATETIME behaviour

I have SQL Server Express 2008 on my local system and I am doing some insertions in a datetime column.
The problem is that the same system on production on (SQL Server 2005) hosted on godaddy records a datetime entry as the previous date and a time of 13:00
E.g. Date being inserted is 07/01/2010 00:00:00
Entry in Local DB = 07/01/2010 00:00:00
Entry in Prod DB = 06/30/2010 13:00:00
Could it be some server/db level setting for datetime storage ?
Edit 1:
pls note, I'm inserting a predefined datetime value, the date being inserted is exactly 07/01/2010 00:00:00. I am NOT using GETDATE().
Edit 2: Solution
Ok, thanks for the answers guys but the problem was not from SQL Server, it was from the data being read from an XML form of the serialized dataset. It was sending the the datetime information as 'mm/dd/yyyy T00:00:00+4:00'
All i did was remove the remove the time segment from it and then insert it in the DB.
Cheers !
You are in Dubai = GMT+y hours
Godaddy is in the USA = GMT-x hours
GETDATE() gives SQL server time
You should use GETUTCDATE() to give GMT (UTC since we lost our empire it appears) which will be consistent globally.
The time difference makes me wonder if this is a UTC thing. Are you running this via SSMS or via application code?
Does it always insert 11 hours behind your local?
Is that an explicit time you're giving the column, or is that the column default (perhaps GETUTCDATE ())?