How to know a column store utc time in sql or not - sql

I am working with CDC in sql and we have a table lsn_timeMapping and a column inside this table trans_begintime its type as mentioned in sql is datetime. My question is how can i get to know that whether it stores datetime in utc format or server datetime .
HERE is a flow mentioned in msdn

It would appear that the time is stored based upon the server's locale settings(local timezone). Therefore, it will not be UTC unless the server timezone is set to UTC.
Source: https://connect.microsoft.com/SQLServer/feedback/details/533689/store-utc-in-lsn-time-mapping

Related

SQL How to set Constraint date default format MM-DD-YYYY [duplicate]

At the moment I have a column that holds timestamps as a datetime
The data is being stored as 10/30/2011 10:50:34 AM
My goal is to convert every date in that specific table to be formatted like 30 Oct 2011 10:50:34
The problem:
When I attempt to run this SQL it successfully processes.
UPDATE DatesTable
SET DateTime = '30 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
I noticed that was weird so if I changed the SQL to say
UPDATE DatesTable
SET DateTime = '31 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
It updates updates the datetime field with the correct day/time information but keeps it in the same format.
I have looked into other possible date datatypes for that column but nothing seems to support that date.
I have also come across this SQL but am unsure if it can solve my problem.
SELECT CONVERT(datetime, datecolumnname, formattingparam) as tmp FROM tablename
Converting Link
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.
The way to solve this is to use the 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.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 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 data is stored independent of format. You only need to reformat dates for display purposes.
sql server doesn't store the data in a text format. It is stored as a number.
Check out this page for how to format dates in sql
http://msdn.microsoft.com/en-us/library/ms187928.aspx
The datetime datatype tells the database engine how to store the data internally, so that you don't need to worry about human representations of the data. This is why your different UPDATE scripts don't change the format. The format is only applied when converting from the database's internal storage (a number representing the amount of time passed since a particular starting-point) and a human-readable date in the culture you require. You just need to format the date when it gets displayed, in the format suitable for your users. For example, for many, Month/Day/Year makes no sense.

Set timezone as IST (+5:30) in DB Browser for SQLite

I have been searching for a setting in DB BRowser for SQLite on how to change the timezone to IST (Indian Standard Time +5:30) Is there a way to set it directly without running any queries? I also found some SQL queries that can convert the db time to IST but almost all are SELECT statements. I am looking for a setting to change the timezone permanently and if that is not possible then may be an update query which can read all records in the database and change/convert/replace all times to IST. Can someone shed some light on it?
My field name is "expire_time" set as DATETIME NOT NULL in CREATE TABLE
What I searched for was
INSERT INTO MyTable(MyColumn) VALUES(datetime(CURRENT_TIMESTAMP, 'localtime'))
but I am not looking for insert statement
SELECT datetime(1092941466, 'unixepoch', 'localtime');
but I am not looking for select statement
Please help me either with a setting (if available in DB Browser for SQLite) or an update query that can change all times from GMT TO IST.
Thanks.
EDIT
SQLite has no DATETIME type. And it treats datatypes very different from other DBMS. For example
CREATE TABLE T (
Field MYTYPE
);
will run OK. Sqlite is applying so called datatype affinity https://www.sqlite.org/datatype3.html#affinity to figure out one of the implemented datatypes it will use instead of stuff specified it CREATE TABLE. DATETIME (as well as MYTYPE) affinity is NUMERIC - a special affinity which means column can store any type you want, TEXT for example.
This boils down the only way to work with DATETIME in Sqlite is datetime functions. And those functions use default timezone UTC. Any other timezone must be provided explicitly as a part of the datetime string. No PRAGMA or something to change this default.
EDIT
If expire_time is currently a string expression of UTC time you can get specific timezone text value, for example
select datetime(expire_time, '+05 hours','+30 minutes') || ' IST' as t
Note datetime(d,'utc') will most probably return NULL if string d contains explicit timezone. So i advice you standardize on storing datetime as UTC in DB and convert it to different timezone needed only when generating an output. This way you have all Sqlite toolbelt at your disposal.

SQL may UTC +8 hour issue

........
Where
(microsdb.MENU_ITEM_DETAIL.CheckDetailID = microsdb.CHECK_DETAIL.CheckDetailID Or
microsdb.DISCOUNT_DETAIL.CheckDetailID = microsdb.CHECK_DETAIL.CheckDetailID) And
microsdb.CHECKS.CheckOpen = CONVERT(CHAR(23), CURRENT_TIMESTAMP, 25)
**Return no result.
Field Data Type
microsdb.CHECKS.CheckOpen (datetime, not null)
CheckOpen 2013-04-08 06:29:26.000
I wondered why my CheckOpen time always 8 hours early than my server time.
Please advise.
Thanks
More than likely, when you stored data into the CheckOpen column of your CHECKS table you parsed it (or read it) directly from a client machine or client interface using their time-zone of US/Pacific.
Later, when you read CURRENT_TIME from your DB server you got the system time for that machine in UTC (since the machine was setup to use UTC by your server admin).
So, the two times are 8 hours off. UTC (GMT) is 8 hours ahead of US/Pacific.
Generally, if a client machine gives you data, you need to parse it, validate it, and sometimes translate it to valid server values or be aware when it's stored that it's only a "client" value. For date/time values, either convert to UTC or be sure to store the "offset" with the stored time. (actually it can be good to store offset even after converting to UTC)

Impossible to store certain datetime formats in SQL Server

At the moment I have a column that holds timestamps as a datetime
The data is being stored as 10/30/2011 10:50:34 AM
My goal is to convert every date in that specific table to be formatted like 30 Oct 2011 10:50:34
The problem:
When I attempt to run this SQL it successfully processes.
UPDATE DatesTable
SET DateTime = '30 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
I noticed that was weird so if I changed the SQL to say
UPDATE DatesTable
SET DateTime = '31 Oct 2011 10:50:34'
WHERE DateTime = '10/30/2011 10:50:34 AM'
It updates updates the datetime field with the correct day/time information but keeps it in the same format.
I have looked into other possible date datatypes for that column but nothing seems to support that date.
I have also come across this SQL but am unsure if it can solve my problem.
SELECT CONVERT(datetime, datecolumnname, formattingparam) as tmp FROM tablename
Converting Link
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.
The way to solve this is to use the 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.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 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 data is stored independent of format. You only need to reformat dates for display purposes.
sql server doesn't store the data in a text format. It is stored as a number.
Check out this page for how to format dates in sql
http://msdn.microsoft.com/en-us/library/ms187928.aspx
The datetime datatype tells the database engine how to store the data internally, so that you don't need to worry about human representations of the data. This is why your different UPDATE scripts don't change the format. The format is only applied when converting from the database's internal storage (a number representing the amount of time passed since a particular starting-point) and a human-readable date in the culture you require. You just need to format the date when it gets displayed, in the format suitable for your users. For example, for many, Month/Day/Year makes no sense.

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 ())?