How to get the numeric value from this code

Naresh y 146 Reputation points
2022-10-10T14:56:17.067+00:00

HI this is my sample code , where i need to extract only the numeric values from the given string,how do i extract

sample input
XS8011
XS8011
XS8011
X2304_2000
X2314_2001

after under score value i need the numeric value
required output
2000
2001

i tried this query but it is giving all the results,can some one pls correct this query

select Customer,SUBSTRING(Customer,CHARINDEX('_',Customer)+1,LEN(Customer)) from tbluser
this didnt work

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


3 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,621 Reputation points
    2022-10-11T02:17:12.187+00:00

    Hi @Naresh y

    I tried this query but it is giving all the results

    Syntax: CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )
    If CHARINDEX does not find expressionToFind within expressionToSearch, CHARINDEX returns 0.
    That's why this code SUBSTRING(Customer,CHARINDEX('_',Customer)+1,LEN(Customer)) returns entire Customer when there is no '_' in the string.
    What you need is a WHERE clause to filter out records that do not have _, just as Tom answered.

    Additional Notes:
    If you are using SQL Sever 2017+ , you could try TRANSLATE function to remove non-numeric characters like this:

    SELECT REPLACE(TRANSLATE( Customer,  
                             'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',  
    						 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'),--keep 'AAAAA...' same length as the second argument  
    			   'A', '')  
    from #tbluser  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. 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.

    Was this answer helpful?

    0 comments No comments

  2. Tom Cooper 8,501 Reputation points
    2022-10-10T17:48:10.84+00:00
    select Customer,SUBSTRING(Customer,CHARINDEX('_',Customer)+1,LEN(Customer))  
    from tbluser  
    where CHARINDEX('_',Customer) > 0  
      
    

    Tom

    Was this answer helpful?

    0 comments No comments

  3. Tom Phillips 17,786 Reputation points
    2022-10-10T15:13:41.69+00:00

    Try:

    SELECT   
    	TRY_CAST(CASE WHEN CHARINDEX('_',Customer) > 0 THEN RIGHT(Customer,CHARINDEX('_',REVERSE(Customer))-1) ELSE NULL END as INT) as Num  
    FROM @tbluser  
      
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.