Solved

Converting DB date in text format to date

Posted on 2006-06-20
7
558 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
  • 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:
bhess1 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 32
VB.net and sql server 4 36
Sql Query 6 67
SQL Server Connection String through a VPN 8 26
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

810 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