Convert text to system date format [duplicate] - sql

This question already has an answer here:
SQL data error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
(1 answer)
Closed 6 years ago.
I want to convert a text date to system date format. How can I accomplish that?
I have the format where I want to convert it 'MM/DD/YYYY' for example, but it can change, it can be dd/mm/yyyy or yyyy/mm/dd.

EDIT
From SQL Server 2012 and later you might use the FORMAT function if you want to force a date into a formatted text: https://msdn.microsoft.com/en-us/library/hh213505.aspx
With earlier versions you might use something like this:
DECLARE #d DATETIME=GETDATE();
DECLARE #TargetFormat VARCHAR(100)='DD/MM/YYYY';
SELECT CONVERT(VARCHAR(100),#d, CASE #TargetFormat
WHEN 'MM/DD/YYYY' THEN 101
WHEN 'DD/MM/YYYY' THEN 103
--add all formats convert can undertsand
ELSE 101 END )
Previous
Look here: https://msdn.microsoft.com/en-us/library/ms187928.aspx
The third parameter in CONVERT specifies the format.
The MM/DD/YYYY was 101, dd/mm/yyyy was 103
But: You should avoid date literals as they are depending on your system's culture.
From your question I get that you are the owner of the source, so you can change this to the way you need it. I strongly advise you to use independent formats.
Look here: https://stackoverflow.com/a/34275965/5089204

Here is an example
SELECT CONVERT(DATETIME,'04/13/2016',101)
The result is of datetime datatype

Related

Convert getdate() to dd/mm/yyyy in SQL Server [duplicate]

This question already has answers here:
How to cast the DateTime to Time
(3 answers)
Closed 2 years ago.
How to convert getdate() to the format dd/mm/yyyy while keeping the datatype date and not varchar? This is what I tried
SELECT cast(CONVERT(varchar(12), GETDATE(), 103) as date)
but it is wrong.
The date datatype doesn't have a concept of a format. It's just a moment in time perhaps with a timezone attached but without any particular fixed representation. When you format it, it becomes a varchar. If you cast the varchar back to a date, it loses the format. This will be pretty much the same in any programming language that has a native date datatype (or date/datetime objects). It's not specific to SQL.
If you want a particular string representation of a date, then you want a varchar, so don't try to cast it back to a date.
This seems to be what's called an XY problem. What is wrong with CONVERT(varchar(12), GETDATE(), 103)? Yes, it's a varchar, but what's why are you unsatisfied with it being a varchar?

Why does SQL Server convert VARCHAR to DATETIME using an invalid style?

I can't make out from the documentation why SQL Server parses a text in a format other than the specified style.
Regardless of whether I provide text in the expected format:
SELECT CONVERT(DATETIME, N'20150601', 112)
or incorrect format (for style 113):
SELECT CONVERT(DATETIME, N'20150601', 113)
The results are the same: 2015-06-01 00:00:00.000 I would expect the latter to fail to convert the date (correctly).
What rules does it employ when trying to convert a VARCHAR to DATETIME? I.e. why does the latter (incorrect format style) still correctly parse the date?
EDIT: It seems I've not been clear enough. Style 113 should expect dd mon yyyy hh:mi:ss:mmm(24h) but it happily converts values in the format yyyymmdd for some reason.
Because the date is in a canonical format ie(20150101). The database engine falls over it implicitly. This is a compatibility feature.
If you swapped these around to UK or US date formats, you would receive conversion errors, because they cannot be implicitly converted.
EDIT: You could actually tell it to convert it to a pig, and it would still implicitly convert it to date time:
select convert(datetime,'20150425',99999999)
select convert(datetime,'20150425',100)
select convert(datetime,'20150425',113)
select convert(datetime,'20150425',010)
select convert(datetime,'20150425',8008135)
select convert(datetime,'20150425',000)
And proof of concept that this is a compatibility feature:
select convert(datetime2,'20150425',99999999)
Although you can still implicitly convert datetime2 objects, but the style must be in the scope of the conversion chart.
Reason why is the date N'20150601' converted to valid datetime is because of fact that literal N'20150601' is universal notation of datetime in SQL Server. That means, if you state datetime value in format N'yyyymmdd', SQL Server know that it is universal datetime format and know how to read it, in which order.
You should convert to varchar type in order to apply those formats:
SELECT CONVERT(varchar(100), CAST('20150601' as date), 113)
OK, you are converting datetime to datetime. What did you expect? In order to apply formats you should convert to varchar and you have to have date or time type as second parameter.

How to check what is the format of a datetime column on my sql server table? [duplicate]

This question already has answers here:
Truncate Datetime to Second (Remove Milliseconds) in T-SQL
(9 answers)
Closed 7 years ago.
I have a table XPTO on my SQL Server.
There is a column DATEEXAMPLE datetime.
When I select the date it comes: 2013-12-02 16:14:13.023 (aaaa-mm-dd hh:mm:ss.sss).
Is this the real datetime format?
How can I change this format to "aaaa-mm-dd hh:mm:ss"?
The data is stored in an internal format that is format-less. The format is something you see on output (and deal with in input).
How you handle the format depends on the database. In SQL Server, you use the convert() function, which is described here. Personally, I have a strong preference for the ISO standard format, which is what you are seeing.
Note: you can also change the default display format in SQL Server by changing the default format (see here).
Use:
convert(varchar, DATEEXAMPLE, n)
By changing n you will have some formatted results: (MSDN)
20 = 120 = yyyy-mm-dd hh:mi:ss(24h)
5 = dd-mm-yy
105 = dd-mm-yyyy
10 = mm-dd-yy
110 = mm-dd-yyyy

Store DateTime as culture independent in SQL Server 2008

I currently have a challenge of storing a DateTime value in a NVarChar field so that it's culture independent.
I've read that you can convert the value to an int by using CONVERT(int, GETDATE(), 112) which should make it culture independent but the former statement doesn't store the time.
What is the industry standard of storing a DateTime as culture independent?
EDIT
Please note that I can't use DateTime in my scenario. It must be NVarChar.
EDIT 2
Alright, found the answer to my own question.
To convert a DateTime to it's binary(8) raw format:
convert(binary(8), GETDATE())
I then store the value in a VARCHAR field as follows:
CONVERT(VARCHAR(MAX), convert(binary(8), GETDATE()), 2)
To retrieve it back from the varchar field and convert it to DateTime:
CONVERT(DateTime,CONVERT(binary(8), [TextField], 2))
As var as I'm concerned, this will store a DateTime as culture independent.
EDIT 3
It seems like user Kaf has the best solution. I will rather use format 126 to convert it to text and then back to DateTime from text.
Thanks everyone and sorry for the confusion.
If you CANNOT store date as Datetime, you can use style 126 which gives ISO8601 format (yyyy-mm-ddThh:mi:ss.mmm (no spaces)). I think it is culture independent.
Fiddle demo
select convert(nvarchar(50),getdate(),126)
Best thing is to store Date as a DateTime/Date type.
You should use DATETIME or DATETIME2 data type to store date and time values. They are stored in binary format in the database and are culture independent.
You can read more on MSDN here: http://msdn.microsoft.com/en-us/library/ms187819(v=sql.100).aspx
More on how SQL Server stores the datetime values: "It uses 8 bytes to store a datetime value—the first 4 for the date and the second 4 for the time." (from: http://sqlmag.com/sql-server/solving-datetime-mystery)
I do not get this idea to store a date in a varchar field so that it is 'culture independant'. dateTime data type is culture independant. What is culture dependent is the way date values are displayed:
MM/dd/YYYY
dd/MM/YYYY
YYYY-MM-DD
etc
But, if the display changes, the underlying value itself is still the same ... and this is why you can easily 'convert' dates from one format to another....
So, for the sake of simplicity, I do strongly advise you to switch to a culture-independant, datetime field. Otherwise any further use of this field's content (calculation, display, print out, etc) will be a real PITA ...

Convert Date Against Where Clause

I have Following dummy table with data:
ACID srno date(mm/dd/yyyy) name
3 1 04/12/2010 mahesh
3 2 04/12/2010 mahendra
Now if I try with Following SQL Transact:
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',101) –- I have date in dd/MM/yyyy Format
and ACID=3
It’s Not returning the srno of the table. That means Date is not execute convert statement as above
What’s the reason?
Try using style 103 instead of 101.
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',103) –- I have date in dd/MM/yyyy Format
and ACID=3
If you convert 12/04/2010 using format 101, you get date "December 4, 2010", which is not in your database. Use format 103 to convert a date in format dd/mm/yyyy to DateTime.
The database stores dates using the DateTime type which is format-agnostic. It does have a default format for string conversions, which seems to be mm/dd/yyyy (101) on your database.
However, when you convert a string to add it to your table, you want to specify the format of your input string, in your example dd/mm/yyyy (103).
Take a look at the MSDN article for CAST and CONVERT which details all format styles that you can use with dates.
To be honest, if you want to specify a DATE LITERAL in SQL Server, please stick with the simplest YYYYMMDD format, e.g.
and dummy.date = '20100412'
It is robust and works for all regional, user language and dateformat settings. This assumes the other side of the comparison is already a date column. Even if you had to CAST it, using this format you don't need to specify a format
and dummy.date = cast('20100412' as datetime)