Currently am using below mentioned code
CONVERT(DATETIME, "16-05-2015 21:27:25.280",105)
Using this its working in my system culture and one of our client in UK.
But facing issue in our US client.
Error message:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value
So there any way to convert the string to DATETIME depends on system culture like in .net.
Thanks in advance
Thejus T V
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 (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 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 (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.
If you use SQL Server 2008 or newer 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 recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
Related
My date format of "03-01-2017 10:24:48" is getting stored in SQL as "2017-03-01 10:24:48.000" where my 'dd-mm-yyyy' format is getting converted to 'yyyy-mm-dd' format.
How can I change the date format of the column to my desired format? My table already contains data.
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical (binary) 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 (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 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 (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.
If you use SQL Server 2008 or newer 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 recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
Changing the format does not change the way the data is stored, just the way it is displayed. You can find date and time styles at https://msdn.microsoft.com/en-us/library/ms187928.aspx?f=255&MSPPError=-2147217396.
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.
I have a Database set up with a column for Datetime.
In my application a have a date selector.
The issue is that the Db uses American Date Format (MM/DD/YYYY) where as the app uses European Date Format (DD/MM/YYYY).
Is there a way to set up the column in the database to use European date format?
I don't want to have to convert through the code in the application.
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value, and a DATETIME is a DATETIME is a DATETIME.
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.
So if you want to see your DateTime in US format, use
SELECT CONVERT(VARCHAR(30), YourDateTimeColumn, 101)
and if you need European (British/French/German) format, use
SELECT CONVERT(VARCHAR(30), YourDateTimeColumn, 103)
It's is a commonly accepted "Best Practice" to avoid using dates as string as much as possible - if ever possible, use the native SQL Server and .NET DATETIME datatype for sending back and forth dates (which is independent of any regional formatting). Try to convert the DATETIME to string only when you need to show it (preferably only in your UI - not in your database!)
Update: if you want to insert DateTime values, as I said, I would strongly recommend to use a proper datatype - and not fiddle around with specifically formatted strings.
If you must use strings, then by all means 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 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 (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.
If you use SQL Server 2008 or newer 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 recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
About "YYYY-MM-DDTHH:MM:SS for dates and times - 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":
Another option is the usage of the usual space as separator between data and time: YYYYMMDD HH:MM:SS or YYYYMMDD HH:MM:SS.SSS.
But then, again, dashes have to be omitted: as said for the long format, " 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!"
Please note that the time part needs the semi-colons!
Converting to DateTime2 is more tolerant, it works with and without dashes, even using European locales.
Dates don't have a "format" - date representations have a format. You should be passing the dates as dates instead of strings to your application, then converting to strings in whatever format is appropriate.
Since you don't say what platform your application is in the code may change, but here's a solution in C#:
DateTime dt = reader.GetDateTime(dateColumnIndex);
string s = dt.ToString("dd/MM/yyyy");
I am trying to read a column from tbl1 with type varchar(255) and load it to tbl2 as type datetime. See code below.
SELECT CAST(LTRIM(RTRIM(NLCompany)) AS varchar(20)) AS SRC_NLCompany,
CAST(LTRIM(RTRIM(AccountCode)) AS varchar(8)) AS SRC_AccountCode,
CAST(LTRIM(RTRIM(DocumentNumber)) AS numeric(10, 0)) AS SRC_DocumentNumber,
CAST(LTRIM(RTRIM(PaymentType)) AS varchar(1)) AS SRC_PaymentType,
CAST(LTRIM(RTRIM(PostingDaybkDate)) AS datetime) AS SRC_PostingDaybkDate
FROM TBL1
Error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
There are many string formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what language / dateformat settings you have - therefore, these settings might work some times - and sometimes not.
The way to solve this 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 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 (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.
If you use SQL Server 2008 or newer 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 recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
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.