Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Syntax error converting datetime from character string.

Syntax error converting datetime from character string.

Scheduled Pinned Locked Moved Database
databasehelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rujuc
    wrote on last edited by
    #1

    I ve written a stored procedure to fetch data from a table. Here i m getting an error "Syntax error converting datetime from character string.". Please guide me. ////////////////////////////////////////////////////////////////////////// CREATE PROCEDURE ic_get_processeddata ( @frmDate DateTime, @toDate DateTime, @empType varchar(20), @ein varchar(15) ) as declare @sqlStr as varchar(255) set @sqlStr ='' set @sqlStr='select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate='+@frmDate+' and toDate='+@toDate if(@empType !='') begin set @sqlStr = @sqlStr + ' and empType=' + @empType end if(@ein !='') begin set @sqlStr = @sqlStr + ' and EIN='+@ein end set @sqlStr = @sqlStr + (' order by empName,empType') print(@sqlStr) exec @sqlStr GO thanx in advance

    M S 2 Replies Last reply
    0
    • R rujuc

      I ve written a stored procedure to fetch data from a table. Here i m getting an error "Syntax error converting datetime from character string.". Please guide me. ////////////////////////////////////////////////////////////////////////// CREATE PROCEDURE ic_get_processeddata ( @frmDate DateTime, @toDate DateTime, @empType varchar(20), @ein varchar(15) ) as declare @sqlStr as varchar(255) set @sqlStr ='' set @sqlStr='select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate='+@frmDate+' and toDate='+@toDate if(@empType !='') begin set @sqlStr = @sqlStr + ' and empType=' + @empType end if(@ein !='') begin set @sqlStr = @sqlStr + ' and EIN='+@ein end set @sqlStr = @sqlStr + (' order by empName,empType') print(@sqlStr) exec @sqlStr GO thanx in advance

      M Offline
      M Offline
      Marek Grzenkowicz
      wrote on last edited by
      #2

      Use CAST or CONVERT to convert @frmDate and @toDate to string, before you try to concatenate them.

      1 Reply Last reply
      0
      • R rujuc

        I ve written a stored procedure to fetch data from a table. Here i m getting an error "Syntax error converting datetime from character string.". Please guide me. ////////////////////////////////////////////////////////////////////////// CREATE PROCEDURE ic_get_processeddata ( @frmDate DateTime, @toDate DateTime, @empType varchar(20), @ein varchar(15) ) as declare @sqlStr as varchar(255) set @sqlStr ='' set @sqlStr='select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate='+@frmDate+' and toDate='+@toDate if(@empType !='') begin set @sqlStr = @sqlStr + ' and empType=' + @empType end if(@ein !='') begin set @sqlStr = @sqlStr + ' and EIN='+@ein end set @sqlStr = @sqlStr + (' order by empName,empType') print(@sqlStr) exec @sqlStr GO thanx in advance

        S Offline
        S Offline
        szukuro
        wrote on last edited by
        #3

        Wouldn't this be a better solution: CREATE PROCEDURE ic_get_processeddata ( @frmDate DateTime, @toDate DateTime, @empType varchar(20), @ein varchar(15) ) AS SELECT EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus FROM ic_process WHERE frmDate = @frmDate AND toDate = @toDate AND (empType = @empType OR @empType = '') AND (EIN = @ein OR @ein='') ORDER by empName,empType This way the @toDate remains a datetime type.

        R 1 Reply Last reply
        0
        • S szukuro

          Wouldn't this be a better solution: CREATE PROCEDURE ic_get_processeddata ( @frmDate DateTime, @toDate DateTime, @empType varchar(20), @ein varchar(15) ) AS SELECT EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus FROM ic_process WHERE frmDate = @frmDate AND toDate = @toDate AND (empType = @empType OR @empType = '') AND (EIN = @ein OR @ein='') ORDER by empName,empType This way the @toDate remains a datetime type.

          R Offline
          R Offline
          rujuc
          wrote on last edited by
          #4

          Hi szukuro Thanx for your suggestion. But your tric was not working for my solution. Some how i modified my stored procedure. Is this the correct way?? can we optimized it?? CREATE PROCEDURE ic_get_processeddata ( @frmDate DateTime, @toDate DateTime, @empType varchar(20), @ein varchar(15) ) as if(@empType !='') and (@ein !='') begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate and empType=@empType and ein=@ein order by empType,empName end else if (@empType !='') and (@ein ='') begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate and empType=@empType order by empName,empType end else if(@ein !='') and (@empType ='') begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate and ein=@ein order by empName,empType end else begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate order by empName,empType end GO

          S 1 Reply Last reply
          0
          • R rujuc

            Hi szukuro Thanx for your suggestion. But your tric was not working for my solution. Some how i modified my stored procedure. Is this the correct way?? can we optimized it?? CREATE PROCEDURE ic_get_processeddata ( @frmDate DateTime, @toDate DateTime, @empType varchar(20), @ein varchar(15) ) as if(@empType !='') and (@ein !='') begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate and empType=@empType and ein=@ein order by empType,empName end else if (@empType !='') and (@ein ='') begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate and empType=@empType order by empName,empType end else if(@ein !='') and (@empType ='') begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate and ein=@ein order by empName,empType end else begin select EIN,empName,empType,I2C,callAnswered,I2CPer,USLeaves,CBF,SOS,SOI,FCR,Bonus from ic_process where frmDate=@frmDate and toDate=@toDate order by empName,empType end GO

            S Offline
            S Offline
            szukuro
            wrote on last edited by
            #5

            Why didn't it work? What was the problem?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups