【发布时间】:2018-04-16 17:09:47
【问题描述】:
我想在字母数字字符串中的字母周围添加引号。例如:如果我的字符串是 8AB8973,则预期的输出是 8'AB'8973。没有特定的模式会出现数字和字符。我尝试运行在 StackOverflow 上找到的以下代码,但它在数字和字母之间添加了一个空格,当我尝试用引号替换空格时,查询需要永远运行。
DECLARE @position INT;
DECLARE @string VARCHAR(max);
SET @string = '9FX8173'
WHILE 1 = 1
BEGIN
SET @position = (SELECT Min(position)
FROM (VALUES (Patindex('%[^ 0-9][0-9]%', @string)),
(Patindex('%[0-9][^ 0-9]%', @string))) AS T(position)
WHERE T.position > 0);
IF @position IS NULL
BREAK;
SET @string = Stuff(@string, @position + 1, 0, ' ');
END
PRINT @string
【问题讨论】:
标签: sql-server tsql split quotes alphanumeric