SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Conversion failed when converting datetime from character string - Error....


Conversion failed when converting datetime from character string - Error....

Author
Message
DP-721271
DP-721271
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 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...
Rob Schripsema
Rob Schripsema
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2465 Visits: 11042

...
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
Propack, Inc.
Eugene Elutin
Eugene Elutin
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4982 Visits: 5478
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 :-D
Looking your code I also suspect that you don't really need a cursor w00t.

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

How to post your question to get the best and quick help
DP-721271
DP-721271
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 23
Thanks for the responses....trying to check the data...
Rob Schripsema
Rob Schripsema
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2465 Visits: 11042
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
Propack, Inc.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search