Escape Character in SQL Server

前端 未结 9 1418
时光取名叫无心
时光取名叫无心 2020-11-29 03:22

I want to use quotation with escape character. How can I do?

I have received error in SQL Server

Unclosed quotation mark after the character strin

9条回答
  •  Happy的楠姐
    2020-11-29 03:57

    You need to just replace ' with '' inside your string

    SELECT colA, colB, colC
    FROM tableD
    WHERE colA = 'John''s Mobile'
    

    You can also use REPLACE(@name, '''', '''''') if generating the SQL dynamically

    If you want to escape inside a like statement then you need to use the ESCAPE syntax

    It's also worth mentioning that you're leaving yourself open to SQL injection attacks if you don't consider it. More info at Google or: http://it.toolbox.com/wiki/index.php/How_do_I_escape_single_quotes_in_SQL_queries%3F

提交回复
热议问题