Pass datetime variable to query as varchar

  • Declare @strStartTime varchar(50)

    select @strStartTime = '12/10/2010'

    select * from Elect_SubB1_Daily B1

    Where B1.Date_Stamp>= convert(datetime,''' + @strStartTime+ ''', 101)

    /* =======================================

    Getting this error when executed :

    Msg 241, Level 16, State 1, Line 5

    Msg 241, Level 16, State 1, Line 5

    Conversion failed when converting date and/or time from character string.

    ??

  • Declare @strStartTime varchar(50)

    select @strStartTime = '12/10/2010'

    SELECT CAST(@strStartTime as datetime)

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • Pops sorry!! I fixed it. I did not need to use the '+' in the query

    This works..

    Declare @strStartTime varchar(50)

    select @strStartTime = '12/10/2010'

    select * from Elect_SubB1_Daily B1

    Where B1.Date_Stamp>= convert(datetime, @strStartTime , 101)

Viewing 3 posts - 1 through 2 (of 2 total)

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