Results 1 to 3 of 3

Thread: Syntax error converting datetime from character string in dynamic SQL statement

  1. #1
    Join Date
    Nov 2002
    Posts
    231

    Syntax error converting datetime from character string in dynamic SQL statement

    Hi, I'm getting the
    "Syntax error converting datetime from character string."

    for the below query.
    I have some request like this query and need to know the correct sql statement for this to avoid this error.



    use northwind
    go
    declare @reqdate datetime
    ,@shipdate datetime
    set @reqdate='1996-05-05'
    set @shipdate='1996-06-05'
    declare @cmd varchar(1000)
    set @cmd='select * from orders where requiredate >='+@reqdate+' AND shipdate >='+@shipdate
    exec @cmd

  2. #2
    Join Date
    Mar 2005
    Location
    India
    Posts
    6

    syntax error converting datetime form character to string

    try the foll. script:

    use northwind
    go
    declare @reqdate datetime
    ,@shipdate datetime
    set @reqdate='1996-05-05'
    set @shipdate='1996-06-05'
    declare @cmd varchar(1000)
    set @cmd='select * from orders where requiredate >='+convert(varchar(11),@reqdate,121)+' AND shipdate >='+convert(varchar(11),@shipdate,121)
    exec @cmd

    in case of any queries, pl. reply with the table structure.

  3. #3
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    set quoted_identifier off
    use northwind
    go
    declare @reqdate datetime,@shipdate datetime
    set @reqdate='1996-05-05'
    set @shipdate='1996-06-05'
    declare @cmd varchar(1000)
    set @cmd='select * from orders where requireddate >="'+convert(varchar(23),@reqdate)+'" AND shippeddate >="'+convert(varchar(23),@shipdate)+'"'
    exec (@cmd)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •