【问题标题】:Add quotes around alpha characters in alphanumeric strings using T-SQL which can begin end with alpha characters?使用可以以字母字符开头的 T-SQL 在字母数字字符串中的字母字符周围添加引号?
【发布时间】:2018-05-05 09:30:55
【问题描述】:

我想在字母数字字符串中的字母周围添加引号。例如:如果我的字符串是 8AB8973,则预期的输出是 8'AB'8973。没有特定的模式会出现数字和字符。我在 StackOverFlow 上发布了一个类似的问题,有人向我提供了适用于上述示例的以下代码。但以下字符串失败。我已经提到了示例、预期输出以及我使用下面的代码收到的内容。 例如 1:字符串的第一个字符是 alpha = PAYGA0102。预期:'PAYGA'0102。收到的结果:PAYGA'0102 例如 2:字符串的最后一个字符是 alpha = 00086K。预期:00086'K'。收到的结果:00086'K 例如 3:所有都是字符 = ADEP 预期 = 'ADEP'。收到的结果:ADEP。

我需要调整以下代码以包含上述所有条件。稍后我将循环代码,但我需要修复主 patindex 代码以满足上述所有条件。下面的代码是重复的,只是为了显示我得到的不同结果。

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;

SET @string = 'PAYGA0102'

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;

  SET @string = '00086K'

  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;

  SET @string = 'ADEP'

  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; 

【问题讨论】:

  • 为了完整性,可能有多个字符组或只有一个字符组。例如:AAA123BBB --> 'AAA'123'BBB' 是有效的情况吗?
  • 是的,这完全是一个有效的案例。
  • 提示:使用适当的软件(MySQL、Oracle、DB2 等)和版本标记数据库问题很有帮助,例如sql-server-2014。语法和功能的差异通常会影响答案。请注意,tsql 缩小了选择范围,但没有指定数据库。

标签: sql string tsql quotes alphanumeric


【解决方案1】:

拥有patternSplitCM 的副本很容易。并且由于该函数完全基于集合,因此您将获得比任何基于 RBAR 的替代方案更好的性能。

针对变量的解决方案

declare @string varchar(100) = 'ABC123ZZ888X';

select newstring = replace((
  select ''''+item+''''
  from dbo.patternSplitCM(@string, '[0-9]')
  order by ItemNumber
  for xml path('')),'''''','''');

对着桌子

-- sample data
declare @yourtable table (someid int identity, string varchar(100));
insert @yourtable values ('ABC'),('123X'),('BBBCC44455555ZZ6P'),('CC923PP5522'),(NULL);

-- the only difference here is that I replaced @string with t.string
select someid, oldstring = string, newstring 
from @yourtable t
cross apply 
( select newstring = replace((
    select ''''+item+''''
    from dbo.patternSplitCM(t.string, '[0-9]')
    order by ItemNumber
    for xml path('')),'''''','''')) addquotes

结果

someid   oldstring           newstring
-------- ------------------- -----------------------
1        ABC                 'ABC'
2        123X                '123'X'
3        BBBCC44455555ZZ6P   'BBBCC'44455555'ZZ'6'P'
4        CC923PP5522         'CC'923'PP'5522'
5        NULL                NULL

【讨论】:

  • 但是在这个字符串 123X 中,预期的输出让我得到 '123'X'。而它需要是 123'X'。我需要将第一个引号编辑为数字而不是字母。
猜你喜欢
  • 2018-04-16
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
相关资源
最近更新 更多