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

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.

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.

How do I change the date in SQL Server 2017 to UK format?

I have got several tables in my database (with data) that is formatted in the American standard of mm/dd/yyyy. Is there a way to convert the date to a British format (i.e. dd/mm/yyyy) that doesn't involve dropping and recreating the tables?
Thanks!
I set my data type to >date when I was creating my table. I can store dates in the format mm/dd/yyyy, but not dd/mm/yyyy.
As I've mentioned in my comment, dates are not stored with their display format - in fact, you can say that dates have no display format - only string representation of dates have a display format.
Whenever dealing with string literals representing date and datetime values in SQL server, use ISO 8601 datetime format (yyyy-MM-ddTHH:mm:ss or yyyyMMddTHHmmss).
SQL Server guarantees to properly parse this string representation into date / datetime values, without ambiguity.
Please note the T seperator between the date and the time. There is a very similar standard format, where the T is replaced with a white-space, but the DateTime data type have a bug parsing this format - and it is culture-dependent (Note that DateTime2 does not have that bug) - and that's another reason why you should never use datetime again.
When you use a string literal like '25/03/2018' it's easy for a human to see that it stands for March 25th 2018, but SQL Server will raise an error trying to parse this string into a date if the current value of DATEFORMAT is not DMY.
However, SQL Server will always parse ISO 8601 string representation of dates correctly, regardless of any local settings or previous set dateformat or set language statements etc'. '2018-02-01T15:40:50' will always be parsed is February 1st 2018, 3:40:50 PM.
Unless specified, As Martin Smith wrote in his comment, the default dateformat depends on the defualt language settings of the current login - so a query that works for one login might raise an error for another login - and that's another good reason never to trust culture-specific string representation of datetime.
DECLARE #dt DATETIME = '01/20/2019';
SELECT FORMAT( #dt, 'd', 'en-gb' ) AS 'UK'
Are you referring to the date format displayed by SQL Server Management Studio or a similar application? The format is controlled by Windows Control Panel settings, not by SQL Server. There is no internal format for dates in SQL Server.
This is defined by default from the machine where is running MS SQL Server.
To see all available cultures please do:
select * from sys.syslanguages
Then, you can change SQL Server language using:
SET LANGUAGE BRITISH
... and the date format will always be like you want.
Note: this will change all the database (not just the date format), the other way is to change the date format using the FORMAT function in T-SQL.

Standard SQL Date vs Legacy SQL Date

I noticed that there is an inconsistent result from Date Function in Standard SQL and Legacy SQL,
In standard SQL, current_date() returns a DATE data type, however, in Legacy SQL, current_date() returns a String data type.
Then, If I create a table which has 'Date' column by using Standard SQL, later on, when I querying the table using Legacy SQL, I have to format 'Date' from Date Data Type To String Data Type, or the other way around.
Unlike Timestamp Data Type, I can query the same table across different SQL Language. Is there an easy way to query in Standard SQL and legacy SQL without converting between Data type and String type?
Thank you in advance.
Legacy SQL has very limited support for the civil time types, so you're better off sticking with standard SQL if possible.

Date and time comparison in SQL Server

I am very new to SQL Server. My problem is to write a SQL query that compares datetime column with some fixed date AND time, but I would like to make it independent of language settings (regarding date and time values).
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value. So when stored, there's no issue with language dependent formats or anything like that.
When using string literals representing date and time, however, there are many formats supported by SQL Server. The way to go 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 for date and time looks like this:
YYYY-MM-DDTHH:MM:SS - 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.

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.