Solved

Converting DB date in text format to date

Posted on 2006-06-20
7
533 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamics crm 2011 8 46
Help with SQL Query 23 39
How to find duplicates in SQL Server 3 23
Slow SQL query 12 29
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now