atr*_*lla 33 sql t-sql sql-server string sql-server-2008
如何在列中找到具有尾随空格的所有列值?对于领先的空间,它只是
select col from table where substring(col,1,1) = ' ';
Run Code Online (Sandbox Code Playgroud)
Jax*_*ian 60
您可以在以下位置找到尾随空格LIKE:
SELECT col FROM tbl WHERE col LIKE '% '
Run Code Online (Sandbox Code Playgroud)
SQL Server 2005:
select col from tbl where right(col, 1) = ' '
Run Code Online (Sandbox Code Playgroud)
作为演示:
select
case when right('said Fred', 1) = ' ' then 1 else 0 end as NoTrail,
case when right('said Fred ', 1) = ' ' then 1 else 0 end as WithTrail
Run Code Online (Sandbox Code Playgroud)
回报
NoTrail WithTrail
0 1
Run Code Online (Sandbox Code Playgroud)
一个非常简单的方法是使用 LEN 函数。LEN 将修剪尾随空格,但不会修剪前面的空格,因此如果您的 LEN() 与 LEN(REVERSE()) 不同,您将获得带有尾随空格的所有行:
select col from table where LEN(col) <> LEN(REVERSE(col));
Run Code Online (Sandbox Code Playgroud)
这也可以用来计算有多少空间可以用于更高级的逻辑。
小智 6
这对我有用:
select * from table_name where column_name not like RTRIM(column_name)
Run Code Online (Sandbox Code Playgroud)
这将为您提供所有带有尾随空格的记录。
如果您想获取具有前导或尾随空格的记录,则可以使用以下命令:
select * from table_name where column_name not like LTRIM(RTRIM(column_name))
Run Code Online (Sandbox Code Playgroud)