If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Convert Char to date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-25-03, 16:10
shadowvixen shadowvixen is offline
Registered User
 
Join Date: Aug 2003
Posts: 2
Convert Char to date

I am trying to convert a char column to a datetime in a SQL query. I have tried using CAST however I get the following message:
Syntax error converting datetime from character string

The date part of the query looks like this:
(CAST(date_opened_char AS datetime) >= '08/01/2003 00:00:00'

What am I doing wrong?

TIA!
SV
Reply With Quote
  #2 (permalink)  
Old 08-25-03, 16:15
jora jora is offline
Registered User
 
Join Date: Dec 2002
Location: Antwerp, Belgium
Posts: 227
in your date_opened_char is most probably a value (or more) that contains data that cannot be converted to a datetime value.
By the way, if you always compare to the 0:00 time, you can omit it in the query, because sql server defaults to midnight time.
__________________
Johan
Reply With Quote
  #3 (permalink)  
Old 08-25-03, 16:17
shadowvixen shadowvixen is offline
Registered User
 
Join Date: Aug 2003
Posts: 2
Below is a value that is in the column:

05/13/02 19:20:45
Reply With Quote
  #4 (permalink)  
Old 08-25-03, 16:22
jora jora is offline
Registered User
 
Join Date: Dec 2002
Location: Antwerp, Belgium
Posts: 227
if i execute this statement
Code:
select (CAST('05/13/02 19:20:45' AS datetime))
then there is no problem. I am not saying that all cannot be converted, but it only takes one to receive an error. Perhaps you can create a stored procedure that browses through the data with a cursor and using the @@error variable you can determine which one(s) create(s) the error.

Also, you have one bracket short after the 'as datetime' part, but this could be of the copy pasting.
__________________
Johan

Last edited by jora; 08-25-03 at 16:26.
Reply With Quote
  #5 (permalink)  
Old 08-25-03, 16:27
jora jora is offline
Registered User
 
Join Date: Dec 2002
Location: Antwerp, Belgium
Posts: 227
how do you check that the data in the char column is correct? If you don't check, a user could have mistaken with the data entry and put the '/' the other way around, like '\'
__________________
Johan
Reply With Quote
  #6 (permalink)  
Old 08-25-03, 17:49
rdjabarov rdjabarov is offline
Registered User
 
Join Date: Jul 2003
Location: San Antonio, TX
Posts: 3,611
use isdate(...) :

CAST(case isdate(date_opened_char) when 0 then '01/01/1900' else date_opened_char end AS datetime) >= '08/01/2003 00:00:00'
Reply With Quote
  #7 (permalink)  
Old 08-25-03, 19:00
nigelrivett nigelrivett is offline
Registered User
 
Join Date: Oct 2001
Location: England
Posts: 426
for that date format you need

convert(datetime, dte, 101)

to beenvironment independant.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On