Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error converting character to smalldatetime type 1

Status
Not open for further replies.

CompCodeBoy

Programmer
Aug 18, 2005
33
US
Having errors wheyn writing the quer below in Query Analyzer

Declare @strSQL varchar(1200)
Declare @datestr smalldatetime

set @strSQL= 'select * from table1 where date_field > ''12/12/2004'''

EXEC (strSQL)

The above works just fine. date_field is of type smalldatetime in table1

How I can get the above to work by replacing 12/12/2004 with @datestr ??? I tried casting and much more. Nothing seems to work. Any ideas?
 
Here is two ways of doing this.

Code:
DECLARE 
    @strSQL  varchar(1200),
    @datestr smalldatetime

    SET @datestr = '12/12/2005'
    SET @strSQL= 'select * from Table1 where date_field > ''' + convert(varchar(10), @datestr, 101) + ''''

    --print @strSQL
    EXEC (@strSQL)

Or declaring the @datestr as varchar to get rid of the additional convert function.
Code:
DECLARE 
    @strSQL  varchar(1200),
    @datestr varchar(10)

    SET @datestr = '12/12/2005'
    SET @strSQL= 'select * from Table1 where date_field > ''' + @datestr + ''''

    --print @strSQL
    EXEC (@strSQL)

Regards,
AA
 
Both work. AA, thanks for your help. I really appreciate it. you're good ;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top