简体   繁体   中英

How to remove Roman letter and numeric value from column in SQL

Using SQL Server, I have a column with numeric and Roman numerals at the end. How do I remove the numeric alone without specifying the position?

Job_Title
Data Analyst 2
Manager 50
Robotics 1615
Software Engineer
DATA ENGINEER III

I tried using this query:

SELECT 
    CASE 
        WHEN PATINDEX('%[0-9 ]%', job_title) > 0 
            THEN RTRIM(SUBSTRING(Job_title, 1, PATINDEX('%[0-9 ]%', job_title) - 1))
            ELSE JOB_TITLE
    END
FROM
    my_table
WHERE 
    PATINDEX('%[0-9]%', JOB_TITLE) <> 0

But the result I'm getting is:

Job_Title
Data
Manager
Robotics

You should remove the space character in the regex expression. So, new code should be

    SELECT case when patindex('%[0-9]%', job_title) > 0 then
    rtrim(substring(Job_title,1, patindex('%[0-9]%', job_title) - 1))
  else
    JOB_TITLE
  end 
from my_table
WHERE PATINDEX('%[0-9]%',JOB_TITLE) <>0

Use the TRANSLATE function like this:

SELECT TRANSLATE(Job_title, '0123456789', '          ') AS JOB_TITLE
from my_table

You can use RTRIM to complete

I think you're trying to remove numbers from the end of a job title, and not exclude results. So, as others have mentioned, you need to remove the space from the brackets of the regex and put it in front of the brackets to say it is separated from the stuff in front of it by a space. But I think you also need to remove the wildcard character from the right side of the comparison value so that the numbers have to be at the end of the job title, like...

SELECT case when patindex('% [0-9]', job_title) > 0 then
    rtrim(substring(Job_title,1, patindex('% [0-9]', job_title) - 1))
  else
    JOB_TITLE
  end 
from my_table

But, you also mention roman numerals... and... that's tougher if it's possible for a job title to end in something like " X" where it means "X" and not "10". If that's not possible, you should just be able to do [0-9IVXivx] to replace all the bracketed segments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM