从STUFF删除尾随逗号

时间:2019-09-27 14:36:15

标签: sql-server tsql

我正在使用地址,并且只想显示不在英国的国家/地区。此方法有效,但是我仅在英国地址上留下了逗号结尾,并且不确定如何删除它。我猜我的STUFF语法不正确,还是我需要采取其他措施?

http://sqlfiddle.com/#!18/2cb48/5

示例数据:

CREATE TABLE [dbo].[AddressDetails]
(
    [AddressID] int IDENTITY(1,1) PRIMARY KEY NOT NULL,
    [PropertyNumber] [nvarchar](50) NULL,
    [PropertyName] [nvarchar](40) NULL,
    [Street] [nvarchar](40) NULL,
    [Locality] [nvarchar](40) NULL,
    [Town] [nvarchar](40) NULL,
    [PostCode] [nvarchar](15) NULL,
    [County] [nvarchar](40) NULL,
    [Country] [nvarchar](40) NULL,
)

INSERT INTO AddressDetails(PropertyNumber, PropertyName, Street, Locality, Town, PostCode, County, Country)
VALUES     
    ('1', NULL, 'Main Street', 'Ilford', 'London', 'E1 1JA', 'Londonshire', 'United Kingdom'),
    ('135', NULL, 'Apple Avenue', NULL, 'Reading', 'RG15 2FC', 'Berkshire', 'GB'),
    ('115/A', 'Cool Building', 'Siskin Close', 'Bushey', 'Watford', 'WD23 2HN', 'Greater London', 'GB'),
    ('1600', 'Amphitheatre', NULL, 'Parkway', 'Mountain View', '94043', 'California', 'USA'),
    ('7221', NULL, 'Shore Lane', 'Hempstead', 'NY', '11550', NULL, 'America!')

查询:

SELECT STUFF(                                           
    COALESCE(', '+NULLIF([PropertyName],''),'')
    +COALESCE(', '+NULLIF(PropertyNumber,''),'')
    +COALESCE(', '+NULLIF([Street],''),'')
    +COALESCE(', '+NULLIF([Locality],''),'')
    +COALESCE(', '+NULLIF([Town],''),'')                                            
    +COALESCE(', '+UPPER(NULLIF([Postcode],'')),'') 
    +COALESCE(', '+ CASE WHEN UPPER([Country]) IN ('GB', 'UNITED KINGDOM') THEN '' ELSE UPPER(NULLIF([Country],'')) END,'')                                                             
    ,1,2,'') as [FullAddress]
FROM AddressDetails

输出:

1, Main Street, Ilford, London, E1 1JA,
135, Apple Avenue, Reading, RG15 2FC,
Cool Building, 115/A, Siskin Close, Bushey, Watford, WD23 2HN,
Amphitheatre, 1600, Parkway, Mountain View, 94043, USA
7221, Shore Lane, Hempstead, NY, 11550, AMERICA!

所需的输出:

1, Main Street, Ilford, London, E1 1JA
135, Apple Avenue, Reading, RG15 2FC
Cool Building, 115/A, Siskin Close, Bushey, Watford, WD23 2HN
Amphitheatre, 1600, Parkway, Mountain View, 94043, USA
7221, Shore Lane, Hempstead, NY, 11550, AMERICA!

3 个答案:

答案 0 :(得分:3)

    Select right(rtrim(FullAddress),1) = ',' then substring(rtrim(FullAddress),1,len(rtrim(FullAddress))-1) as [FullAddress]  From 
(SELECT STUFF(                                           
    COALESCE(', '+NULLIF([PropertyName],''),'')
    +COALESCE(', '+NULLIF(PropertyNumber,''),'')
    +COALESCE(', '+NULLIF([Street],''),'')
    +COALESCE(', '+NULLIF([Locality],''),'')
    +COALESCE(', '+NULLIF([Town],''),'')                                            
    +COALESCE(', '+UPPER(NULLIF([Postcode],'')),'') 
    +COALESCE(', '+ CASE WHEN UPPER([Country]) IN ('GB', 'UNITED KINGDOM') THEN '' ELSE UPPER(NULLIF([Country],'')) END,'')                                                             
    ,1,2,'') as [FullAddress]
FROM AddressDetails) temp

请检查,这可以为您提供帮助

答案 1 :(得分:2)

与您的STUFF(您的国家/地区电话)无关。这样做并记下','+ 的位置。

SELECT STUFF(                                           
COALESCE(', '+NULLIF([PropertyName],''),'')
+COALESCE(', '+NULLIF(PropertyNumber,''),'')
+COALESCE(', '+NULLIF([Street],''),'')
+COALESCE(', '+NULLIF([Locality],''),'')
+COALESCE(', '+NULLIF([Town],''),'')                                            
+COALESCE(', '+UPPER(NULLIF([Postcode],'')),'') 
+COALESCE(CASE WHEN UPPER([Country]) IN ('GB', 'UNITED KINGDOM') THEN '' ELSE ', ' + UPPER(NULLIF([Country],'')) END,'')                                                             
,1,2,'') as [FullAddress]
FROM AddressDetails

欢呼 罗布

答案 2 :(得分:2)

最新答案

只是要指出Function Connect-Exchange { # Find servers list, then sort by name given what site we are running in: SITE1 = Ascending , SITE2 = Descending $ADSite = (Get-ADDomainController).Site if ($ADSite -like "SITE1*") { $descOrder = $true } else { $descOrder = $false } $exchSession = $null $ExchServersDN = "CN=Exchange Install Domain Servers,CN=Microsoft Exchange System Objects,DC=example,DC=com” $ExchServers = (Get-ADObject -Identity $($ExchServersDN) -Properties Member).Member | Sort-Object -Descending:$descOrder # Iterate through Exchange server list until connection succeeds $i = 0; while ((-Not $exchSession) -and ($i -lt $ExchServers.Count)) { $ExchServerURI = "http://" + (Get-ADObject -Identity $ExchServers[$i] -Properties dNSHostName).dnsHostName + "/Powershell" $exchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI $ExchServerURI -ErrorAction SilentlyContinue If (-Not $exchSession) { $i++ } else { Import-PSSession $exchSession -DisableNameChecking | Out-Null } } return $exchSession } 会将CONCAT()值作为一个空字符串来处理。

我个人觉得它更容易阅读。

示例

NULL

返回

SELECT  stuff(
        concat(
              ', '+NullIf([PropertyName]  ,'')
             ,', '+NULLIF([PropertyNumber],'')
             ,', '+NULLIF([Street]        ,'')
             ,', '+NULLIF([Locality]      ,'')
             ,', '+NULLIF([Town]          ,'')
             ,', '+NULLIF([Postcode]      ,'')
             ,', '+NULLIF(CASE WHEN UPPER([Country]) IN ('GB', 'UNITED KINGDOM') THEN '' ELSE UPPER(NULLIF([Country],'')) END,'')
             )
         ,1,2,'')
FROM AddressDetails