dynamic query throwing convertion error

  • declare @a varchar(50)

    declare @b-2 varchar(20)

    set @b-2=(select gender from table where Cov='101')

    set @a=@b + convert(datetime,'24/08/2010',103)

    select CoveNoteNo from tableccr where Riskstart=@a

    the above query i m using to fetch data frm a table in database . now in my table the

    date is stored as 2010-08-24 00:00:00.000 and the datatype is datetime.

    now when i run this query i m getting error:

    Msg 241, Level 16, State 1, Line 4

    Conversion failed when converting datetime from character string.

    i know that i m getting this error because i am trying to concatonate a datetime value to

    varchar, but i want it in this fashion only.

    i want this same query to run beceause i m using same type of query in other program.

    i want a right query .

  • try this:

    declare @a varchar(50)

    declare @b-2 varchar(20)

    set @b-2=(select gender from table where Cov='101')

    set @a='''' + @b-2 + ''''' OR CAST ( ' + convert(datetime,'24/08/2010',103) + ' AS DATETIME )'

  • Use This :

    SET @a = @b-2 + CAST (convert(datetime,'24/08/2010',103) AS VARCHAR(50))

  • this has eliminated the error but now no data is comming .

  • because u are concatenating Gender with the Date and using this to match RiskStart..

    Your @a+@b results in Riskstart = 'M2010-08-25 00:00:00.000'.. is it wat you have it in RiskStart column ??

  • Or else you can try this

    declare @a varchar(50)

    declare @b-2 varchar(20)

    set @b-2='M'

    set @a=@b + convert(varchar(50),'24/08/2010',101)

    select @a

    Abhijit - http://abhijitmore.wordpress.com

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply