SQL Server:删除字符串开头和结尾的连字符

问题描述:

我需要从表中的7个不同列连接起来,并在每列的文本之间用连字符连接。SQL Server:删除字符串开头和结尾的连字符

在少数情况下,列有空值, 我得到像字符串; --Account--stands---

期望的输出是Account-stands。请帮忙。

问候, Sajan

+0

欢迎#1。你到目前为止尝试了什么?查看本指南:[如何创建最小,完整和可验证的示例](https://*.com/help/mcve)并尝试包含示例代码以显示您的问题。 – MrLeeh

在SQL Server中,你可以这样做:

select stuff(coalesce('-' + col1, '') + 
      coalesce('-' + col2, '') + 
      coalesce('-' + col3, '') + 
      coalesce('-' + col4, '') + 
      coalesce('-' + col5, '') + 
      coalesce('-' + col6, '') + 
      coalesce('-' + col7, ''), 
       1, 1, '') 

许多其他的数据库支持像CONCAT_WS()能够简化这一操作的功能。

+0

这会抛出语法错误,删除'('每个'coalesce'之前。 –

+0

复制和粘贴的缺陷,我敢肯定:-) –

+0

@Wandered。 。 。谢谢。 –

您可以通过使用COALESCESTUFF实现它,避免与空值的问题''你可以在以下

select stuff(coalesce(nullif('-' + col1, '-'), '') + 
       coalesce(nullif('-' + col2, '-'), '') + 
       coalesce(nullif('-' + col3, '-'), '') + 
       coalesce(nullif('-' + col4, '-'), '') + 
       coalesce(nullif('-' + col5, '-'), '') + 
       coalesce(nullif('-' + col6, '-'), '') + 
       coalesce(nullif('-' + col7, '-'), ''), 
       1, 1, '') 

那么你可以使用CASE表达式来检查值是否是null或不加NULLIF

查询

select case when t.[concat_string] like '%-' 
then left(t.[concat_string], len(t.[concat_string]) - 1) 
else t.[concat_string] end as [concat_string] from(
    select 
    case when [col1] is null or ltrim(rtrim([col1])) = '' 
     then '' else [col1] + '-' end + 
    case when [col2] is null or ltrim(rtrim([col2])) = '' 
     then '' else [col2] + '-' end + 
    case when [col3] is null or ltrim(rtrim([col3])) = '' 
     then '' else [col3] + '-' end + 
    case when [col4] is null or ltrim(rtrim([col4])) = '' 
     then '' else [col4] + '-' end + 
    case when [col5] is null or ltrim(rtrim([col5])) = '' 
     then '' else [col5] + '-' end + 
    case when [col6] is null or ltrim(rtrim([col6])) = '' 
     then '' else [col6] + '-' end + 
    case when [col7] is null or ltrim(rtrim([col7])) = '' 
     then '' else [col7] end as [concat_string] 
    from [your_table_name] 
)t; 

如果任何列不VARCHAR,那么你可能需要CASTVARCHAR

+0

如此复杂的语法,更可读的方法是使用'STUFF'和'COALESCE'来实现它。 –

+0

@StanislovasKalašnikovas:如果该值不为空,则'COALESCE'不起作用,但''''不起作用。让我的答案在另一个角度出现在那里。 – Wanderer

假设你已经建立你的查询关键词,比如下面的一个。

select 
isnull(col1,'') + '-' + isnull(col2,'') + '-' + isnull(col3,'') as [columns] 
from yourtable 

用一个字符替换空值。例如尝试下面的查询

 select replace(replace ([columns],'|-',''), (case when 
charindex ('-|',replace ([columns],'|-','')) > 0 then '-|' else '|' end),'') 
    from(select 
     isnull(col1,'|') + '-' + isnull(col2,'|') + '-' +isnull(col3,'|')[columns] 
     from yourtable)temp 

DECLARE @String table (String nvarchar(max)) 
INSERT INTO @String 
SELECT '--Account--stands---' 

;WITH Cte 
AS 
(
SELECT String From 
(
SELECT Split.a.value('.', 'VARCHAR(100)') AS String, 
PATINDEX('%[a-zA-Z]%',Split.a.value('.', 'VARCHAR(100)')) AS ISChar FROM 
(
SELECT 
CAST('<S>' + REPLACE(STRING ,'--','</S><S>') + '</S>' AS XML) AS String from @String 
) AS A 
CROSS APPLY String.nodes('/S') AS Split(a) 

) dt 
Where dt.ISChar=1 
) 
SELECT DISTINCT STUFF((SELECT DISTINCT '- ' + String FROM Cte 
FOr xml path ('')),1,1,'') AS String 
FROM Cte 

输出

String 
------- 
Account- stands