Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / SQL

Using Case to convert Date format in SQL Select

Rate me:
Please Sign up or sign in to vote.
3.11/5 (4 votes)
28 Sep 2016CPOL 21.7K   3   3
Using Case to convert Date format in SQL Select

In legacy and new application, date format can be a headache, simply because the date format may be stored differently and maybe in a string value.

I find using CASE WHEN statement to convert all my dates to the same format.

The Converting Function

This case statement takes the table field to the SQL converter function, which needs a different style due to the known format of the string value.

I have added a link to this blog, which shows you the different styles you can use.

CONVERT ( DATETIME , [TABLE.FIELD], SQLSTYLE) 

SQL SELECT Case Statement

Below is an example where you can use a case statement to use different convert styles, depending on the string value format.

SELECT 
			CASE 
			 WHEN DATAFIELD THEN  CONVERT( DATETIME , '1900/01/01 00:00:00',103)
		 
			 WHEN  DATAFIELD THEN  
				CASE 
                                      
                                       --I KNOW WITHIN MY DATA SET, THERE IS DATE STRING WITH - AND / CHARACTERS AND THEY NEED TO BE CONVERTED DIFFERENTLY
                                     
					WHEN DATAFIELD  LIKE   '%-%'     AND ISDATE(DATAFIELD) = 1  THEN 
						CONVERT( DATETIME,  DATAFIELD,120 ) 
					WHEN DATAFIELD  LIKE   '%/%'     THEN  
						CONVERT( DATETIME,  SUBSTRING(DATAFIELD,0,11),103) 
					ELSE
 
                                       --THE DATE 1900/01/01 IS THE FIRST DATE, I LIKE TO USE TO MAKE SURE I KNOW ITS NOT TO BE ACTED ON, BUT ITS IN THE CORRECT FORMAT
					CONVERT( DATETIME,  '1900/01/01 00:00:00',103)
				END
				ELSE 
					CONVERT( DATETIME, '1900/01/01 00:00:00',103)
			 END  AS  DATETEXTFIELD		
	 FROM DATATABLE 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHard to understand Pin
Wendelius29-Sep-16 9:50
mentorWendelius29-Sep-16 9:50 
Not voted yet since in my opinion the idea you present is very hard to understand at the moment. For example:
  • You store dates in strings. This is the first problem and should be resolved by storing the dates as dates in the database
  • You mention legacy systems so let's consider you cannot affect the database design. Then what is the idea of different WHEN parts? In the example you have
    SQL
    SELECT 
       CASE 
          WHEN DATAFIELD THEN  CONVERT( DATETIME , '1900/01/01 00:00:00',103)
          WHEN DATAFIELD THEN
             CASE...
    In what situation would the latter WHEN fire?
    At least in the example the condition is the same as in the first WHEN.

  • What is the DATAFIELD used in the condition? Not a column name I suspect.
  • From code
    SQL
    WHEN DATAFIELD  LIKE   '%-%'     AND ISDATE(DATAFIELD) = 1  THEN 
       CONVERT( DATETIME,  DATAFIELD,120 ) 
    WHEN DATAFIELD  LIKE   '%/%'     THEN
       CONVERT( DATETIME,  SUBSTRING(DATAFIELD,0,11),103) 
    ELSE ...
    Why in the first conversion the content of the column is tested to be date but in the second case the test isn't carried out? Wouldn't the test in the first case fail if the value isn't implicitly recognized as a date?
  • Why the second conversion uses substring?
  • From the code
    SQL
    --THE DATE 1900/01/01 IS THE FIRST DATE, I LIKE TO USE TO MAKE SURE I KNOW ITS NOT TO BE ACTED ON, BUT ITS IN THE CORRECT FORMAT
    Why a constant date (1900-01-01) is used to indicate a missing date instead of NULL?
Perhaps adding some practical examples would clarify the idea.

modified 1-Oct-16 3:28am.

QuestionFormatting issues with several blog posts Pin
Wendelius28-Sep-16 4:13
mentorWendelius28-Sep-16 4:13 
GeneralRe: Formatting issues with several blog posts Pin
Thomas Cooper28-Sep-16 4:53
Thomas Cooper28-Sep-16 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.