2

In my table column, i have below sample data

Test1 145,  Area 1
Test2 146,
Test3 145,  Area 2, Plot 10

What i want to achieve is to replace "," in the string but only if it is the last character. If i have more characters after "," then the replace should leave the string as it is.

In the example above, the replace would only work in line 2.

The expected out put would be like below

Test1 145,  Area 1
Test2 146 
Test3 145,  Area 2, Plot 10

In line 2 above, "," has been replaced with empty space.

I have tried this Replace(column1, ', ', '') AS ColName but this replaces all the "," in Test1 and Test3.

3
  • So the 3 lines are a single value or are you showing 3 rows? Commented Jun 9, 2016 at 13:07
  • @AlexK. i'm showing 3 rows Commented Jun 9, 2016 at 13:12
  • 4
    Possible duplicate of SQL : remove last comma in string Commented Jun 9, 2016 at 13:24

5 Answers 5

2

You can try this:

DECLARE @value VARCHAR(1024) = 'Test2 146,';

SELECT IIF(RIGHT(@value,1) = ',', LEFT(@value, LEN(@value) - 1), @value);

For column it looks like below:

DECLARE @DataSource TABLE
(
    [value] VARCHAR(1024)
);

INSERT INTO @DataSource ([value])
VALUES ('Test1 145,  Area 1')
      ,('Test2 146,')
      ,('Test3 145,  Area 2, Plot 10');


SELECT IIF(RIGHT([value],1) = ',', LEFT([value], LEN([value]) - 1), [value])
FROM @DataSource;
Sign up to request clarification or add additional context in comments.

2 Comments

looks like IIF is only available in 2012+
You can use CASE statement - check the answers below.
2

You can also do this will LIKE and IIF :

SELECT IIF(t.Column LIKE '%,' , LEFT(t.column, LEN(t.column) - 1) , t.column) as new_val
FROM YourTable t

For older versions: You can use CASE EXPRESSION since IIF is only available since 2012+ version(Link by @gotqn)

SELECT CASE WHEN t.Column LIKE '%,' 
            THEN LEFT(t.column, LEN(t.column) - 1) 
            ELSE t.column
       END as new_val
FROM YourTable t

9 Comments

Of course I also just noticed this is tagged 2005 so that won't work for the OP. But it will work for others that stumble in here in the future.
The IIF won't work, but the case will won't it? Or another function here is not available in that version either?
CASE is OK - IIF is from 2012+ editions (msdn.microsoft.com/en-us/library/hh213574.aspx)
Yes sorry my comment was not very clear. The case expression should work fine. The IIF version should work too but is not likely going to work for the OP as they are on 2005.
I just tested the case expression but still didn't work, what i have found to be working is the answer by @nscheaffer.
|
1

I am pretty sure IIF isn't available in SQL Server 2005. This is basically the same logic as the previous answer using CASE instead.

declare @MyString varchar(50)

set @MyString = 'Test2 146,'

select 
case
    when right(rtrim(@MyString), 1) = ',' then
        substring(@MyString, 1, len(rtrim(@MyString)) - 1)
    else
        @MyString
end

Comments

0

Something like this:

SELECT CASE 
          WHEN Column1 LIKE '%,' THEN STUFF(column1, LEN(column1), 1, '') 
          ELSE Column1
       END

Comments

0

This shows one way to do it.

DECLARE @test VARCHAR(30);

SET @test = 'Test1, 145, Area 1';

SELECT @test;

IF CHARINDEX(',', @test, LEN(RTRIM(@test))) > 0
BEGIN
    SET @test = Replace(@test, ',', '');
END

SELECT @test;

SET @test = 'Test2 146,';

SELECT @test;

IF CHARINDEX(',', @test, LEN(RTRIM(@test))) > 0
BEGIN
    SET @test = Replace(@test, ',', '');
END

SELECT @test;

SET @test = 'Test3 145, Area 2, Plot 10';

SELECT @test;

IF CHARINDEX(',', @test, LEN(RTRIM(@test))) > 0
BEGIN
    SET @test = Replace(@test, ',', '');
END

SELECT @test;

-- How to work into a SELECT statement

SET @test = 'Test2 146,';

SELECT CASE WHEN CHARINDEX(',', @test, LEN(RTRIM(@test))) > 0 THEN SUBSTRING(@test, 1, LEN(@test) - 1) ELSE @test END AS 'Converted Value';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.