Monday, March 18, 2013

Convert String Date to SQL Date

If you need to convert a date that is in a string format:

03/18/13

into a SQL Server Date so that you can process it, ORDER BY, etc.. then do the following:

CAST(HIRE as Datetime)

where HIRE is a date in string format. If you want to display the date in a particular format, then just CONVERT it to the format you want:

CONVERT(CHAR(10),CAST(HIRE as Datetime),101) as Hire_Date

The CHAR(10) saves space for 10 characters and the 101 is one of the formats available. I didn't use VARCHAR because the field is exactly 10 characters.  See the link below for a format you want to use if you don't like the 101 format (mm/dd/yyyy)




http://msdn.microsoft.com/en-us/library/ms187928.aspx


No comments:

Post a Comment