Solved

Converting DB date in text format to date

Posted on 2006-06-20
7
564 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

861 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