Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Conversion failed when converting datetime from character string - Error.... Expand / Collapse
Author
Message
Posted Tuesday, July 6, 2010 4:07 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 3, 2011 12:19 PM
Points: 3, Visits: 23
I'm getting really yanked about this...this worked at one time but returns the following error...
"Conversion failed when converting datetime from character string"

I'm writing a Stored Procedure where i'm retrieving data from a table and inserting into other tables. There are other columns as well, but listed only the ones with trouble..

Appreciate your responses...

DECLARE
@vchtstsirdt nvarchar(30),
@vchtstsdate nvarchar(30),
@vchtstPDTDt nvarchar(30),
@vchtstsaddt nvarchar(30)

-- declare the cursor

DECLARE SSC CURSOR FOR
select "Column List"
from tst_tab

OPEN SSC

FETCH SSC INTO @

-- start the main processing loop.

WHILE @@Fetch_Status = 0
BEGIN

IF @vchtstsirdt IS NULL
BEGIN
SET @vchtstsirdt = NULL
END
Else
SET @vchtstsirdt = CONVERT(datetime, @vchtstsirdt)

IF @vchtstsdate IS NULL
BEGIN
SET @vchtstsdate = NULL
END
Else
SET @vchtstsdate = CONVERT(datetime, @vchtstsdate)

IF @vchtstPDTDt IS NULL
BEGIN
SET @vchtstPDTDt = NULL
END
Else
SET @vchtstPDTDt = CONVERT(datetime, @vchtstPDTDt)

IF @vchtstsaddt IS NULL
BEGIN
SET @vchtstsaddt = NULL
END
Else
SET @vchtstsaddt = CONVERT(datetime, @vchtstsaddt)

/* Tried the following as well*/


/*IF @vchtstsirdt IS NULL
SET @vchtstsirdt = CONVERT(datetime, ISNULL(@vchtstsirdt,''))
Else
SET @vchtstsirdt = CONVERT(datetime, @vchtstsirdt)

IF @vchtstsdate IS NULL
SET @vchtstsdate = CONVERT(datetime, ISNULL(@vchtstsdate,''))
Else
SET @vchtstsdate = CONVERT(datetime, @vchtstsdate)

IF @vchtstPDTDt IS NULL
SET @vchtstPDTDt = CONVERT(datetime, ISNULL(@vchtstPDTDt,''))
Else
SET @vchtstPDTDt = CONVERT(datetime, @vchtstPDTDt)

IF @vchtstsaddt IS NULL
SET @vchtstsaddt = CONVERT(datetime, ISNULL(@vchtstsaddt,''))
Else
SET @vchtstsaddt = CONVERT(datetime, @vchtstsaddt)
*/

Thx in Advance...
Post #948226
Posted Tuesday, July 6, 2010 4:50 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 18, 2014 10:48 AM
Points: 1,935, Visits: 10,956
...
IF @vchtstsaddt IS NULL
BEGIN
SET @vchtstsaddt = NULL
END
Else
SET @vchtstsaddt = CONVERT(datetime, @vchtstsaddt)
...

This makes little or no sense. If the variable already is null, why set it to null?

On the other hand, each of the variables is declared as a nvarchar(30) data type. Most likely, one of those does not actually contain a value that can be converted to a datetime value. I would check the actual contents of those values to see if you have bogus values. In any event, you're then assigning the datetime converted value back to the nvarchar variable. What is then done with these values?


Rob Schripsema
Accelitec, Inc
Post #948238
Posted Wednesday, July 7, 2010 1:56 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
DP-721271 (7/6/2010)

...
/* Tried the following as well*/

IF @vchtstsirdt IS NULL
SET @vchtstsirdt = CONVERT(datetime, ISNULL(@vchtstsirdt,''))
Else
SET @vchtstsirdt = CONVERT(datetime, @vchtstsirdt)
...


That is also code noodles, but with different result.
NULL values will be converted to 1 Jan 1900

And there is very simple explanation for the error you getting:
Conversion failes due to some of values yuo have cannot be converted to datetime
Looking your code I also suspect that you don't really need a cursor .



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #948369
Posted Wednesday, July 7, 2010 5:13 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 3, 2011 12:19 PM
Points: 3, Visits: 23
Thanks for the responses....trying to check the data...

Post #948910
Posted Wednesday, July 7, 2010 5:26 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 18, 2014 10:48 AM
Points: 1,935, Visits: 10,956
You can look for rows where there may be invalid dates by using the IsDate function in TSQL.

Select * from MyTable where IsDate(MyColumn) = 0

Note, however, that there are exceptions where IsDate returns true (1) even though you might not think of it as a valid date. For example:

Select IsDate('2010')

returns '1'.


Rob Schripsema
Accelitec, Inc
Post #948916
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse