Avatar of tmajor99
tmajor99

asked on 

SQL Select - Text to Date Conversion,

I have a text column in my SQL server table that contains a date like this 02/21/2015.   I need to evaluate this column in a where a clause so I can get all the records that have a date > 9/21/2015.  

This is what I need to do:  
Select CREATE_DATE from MyTable where CREATE_DATE > 09/21/2105.  

I am assuming there is a CAST or CONVERT that I can use?
Microsoft SQL Server

Avatar of undefined
Last Comment
PortletPaul
ASKER CERTIFIED SOLUTION
Avatar of jyothees venkat
jyothees venkat

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

You cannot convert directly from a TEXT datatype to DATE but you can convert it if you do it in two steps, convert to VARCHAR/NVARCHAR and then to DATE:

DECLARE @Example TABLE
(
	DateString	TEXT
);

INSERT INTO @Example (DateString)
VALUES ('09/21/2015');

SELECT CONVERT(DATE, CAST(DateString AS VARCHAR), 101)
FROM @Example

Open in new window

If you are using SQL 2012 or above then I would recommend using the TRY_CONVERT function instead.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

To avoid errors from invalid data that does not contain a date, the safest way to do it is below.  You will inevitably lose efficiency no matter how you do this conversion:

Select CREATE_DATE
from MyTable
where 1 =
    case when isdate(left(create_date, 10)) = 0 then 0
             when convert(date, left(create_date, 10)) > '20150921') then 1
             else 0 end

Note that a character comparison against a 'mm/dd/yyyy' format will be invalid!
Avatar of PortletPaul
PortletPaul
Flag of Australia image

what date is 07/08/2015?    (July or August?)


PLEASE stop treating mm/dd/yyyy as the ultimate and universal method of writing dates because it just isn't that.

Most of the world does NOT use that format and you cannot be certain your server will always recognize '09/21/2015' if it is expecting (for example) '21/09/2015'

nb: Scott is using the safest of all 'date literals' for SQL Server (YYYYMMDD) which will be recognized regardless of server settings

no points pl.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo