Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting DB date in text format to date

Posted on 2006-06-20
7
Medium Priority
?
599 Views
Last Modified: 2012-05-05
I have a database in which the date fields were converted to text ( I inheirited the database). Now I would like to query the database, but the dates are stuck in text format. I need to convert them so I can compare date ranges in a query.

I'm using SQL Query Analyzer to test the SQL query string, and I get this message:

Server: Msg 306, Level 16, State 1, Line 3
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

Is it possible to simply cast the text dates to temporarily make them dates for query purposes?

Please provite a simple SQL query for this.

Thanks!
Kittrick
0
Comment
Question by:Kittrick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16947556
>>Is it possible to simply cast the text dates to temporarily make them dates for query purposes?<<
Yes, but only if the format is well defined.  For example, assuming mm/dd/yyyy than the following should work:

CONVERT(datetime, CAST(YourDateColumn as varchar(10), 101)
0
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 1000 total points
ID: 16947563
Unless you must leave the dates as text for some operational reason (e.g. other applications would break), I would convert the fields to varchar - things will be much cleaner then, since Text fields can only be converted to Varchar fields.

ALTER TABLE MyTable ALTER COLUMN DateAsText Varchar(<however long the date text actually is which depends on format>)

Otherwise, you will need to have a conversion like:

SELECT Cast(Cast(DateAsText As Varchar(100)) As DateTime) As DateAsDate
FROM MyTable

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16947568
Oops, that should have been:
CONVERT(datetime, CAST(YourDateColumn as varchar(10)), 101)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:Kittrick
ID: 16947573
Thanks for your reply acperkins. How would I use it in the SQL query?

Here's my SQL statement:

SELECT ID, Start_date FROM database WHERE start_date>=1/1/2006

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16947578
If there is any chance that YourDateColumn is not valid than you will have to do something like this:

CASE
      WHEN ISDATE(CAST(@YourDateColumn as varchar(10))) = 1 THEN CONVERT(datetime, CAST(@YourDateColumn as varchar(10)), 101)
      ELSE Null
END
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 1000 total points
ID: 16947585
Assuming they are all valid dates:

SELECT ID, Start_date
FROM database
WHERE CONVERT(datetime, CAST(start_date as varchar(10)), 101) >= '2006-01-01'
0
 
LVL 4

Author Comment

by:Kittrick
ID: 16947637
Thanks Bhess and acperkins! Both solutions worked, so I split the points evenly.

Kittrick
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question