How do I display varchar date string with mixed format to another format? - sql

I am using SQL server 2008 R2. I know I can use CONVERT with different format code as the third parameter to do the conversion to DATETIME first and CONVERT again to VARCHAR with another format code to change the display format.
The real problem now is I have mixed raw data in a single column. So my question is how do you write a single SELECT statement to display from mixed YYYY/MM/DD, DD/MM/YYYY all to DD/MM/YYYY?
I tried to use ISDATE() but it think 31/01/2013 is not a date while 01/01/2013 is a date. Now I could only think of to see if the YYYY is on the left or on the right to determine the correct input format, but I dont know how to write it out in a single SELECT statement.
Any procedure to change the format first then do a simple SELECT is not an option. I am not allowed to change the source.
Thank you

Why not just use string manipulations? Something like:
select (case when substring(d, 5, 1) = '/' -- YYYY/MM/DD
then right(d, 2)+'/'+substring(6, 2)+'/'+left(d, 4)
else d
end)
By the way, if you are choosing formats for dates when represented as strings, I highly recommend YYYY-MM-DD (or YYYY/MM/DD) because comparison operators work on them.

If you are sure that only those 2 formats (yyyy/mm/dd and dd/mm/yyyy) exist in the data, then you could probably get away with a CASE statement along the lines of:
CASE
WHEN (SUBSTRING(dateColumn, 5, 1) = '/') THEN CONVERT(datetime, dateColumn, 111)
ELSE CONVERT(datetime, dateColumn, 103)
END

Related

Convert TEXT to Date in SQL Server 2012

I have a TEXT in this format 31/10/15.
How do I convert this into a DATE format?
As I need to let the user search from data using a date range.
example: From 15/7/13 to 31/10/15
Or is there a way to so without converting to date?
You can use CONVERT() for this:
DECLARE #d VARCHAR(50) = '31/10/50'
SELECT CONVERT(DATE, #d,3)
Note that with a 2-digit year SQL Server will make the year start with '19' for 50 and up, and 49 and below will be '20'
Storing as a DATE field will allow easier comparisons, otherwise you'll have to perform this conversion at each step.
Use CONVERT; example:
SELECT [Date] = CONVERT(date, '31/10/15', 3);
And yes, it's possible to search dates in the same format as the examples you provide, but don't do that – use the proper data types in both your queries and your table columns.

SQL date formats

How can I convert this 201402110544 to date(2014-02-11) in SQL server 2008?
You can cast as DATE data type (https://msdn.microsoft.com/en-us/library/bb630352(v=sql.100).aspx)
SELECT CAST(datetime_value AS DATE)
SELECT CAST(GETDATE() AS DATE) --> 2015-08-18
If you have a string to cast as DATE you can use:
SELECT CAST(LEFT('201402110544', 8) AS DATE)
You trim out the time part, by taking 1st 8 chars (YYYYMMDD) and will result a valid string to cast as DATE.
If this format is always the same, you can do this
DECLARE #d VARCHAR(20)='201402110544'
SELECT CAST(SUBSTRING(#d,0,9) as DATETIME)
Also have a look at The ultimate guide to the datetime datatypes which explains in detail about handling date-times
String to date conversion sure is a weakness of SQL Server. CONVERT does that, but can only deal with a number of given formats. So you must convert your string into such a format first and then convert it to date afterwards.
One such format is 120 = 'yyyy-mm-dd hh:mi:ss' which I think comes closest to yours. Another 102 = 'yyyy.mm.dd' suffices to get the date.
convert(
date,
concat(substring(datestring, 1,4), '.',
substring(datestring, 5,2), '.',
substring(datestring, 7,2)),
102)
from data;
SQL fiddle: http://www.sqlfiddle.com/#!3/9eecb7/3689
EDIT: I stand corrected. Horia is right; CAST supports 'yyyymmdd', so you can use it directly by merely cutting of the string's time part. This is more readable than my suggestion above, which makes CAST the better option in your case.

I want to convert a whole column into the date format of yyyymmdd, I do not want the current date

I want to convert a whole column into the date format of yyyymmdd, I do not want the current date, thus I cannot use getdate() command, there is already data in the column, I just need the right command to convert the whole column into yyyymmdd format.
The column I am using is FIELD_034 and the table is Sur_CompassAuto1_1_7_fetch.
I am using SQL Server.
Thank you
I fully agree with the paqogomez's response, but instead of date style of 111, better use 112.
The output for the statement
SELECT CONVERT(VARCHAR(10), FIELD_034, 111)
will result in yyyy/MM/dd. Where as,
SELECT CONVERT(VARCHAR(10), FIELD_034, 112)
will return yyyyMMdd, which is what he needed.
A common misconception is that a datetime has a format. If you are storing your dates as a datetime, then you can output it in any format.
It sounds as though you might be storing your values as a Varchar. You would be better off converting your varchar dates into a datetime, then you can do whatever you want with them
That said, if you HAVE FIELD_034 as a DateTime, then its as easy as
SELECT CONVERT(VARCHAR(10), FIELD_034, 112)
from Sur_CompassAuto1_1_7_fetch
If the field is a varchar its similar:
SELECT CONVERT(datetime, FIELD_034, 112)
from Sur_CompassAuto1_1_7_fetch
The difficulty of this one is if you have your values in different or nonstandard formats. Then it would require some clean up to make the query work.
Edit: as #AArnold says, its 112 instead of 111. Date formats are subtle.

How to convert date stored in nvarchar to date in MMDDYYY format removing dashes (e.g 06122012)?

I want to convert date stored as nvarchar in Birth_date column SQL.
For example, currently I have dates in 1999-01-22 format, I want to convert it to 01221999 (8 characters) using SQL Server.
Can anybody help me please?
I agree with #marc_s that you should probably be using the proper data type (date), but if you really have to return this as your custom format, we can make good use of convert:
select replace(convert(varchar(10), convert(date, Birth_date), 101), '/', '')
I suggest that it's a code smell that convert doesn't even have the format you're looking for. We must therefore massage the converted data, in this case using replace to remove slashes.

Formatting Dates In SqlServer To Use As Comparison

This should be easy
I have a date column on table A
SELECT * FROM TABLEA
WHERE DTE = '01/02/2010'
The problem is that this is deployed onto US servers and I have no control over the date as its an arg.
How can I tell SqlServer to treat this date as being in that format??
I gave tried this:
SELECT *
FROM TABLEA
WHERE DTE = CONVERT(VARCHAR(10), '01/01/2010' , 101) AS [DD/MM/YYYY]
Your last try was almost correct, but it should have been
SELECT * FROM TABLEA WHERE AS_OF_DATE = CONVERT(DATETIME, '01/01/2010', 101)
Use a safe format. For dates (without a time component), the safe format is YYYYMMDD, e.g. today is '20100209'.
For datetimes, the safe format is YYYY-MM-DD'T'HH:mm:SS, where 'T' is the literal T, so right now is '2010-02-09T11:10:30'.
(When I'm saying safe, I mean that SQL Server always, unambiguously, knows how to convert these strings into datetime values)
Check out this reference article: The ultimate guide to the datetime datatypes
EDIT: Specifically what Tibor says about SET DATEFORMAT & SET LANGUAGE, since you mention that you have no control over the input format.
Another option is a double conversion (check performance when used as criteria):
select strTempNo, dtmTempDateStart,
convert(varchar(10), Convert(datetime, dtmTempDateStart, 103), 126) As UTCDate
I use 103 here as the data is already in UTC format but this works as well (UTC ISO8601 is 126).
If your dates are known to be always in American format you have to use 101.
Alternatively use 112 (ISO "safe" format) and cut the first 8 characters out of the string.
Data sample: (Sorry, don't have an American date table available)
521002 2008-09-1500:00:00.000 2008-09-15
580195 2008-04-1500:00:00.000 2008-04-15
530058 2008-09-2200:00:00.000 2008-09-22
580194 2008-04-0200:00:00.000 2008-04-02
500897 2008-07-0100:00:00.000 2008-07-01
500966 2008-09-2300:00:00.000 2008-09-23