July 6, 2010 at 4:07 pm
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...
July 6, 2010 at 4:50 pm
...
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.
July 7, 2010 at 1:56 am
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 :w00t:.
July 7, 2010 at 5:13 pm
Thanks for the responses....trying to check the data...
July 7, 2010 at 5:26 pm
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.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply