Solved

Converting DB date in text format to date

Posted on 2006-06-20
7
582 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 250 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 250 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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

695 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