Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TrimEnd Equivalent in SQL Server

I have column (Numbers) which has values as follows:

1,2,3
1,2,3,
1,2,3,,,
1,2,3,,,,,,

I want to Trim all the Commas at the end of string, So that result would be

1,2,3
1,2,3
1,2,3
1,2,3

I have tried below Query but by this we can remove only one last comma

DECLARE @String as VARCHAR(50)
SET @String='1,2,3,4,,,,,,,,,,,,,,,,'

SELECT CASE WHEN right(rtrim(@String),1) = ',' then substring(rtrim(@String),1,len(rtrim(@String))-1)
    ELSE @String 
    END AS TruncString

How can I remove all the commas at the end of string?

like image 868
Vignesh Kumar A Avatar asked Jan 28 '15 11:01

Vignesh Kumar A


People also ask

Is there a trim function in SQL?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How can I remove last 5 characters from a string in SQL?

Below is the syntax for the SUBSTRING() function to delete the last N characters from the field. Syntax: SELECT SUBSTRING(column_name,1,length(column_name)-N) FROM table_name; Example: Delete the last 2 characters from the FIRSTNAME column from the geeksforgeeks table.

How do I trim the first 3 characters in SQL?

To delete the first characters from the field we will use the following query: Syntax: SELECT SUBSTRING(string, 2, length(string));


1 Answers

You can do this using:

LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1))

The premise of this is you first reverse the string using REVERSE:

REVERSE(Numbers) --> ,,,,,,3,2,1

You then find the position of the first character that is not a comma using PATINDEX and the pattern match [^,]:

PATINDEX('%[^,]%', REVERSE(Numbers)) --> ,,,,,,3,2,1 = 7

Then you can use the length of the string using LEN, to get the inverse position, i.e. if the position of the first character that is not a comma is 7 in the reversed string, and the length of the string is 10, then you need the first 4 characters of the string. You then use SUBSTRING to extract the relevant part

A full example would be

SELECT  Numbers,
        Reversed = REVERSE(Numbers),
        Position = PATINDEX('%[^,]%', REVERSE(Numbers)),
        TrimEnd = LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1))
FROM    (VALUES 
            ('1,2,3'), 
            ('1,2,3,'), 
            ('1,2,3,,,'), 
            ('1,2,3,,,,,,'), 
            ('1,2,3,,,5,,,'), 
            (',,1,2,3,,,5,,')
        ) t (Numbers);

EDIT

In response to an edit, that had some errors in the syntax, the below has functions to trim the start, and trim both sides of commas:

SELECT  Numbers,
        Reversed = REVERSE(Numbers),
        Position = PATINDEX('%[^,]%', REVERSE(Numbers)),
        TrimEnd = LEFT(Numbers, LEN(Numbers) - (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1)),
        TrimStart = SUBSTRING(Numbers, PATINDEX('%[^,]%', Numbers), LEN(Numbers)),
        TrimBothSide = SUBSTRING(Numbers, 
                                    PATINDEX('%[^,]%', Numbers), 
                                    LEN(Numbers) - 
                                        (PATINDEX('%[^,]%', REVERSE(Numbers)) - 1) - 
                                        (PATINDEX('%[^,]%', Numbers) - 1)
                                    )
FROM    (VALUES 
            ('1,2,3'), 
            ('1,2,3,'), 
            ('1,2,3,,,'), 
            ('1,2,3,,,,,,'), 
            ('1,2,3,,,5,,,'), 
            (',,1,2,3,,,5,,')
        ) t (Numbers);
like image 186
GarethD Avatar answered Nov 11 '22 21:11

GarethD