sql - stored procedure to remove quotes -


i trying write stored procedure removed leading , trailing quotes arbitrary table , column. keep getting error saying table name isn't declared.

here sp

create table [dbo].[test] (id nvarchar(20)) insert dbo.test values ('"07617966004766"') go  create procedure sp_stripdoublequotes      @tablename sysname,     @columnname sysname begin     set nocount on;      update @tablename     set @columnname = substring(@columnname, 2, len(@columnname))     left(@columnname, 1) = '"'      update @tablename     set @columnname = substring(@columnname, 1, len(@columnname)-1)     right(@columnname, 1) = '"'  end go exec [dbo].[sp_stripdoublequotes] n'[dbo].[test]', n'[id]'    select * test 

here link fiddle: link fiddle

create procedure sp_stripdoublequotes      @tablename sysname,     @columnname sysname,     @sql varchar(max) begin     set nocount on;         set @sql =      'update ' + '[' + @tablename +']' +     'set' + '[' + @columnname +']' +'= substring(' +'[' + @columnname +']' +', 2, len(' +'[' + @columnname +']' +'))     left('  + '[' + @columnname +']' +', 1) = '+'''"'''     --print(@sql)     exec (@sql)      set @sql =     'update ' + '[' + @tablename +']' +     'set' + '[' + @columnname +']' +'= substring(' + '[' + @columnname +']' +', 1, len(' + '[' + @columnname + ']' +')-1)     right(' + '[' + @columnname +']' +', 1) = '+'''"'''     --print(@sql)     exec (@sql) end go exec [dbo].[sp_stripdoublequotes] n'test', n'id'  -- exec [dbo].[sp_stripdoublequotes] n'[dbo].[test]', n'[id]'  

updated 2nd: added [] wrap table , column incase table , column name have whitespace in them. @sean lange , @richard

updated 3rd: @[benjamin moskovits] (xd) mentioned, if hard coded brackets, correct execute command exec [dbo].[sp_stripdoublequotes] n'test', n'id'. try add or remove brackets , print see whether syntax correct before executing it.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -