Escape Character in SQL Server

前端 未结 9 1406
时光取名叫无心
时光取名叫无心 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条回答
  • 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

    0 讨论(0)
  • 2020-11-29 03:57

    You could use the **\** character before the value you want to escape e.g insert into msglog(recipient) values('Mr. O\'riely') select * from msglog where recipient = 'Mr. O\'riely'

    0 讨论(0)
  • 2020-11-29 03:59
    WHERE username LIKE '%[_]d';            -- @Lasse solution
    WHERE username LIKE '%$_d' ESCAPE '$';
    WHERE username LIKE '%^_d' ESCAPE '^';
    

    FROM: SQL Server Escape an Underscore

    0 讨论(0)
提交回复
热议问题