当前位置: 首页 > 知识库问答 >
问题:

在没有UDF和CLR的情况下,从T-SQL中的字符串中修剪所有空白字符的好方法是什么?

温凯
2023-03-14

的。NET函数字符串。trim修剪了一组相当广泛的空白字符。如何以最好的方式模拟这种精确的行为SQL?

LTRIMRTRIM仅修剪空格字符,这是不够的。

这对于SQLCLR来说很容易,但这并不总是可能的,而且调用成本也相当高。因此,我希望避免使用SQLCLR。

此外,标量UDF强制执行串行计划,并且调用速度很慢。所以它可能也不应该是标量UDF。

鉴于这些限制,最好的方法是什么?

共有2个答案

逄兴昌
2023-03-14

我很想看看是否有人找到了通用的SQL解决方案。

我能想到的最好方法是一个简单的替换函数:

SELECT MyString = LEFT(MyString, LEN(RTRIM(REPLACE(REPLACE(REPLACE(MyString COLLATE Latin1_General_100_BIN2, NCHAR(9), ' '), NCHAR(12), ' '), NCHAR(13), ' ')))) AS RTrimmed

SELECT MyString = RIGHT(MyString, LEN(LTRIM(REPLACE(REPLACE(REPLACE(MyString COLLATE Latin1_General_100_BIN2, NCHAR(9), ' '), NCHAR(12), ' '), NCHAR(13), ' ')))) AS LTrimmed

您可以在此处获取当前空白字符列表:

http://unicode.org/charts/uca/chart_Whitespace.html

或者,为了向您自己证明这一点,您可以将所有字符的列表从SQL Server导出到Excel之类的文件中,清除这些字符,然后将它们重新导入。删除的是空白。

颛孙轩昂
2023-03-14

此代码提供了一个模式,您可以扩展该模式来处理您选择的修改后的LTRIM的空白。

declare @Tab as NVarChar(1) = NChar( 9 );
declare @Space as NVarChar(1) = NChar( 32 );

declare @Samples as Table ( String NVarChar(16) );
insert into @Samples ( String ) values
  ( 'Foo' ),
  ( @Tab + 'Foo' ),
  ( @Space + 'Foo' ),
  ( @Space + @Tab + 'Foo' ),
  ( @Tab + @Space + 'Foo' );
select String, Len( String ) as [Length], PatIndex( '%[^' + @Tab + @Space + ']%', String ) - 1 as [WhitespaceCount]
  from @Samples;

REVERSE函数可用于实现RTRIM的修改版本。

更新:以下代码使用中使用的空白字符列表。NET框架4。它还具有LEN不计算尾随空格的功能。

declare @Tab as NVarChar(1) = NChar( 9 );
declare @Space as NVarChar(1) = NChar( 32 );

declare @Samples as Table ( String NVarChar(16) );
insert into @Samples ( String ) values
  ( 'Foo' ),
  ( @Tab + 'Foo' ),
  ( @Space + 'Foo' ),
  ( @Space + @Tab + 'Foo' ),
  ( @Tab + @Space + 'Foo' ),
  ( @Tab + 'Foo' + @Space ),
  ( @Space + 'Foo' + @Tab ),
  ( @Space + @Tab + 'Foo' + @Tab + @Space ),
  ( @Tab + @Space + 'Foo' + @Space + @Tab ),
  ( 'Foo' + @Tab ),
  ( NULL ),
  ( '           ' ),
  ( @Space + NULL + @Tab + @Tab ),
  ( '' ),
  ( 'Hello world!' );

declare @WhitespacePattern as NVarChar(100) = N'%[^' +
  NChar( 0x0020 ) + NChar( 0x00A0 ) + NChar( 0x1680 ) + NChar( 0x2000 ) +
  NChar( 0x2001 ) + NChar( 0x2002 ) + NChar( 0x2003 ) + NChar( 0x2004 ) +
  NChar( 0x2005 ) + NChar( 0x2006 ) + NChar( 0x2007 ) + NChar( 0x2008 ) +
  NChar( 0x2009 ) + NChar( 0x200A ) + NChar( 0x202F ) + NChar( 0x205F ) +
  NChar( 0x3000 ) + NChar( 0x2028 ) + NChar( 0x2029 ) + NChar( 0x0009 ) +
  NChar( 0x000A ) + NChar( 0x000B ) + NChar( 0x000C ) + NChar( 0x000D ) +
  NChar( 0x0085 ) + N']%';
-- NB: The   Len   function does not count trailing spaces.
--     Use   DataLength   instead.
with AnalyzedSamples as (
  select String, DataLength( String ) / DataLength( NChar( 42 ) ) as [StringLength],
    PatIndex( @WhitespacePattern, String ) - 1 as [LeftWhitespace],
    PatIndex( @WhitespacePattern, Reverse( String ) ) - 1 as [RightWhitespace]
  from @Samples ),
  TrimmedSamples as (
  select String, StringLength, [LeftWhitespace], [RightWhitespace],
    case
      when String is NULL then NULL
      when LeftWhitespace = -1 then N''
      else Substring( String, LeftWhitespace + 1, StringLength - LeftWhitespace )
      end as [LTrim],
    case
      when String is NULL then NULL
      when RightWhitespace = -1 then N''
      else Reverse( Substring( Reverse( String ), RightWhitespace + 1, StringLength - RightWhitespace ) )
      end as [RTrim],
    case
      when String is NULL then NULL
      when LeftWhitespace = -1 then N''
      else Substring( String, LeftWhitespace + 1, StringLength - LeftWhitespace - RightWhitespace )
      end as [Trim]
    from AnalyzedSamples )
  select N'"' + String + N'"' as [String], StringLength, [LeftWhitespace], [RightWhitespace],
    N'"' + [LTrim] + N'"' as [LTrim], DataLength( [LTRIM] ) / DataLength( NChar( 42 ) ) as [LTrimLength],
    N'"' + [RTrim] + N'"' as [RTrim], DataLength( [RTRIM] ) / DataLength( NChar( 42 ) ) as [RTrimLength],
    N'"' + [Trim] + N'"' as [Trim], DataLength( [TRIM] ) / DataLength( NChar( 42 ) ) as [TrimLength]
    from TrimmedSamples;
 类似资料:
  • 问题内容: 与该方法等效的 JavaScript 是什么: C#仅在字符串的 开头 和 结尾 处修剪所选字符! 问题答案: 一行就足够了: 在功能上:

  • 问题内容: 下面程序的输出: 是: 然而 如何从字符串的开头和结尾删除换行符(Java)? 否则说。 我想念什么? 问题答案: 既然是一成不变的 不会更改基础值,它会返回一个没有开头和结尾空格字符的新值。您需要替换参考

  • 问题内容: 在Java中,我这样做是为了修剪字符串: 输出为: 作品。但是我想知道是否通过给自己分配一个变量来做正确的事情。我不想通过创建另一个变量并将调整后的值分配给它来浪费资源。我想就地进行修剪。 那我这样做对吗? 问题答案: 您做对了。从文档中: 字符串是常量;它们的值创建后无法更改。字符串缓冲区支持可变字符串。由于String对象是不可变的,因此可以共享它们。 同样从文档中: 修剪 公共字

  • 本文向大家介绍在C ++中查找所有好的字符串,包括了在C ++中查找所有好的字符串的使用技巧和注意事项,需要的朋友参考一下 假设我们有两个字符串s1和s2。这些字符串的大小为n,我们还有另一个字符串称为evil。我们必须找到好字符串的数量。 如果字符串的大小为n,则按字母顺序大于或等于s1,按字母顺序小于或等于s2,并且作为子字符串不包含邪恶,则该字符串称为良。答案可能非常大,因此请以10 ^ 9

  • 我有一个包含路径的字符串 并且我想修剪前导和后导的和。Python3中的最佳实践是什么? 目前我正在使用 两个问题: 这是在Python 3中修剪特定字符的最佳修剪函数吗? 在Python 3中是否有针对此类操作的特定路径函数,这样我就不必手动设置分隔符了?