How do I escape a single quote in SQL Server?

前端 未结 13 1852
情话喂你
情话喂你 2020-11-21 07:01

I\'m trying to insert some text data into a table in SQL Server 9.

The text includes a single quote(\').

How do I escape that?

相关标签:
13条回答
  • 2020-11-21 07:46

    Also another thing to be careful of is whether or not it is really stored as a classic ASCII ' (ASCII 27) or Unicode 2019 (which looks similar, but not the same).

    This isn't a big deal on inserts, but it can mean the world on selects and updates.
    If it's the unicode value then escaping the ' in a WHERE clause (e.g where blah = 'Workers''s Comp') will return like the value you are searching for isn't there if the ' in "Worker's Comp" is actually the unicode value.

    If your client application supports free-key, as well as copy and paste based input, it could be Unicode in some rows, and ASCII in others!

    A simple way to confirm this is by doing some kind of open ended query that will bring back the value you are searching for, and then copy and paste that into notepad++ or some other unicode supporting editor.

    The differing appearance between the ascii value and the unicode one should be obvious to the eyes, but if you lean towards the anal, it will show up as 27 (ascii) or 92 (unicode) in a hex editor.

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