2

I am trying to replace a CRLF character in a table in SQL Server. The statement for one column works and goes like this:

select REPLACE(REPLACE(col_name,char(13),''), char(10), '') from table_name

Now I would like to repeat this for every column in my table. I have the following script which is not working:

Declare @sql varchar(max) = ''

select @sql = @sql + 'select [' + c.name + '] REPLACE(REPLACE(' + c.name + ', char(13),''), char(10), '') from [' + t.name + ']; ' 
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
where t.name = table_name

EXEC (@sql)

Unfortunately this doesn't work and I get the following error:

Msg 102, Level 15, State 1, Line 24
Incorrect syntax near 'REPLACE'.
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near 'REPLACE'.
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near 'REPLACE'.
Msg 102, Level 15, State 1
1
  • Just noticed, you use select, but if you want to get rid of that you need update. Add this to my answer. Commented Sep 30, 2016 at 12:54

3 Answers 3

2

Two things; as noted by the other answer there needs to be a comma after the initial [c.name]. Also, to get a single quote inside the string, you need two single quotes. Hence, to get two single quotes together, you need four single quotes.

select @sql = @sql + 'select [' + c.name + '], REPLACE(REPLACE(' + c.name + ', char(13),''''), char(10), '''') from [' + t.name + ']; ' 
    from sys.columns c
    inner join sys.tables t on c.object_id = t.object_id
    where t.name = 'table_name'

    exec (@sql)

NB: print(@sql) is a great help in debugging such problems!

Sign up to request clarification or add additional context in comments.

1 Comment

You have inconsistent usage of square brackets. And to be honest you shouldn't use them in this type of situation. You should eliminate the brackets and instead use QUOTENAME. When you hard code brackets like this if there is a bracket in the name it will crash. And it important enough to use them once you should be consistent. Still this is a good answer and worth of a +1.
2

Add quotes, remove first c.name or add comma, instead of adding [] use QUOTENAME:

Declare @sql varchar(max) = ''

select @sql = @sql + 'select ' + QUOTENAME(c.name) + ', REPLACE(REPLACE(' + QUOTENAME(c.[name]) + ', char(13),''''), char(10), '''') from ' + QUOTENAME(t.[name]) + '; ' 
from sys.columns c
inner join sys.tables t 
    on c.object_id = t.object_id
where t.[name] = 'table_name'

EXEC (@sql)

EDIT

If you need to update change @sql part:

select @sql = @sql + 'UPDATE ' + QUOTENAME(t.[name]) + ' SET ' + QUOTENAME(c.name) + ' = REPLACE(REPLACE(' + QUOTENAME(c.[name]) + ', char(13),''''), char(10), '''') ; ' 

Comments

1

You can try this way, variable is not set with replaced value, quotes are not double-quoted...

Declare @sql varchar(max) = ''

select @sql = @sql + 'select [' + c.name + '] = REPLACE(REPLACE(' + c.name + ', char(13),''''), char(10), '''') from [' + t.name + ']; ' 
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
where t.name = 'commasep'

select @sql

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.