Remove UNION at the end of a String

Bobby P 221 Reputation points
2023-05-17T18:50:18.7833333+00:00

I have created some Dynamic SQL that is in a Variable @SQLQuery1. The word "UNION" is at the very end because my DynamicSQL is sweeping through databases and doing UNIONs along the way. So the last UNION is dangling by itself at the end of the string.

I know I can use RIGHT and CHARINDEX to remove the "UNION" at the very end...just looking for Syntax help...

Thanks in Advance for your help

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,705 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2023-05-17T20:13:50.5366667+00:00

    If “UNION” is always present at the end, and there are no subsequent spaces, then:

    set @SQLQuery1 = substring(@SQLQuery1, 1, len(@SQLQuery1) - len('UNION'))
    
    0 comments No comments

  2. Bobby P 221 Reputation points
    2023-05-17T20:37:50.87+00:00

    SELECT @SQLQuery2 = SUBSTRING (@SQLQuery2, 1, LEN (@SQLQuery2) - CHARINDEX ('U', REVERSE (@SQLQuery2)))

    ;

    0 comments No comments

  3. Erland Sommarskog 101K Reputation points MVP
    2023-05-17T21:39:55.0566667+00:00

    Insert the statements you want to create for each database into a temp table with two columns, db and stmt. Then create the total query with:

    SELECT @query = string_agg(convert(nvarchar(MAX), stmt), 'UNION ALL ' + char(13) + char(10))
    FROM   #temp
    

    Requires SQL 2017 or later.

    And, yeah, UNION ALL. I don't think you want to remove duplicates from the result set, do you?

    0 comments No comments

  4. Guoxiong 8,126 Reputation points
    2023-05-17T22:33:05.2133333+00:00

    If you cannot make sure if the word "UNION" is always at the end of the string, you can try this:

    DECLARE @SQLQuery1 varchar(max);
    SET @SQLQuery1 = 'SELECT ID, Name FROM DB1.dbo.MyTable UNION SELECT ID, Name FROM DB2.dbo.MyTable UNION SELECT ID, Name FROM DB3.dbo.MyTable UNION';
    SELECT CASE WHEN LEFT(REVERSE(@SQLQuery1), 5) = REVERSE('UNION') THEN REVERSE(SUBSTRING(REVERSE(@SQLQuery1), 6, LEN(@SQLQuery1) - 5)) ELSE @SQLQuery1 END;
    

    User's image

    0 comments No comments

  5. PercyTang-MSFT 12,426 Reputation points Microsoft Vendor
    2023-05-18T02:01:09.2433333+00:00

    Hi @Bobby P

    Or you can try this.

    Declare @SQLQuery2 varchar(max) = 'select * from table1 UNION select * from table2 UNION';
    SELECT @SQLQuery2 =  left(@SQLQuery2,len(@SQLQuery2) - len('UNION'));
    print @SQLQuery2;
    

    Printout:

    User's image

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments