0

I am using SQL Server 2016. I have a column A that has values such as C20.1 and C20.2. I just want to remove the postfix values (.1 and .2) and keep C20 only.

Column A
---------
C20
C20.1
C20.2

Output should be like this:

Column B
--------
C20
C20
C20

I tried this SQL statement:

SELECT 
    REPLACE (ColumnA, '.1', '.2', '')

but this query throws an error when I pass 4 parameters.

2 Answers 2

2

We could phrase your requirement as saying that you want to retain the input string up, but not including, the first dot, or if not dot be present, then retain the entire string. This gives us:

SELECT
    val,
    CASE WHEN CHARINDEX('.', val) > 0
         THEN SUBSTRING(val, 1, CHARINDEX('.', val) - 1)
         ELSE val END AS val_out
FROM yourTable;

screen capture from demo link below

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You can use substring()

select substring(columnA,0,4)
from table

1 Comment

The best expression is left(columnA, 4).

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.