x

Error when converting datetime from character string

I've googled this and plenty about this in the web but I am not sure why my string is not converting to datetime.

Any ideas?

 SELECT gi.ITEM_NUM
 ,i.DSCR
  ,CAST(CASE WHEN (cast(MAX(CRTE_DTS) as datetime)) > DATEADD(d,-7,GETDATE()) THEN 1 ELSE 0 END  AS BIT) AS RECENT_ADD
 FROM DAMART.dbo.T_x_ITEM AS gi
 INNER JOIN DB.dbo.T_ITEM AS i
 ON i.ITEM_NUM = gi.ITEM_NUM
more ▼

asked Feb 24, 2011 at 03:30 PM in Default

avatar image

siera_gld
1k 82 88 93

Here is the actual date stored in the db

Oct 4 2007 2:41PM

Feb 24, 2011 at 03:43 PM siera_gld
(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Is that the only row in the table?

If not, it's quite likely there is at least one non-date string value stored in the CRTE_DTS column.

Try this to search for offending values:

 SELECT CRTE_DTS 
 FROM DAMART.dbo.T_x_ITEM AS gi
 INNER JOIN DB.dbo.T_ITEM AS i
 ON i.ITEM_NUM = gi.ITEM_NUM 
 WHERE ISDATE(CRTE_DTS) = 0
more ▼

answered Feb 24, 2011 at 03:57 PM

avatar image

KenJ
24.8k 3 10 19

UR Awesome

Feb 24, 2011 at 04:06 PM siera_gld
(comments are locked)
10|1200 characters needed characters left

How is the date represented in the database?

What's the result of just running "SELECT MAX(CRTE_DTS) FROM... (wherever)"

I've just tried the following, and it's worked quite happily, so I'm reckoning there might be some extra data that's floating to the top of the "MAX" calculation...

 DECLARE @t VARCHAR(20)
 SELECT @t = 'Oct 4 2007 2:41PM'
 SELECT cast(@t as datetime)

... either that, or you've got some strange localisation settings.

more ▼

answered Feb 24, 2011 at 03:50 PM

avatar image

ThomasRushton ♦♦
40.3k 20 49 53

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x81
x42
x37

asked: Feb 24, 2011 at 03:30 PM

Seen: 1134 times

Last Updated: Feb 24, 2011 at 03:45 PM

Copyright 2016 Redgate Software. Privacy Policy