无法删除字符串SQL Server中的第一个逗号

时间:2012-04-12 20:11:56

标签: sql-server

外,代码工作正常
SELECT DISTINCT isnull(@FEATURE + ', ', ',') + Feature 
    FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 where (rf2.roomid =   rf.roomid)
    for xml path('')), 

这给了我一些问题。我不知道如何摆脱第一个逗号。我已经尝试了substring但它删除了所有的逗号(我猜它是在select语句中)。我甚至试过了 COALESCE(@ FEATURE +',','')+ FEATURE 但我最终会得到相同的结果,无论是在结尾还是在开头都没有逗号或一个额外的逗号。

DECLARE @FEATURE VARCHAR(500) 
-- INSERT INTO BookableRooms IF NOT EXISTS ROOM NUMBER 
INSERT INTO dbo.BookableRooms_test (FK_Building_code,
                                    Room_num,Other_room_name,
                                    Capacity_from,
                                    Capacity_to,
                                    Accessiblity,AV_book_separate,Flooring_type,Features,
                                    Food_drink_allowed, Alcohol_allowed,Internet,
                                    [Owner],FK_BookingContact_ID,
                                    LCD_projector,Computer,FK_SpaceManager_code,Last_updated_date,Last_updated_by,SpecialFlag)
SELECT DISTINCT CASE
            WHEN rf.[Building Reference] is null or rf.[Building Reference] = '' THEN 'HH'
            ELSE rf.[Building Reference] 
        END,
        [ROOMID],[Description],
       (SELECT MIN(rf2.CAPACITY) FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] AS rf2 WHERE rf2.ROOMID = rf.ROOMID),
       (SELECT MAX(rf2.CAPACITY) FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] AS rf2 WHERE rf2.ROOMID = rf.ROOMID),  
       dbo.iszero([Wheelchair Accessible]), dbo.iszero(rf.[Separate AV]),dbo.isflat(rf.[Flat Floor]),
       (
        SELECT DISTINCT isnull(@FEATURE + ', ', ',') + Feature 
        FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 where (rf2.roomid = rf.roomid)
        for xml path('')),
       dbo.iszero(rf.[Food and Drink]),dbo.iszero(rf.[Alcohol]),dbo.iszero(rf.[Room Internet]),
       'HH',5,
       dbo.iszero(rf.[Digital Projector]),dbo.iszero(rf.[Computer]),'HH',GetDate(),'System',dbo.iszero(rf.[Restricted])

 FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf  where NOT EXISTS(
    SELECT 1 FROM dbo.BookableRooms_test b WHERE 
    b.Room_num = rf.ROOMID);

3 个答案:

答案 0 :(得分:3)

使用Stuff但在子查询的外侧:

stuff ((SELECT DISTINCT ',' + Feature 
        FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 
        WHERE (rf2.roomid = rf.roomid)
        for xml path('')), 1, 1, '')

答案 1 :(得分:2)

使用STUFF功能删除前导逗号。

SELECT DISTINCT STUFF(',' + Feature 
    FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 where (rf2.roomid =   rf.roomid)
    for xml path('')),1,1,''), 

答案 2 :(得分:0)

我不确定你想要实现的目标。如果我的猜测是错误的,如果你告诉我们你在哪种情况下会得到什么结果会很好。

试试这个

CASE WHEN @FEATURE IS NULL THEN NULL ELSE @FEATURE + ', ' END