Skip to content
Advertisement

SQL – Prepend a value if missing

Code/data:

DECLARE @T TABLE
    (
        [Col1]   VARCHAR(20)
      , [RowNum] INT
    ) ;

INSERT INTO @T
VALUES
    ( N'second', 1 )
  , ( N'fifth', 4 )
  , ( N'fourth', 3 )
    --, ( N'zzz', 1 )
  , ( N'third', 2 )

---- OR when "zzz" is part of this list
--VALUES
--     ( N'second', 2 )
--   , ( N'fifth', 5 )
--   , ( N'fourth', 4 )
--   , ( N'zzz', 1 )
--   , ( N'third', 3 )
SELECT  STUFF ((
                   SELECT   ',' + [SQ].[Col1]
                   FROM
                            (
                                SELECT  N'zzz' AS [Col1]
                                      , 1 AS [RowNum]
                                UNION
                                SELECT  [Col1]
                                      , [RowNum]
                                FROM    @T
                            ) AS [SQ]
                   FOR XML PATH ( '' ), TYPE
               ).[value] ( '.', 'varchar(MAX)' ), 1, 1, ''
              ) ;

Current output:

fifth,fourth,second,third,zzz

Goal: Prepend “zzz,” in the output string if missing in the 2nd part of the union AND the values should be in ASC ordered based on the values specified in [rownum] field defined in the 2nd part of the union. If “zzz” exists in the 2nd part of the input already (it will always be RowNum 1 in that case), it should return it only once as the first value.

Expected output:

zzz,second,third,fourth,fifth

UPDATED the requirement due to an error on my part when creating this post. Updated code/data represents more accurate scenario. Please note the RowNum seq in the 2nd part of the UNION, it also starts with 1, but this time, it might or might not be associated to “zzz” Basically, I want to prepend “zzz” in the comma-delimited & ordered output if it doesn’t exist.

Advertisement

Answer

Attached the Answer for the updated Scenario-

DECLARE @T TABLE
    (
        [Col1]   VARCHAR(20)
      , [RowNum] INT
    ) ;

INSERT INTO @T
VALUES
    ( N'second', 1 )
  , ( N'fifth', 4 )
  , ( N'fourth', 3 )
    --, ( N'zzz', 1 )
  , ( N'third', 2 )

---- OR when "zzz" is part of this list
--VALUES
--     ( N'second', 2 )
--   , ( N'fifth', 5 )
--   , ( N'fourth', 4 )
--   , ( N'zzz', 1 )
--   , ( N'third', 3 )

SELECT  STUFF ((
                   SELECT   ',' + [SQ].[Col1]
                   FROM
                            (
                                SELECT  N'zzz' AS [Col1]
                                      , 0 AS [RowNum]
                                UNION
                                SELECT  [Col1]
                                      , [RowNum]
                                FROM    @T
                            ) AS [SQ]
                   ORDER BY [RowNum] 
                   FOR XML PATH ( '' ), TYPE
               ).[value] ( '.', 'varchar(MAX)' ), 1, 1, ''
              ) ;

Returns

zzz,second,third,fourth,fifth

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement