SQL 撇号
如何在 SQL 中生成包含空字符串的字符串: ' '
我尝试了以下操作:
声明@TEST NVARCHAR(50)、@COL1 NVARCHAR(50)、@COL2 NVARCHAR(50) 选择@COL1 = 'A',@COL2 = 'B'
SELECT @TEST = 'SELECT '' ['+ @COL1 + '], ''[' + @COL2+ ']'
选择@TEST
但字符串最终看起来喜欢:
选择'[A],'[B]
当它需要看起来像:
选择“[A]”、“[B]”
谢谢。
How can I generate a string in SQL which contains an empty string: ' '
I tried this:
DECLARE @TEST NVARCHAR(50), @COL1 NVARCHAR(50), @COL2 NVARCHAR(50)
SELECT @COL1 = 'A', @COL2 = 'B'SELECT @TEST = 'SELECT '' ['+ @COL1 + '], ''[' + @COL2+ ']'
SELECT @TEST
But the string ends up looking like:
SELECT ' [A], '[B]
When it needs to look like:
SELECT '' [A], ''[B]
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SQL 中字符串中的两个单引号被视为单个转义单引号,因此为了在输出中生成两个单引号,您需要在输入中添加 4,如下所示:
Two single quotes in a string in SQL is treated as a single escaped single quote, so in order to generate two in the output, you need to put 4 in the input, like so:
那么,快速的答案是:
''''
(将它们加倍)例如
SELECT 'XX''''XX'
→XX''XX
code>我就这样吧,因为“你为什么要这么做‽”部分让我非常紧张。
Well, the quick answer is:
''''
(double them up)e.g.
SELECT 'XX''''XX'
→XX''XX
I'll leave it at that, because the "why would you want to do that‽" part makes me very nervous.
您不需要连接字符串。
应该足够了。参数@COL1和@COL2将自动替换为实际值。
但是,您不能像这样动态声明列名称。上面的参数代表值。通常你会这样做:
如果你打算动态更改列名,那么你根本不会使用撇号:
You don't need to concatenate strings.
should be sufficient. The parameters @COL1 and @COL2 will be replaced by the actual values automatically.
However you cannot declare column names dynamically like this. The parameters on stand for values. Usually you would do something like this:
If you intend to change the column names dynamically, then you would not use apostrophes at all: