0

I have a column u_manualdoc which contains the values are like this CGY DR# 7405. I want to remove the CGY DR#. Here's the code:

select u_manualdoc, cardcode, cardname from ODLN

I want only the 7405 number. Thanks!

6
  • You should show us more data. Commented May 24, 2018 at 5:41
  • here are some data in u_manualdoc "CGY PST - 58277" , "CGY RMC PST # 58083", "CGY DR # 7443, CSI # 1304", "PO# 0568 , 0570 CGY DR# 7446". I want only a number. Thanks! Commented May 24, 2018 at 5:47
  • Your first data point has a hyphen instead of pound sign. Are there any other separators besides hyphen and pound? Commented May 24, 2018 at 5:48
  • comma, I think. There's no pound sign here sir Commented May 24, 2018 at 6:01
  • please edit the question and update with the rules or logic that you wanted Commented May 24, 2018 at 6:08

4 Answers 4

1

Try this:

--sample data you provided in comments
declare @tbl table(codes varchar(20))
insert into @tbl values
('CGY PST - 58277') , ('CGY RMC PST # 58083'), ('CGY DR # 7443'), ('CSI # 1304'), ('PO# 0568 , 0570'), ('CGY DR# 7446')

--actual query that you can apply to your table
select SUBSTRING(codes, PATINDEX('%[0-9]%', codes), len(codes)) from @tbl

The key point here is to use patindex, which searches for a pattern and returns index where such pattern occur. I specified %[0-9]% which means that we search for any digit - it will return first occurrence of a digit. Now- since this would be our starting point to substring, we pass it to such function. Third parameter of substring is length. Since we want the rest of a string, len function makes sure that we get that :)

Applying to your naming:

select SUBSTRING(u_manualdoc, PATINDEX('%[0-9]%', u_manualdoc), len(u_manualdoc)),
       cardcode,
       cardname
from ODLN
Sign up to request clarification or add additional context in comments.

3 Comments

@sCom You are right, but from OP data I guess it's mistake and these are two different entries (0570 is part of previous entry).
There should be a constant separator to extract , suppose if string is ' 0570 CGY-DR# 7446' then it will fail, as it will give output as 'DR# 7446' rather than '7446'
There are some value that still has a character because others have the value like this ('PO# 0568 , 0570 CGY DR# 7446'). Not all value has the same length. But the rest are only the numbers. Thanks @Evaldas Buinauskas
1

You should use string functions charindex,len and substring to get it. See the code below.

select SUBSTRING(u_manualdoc,CHARINDEX('#',u_manualdoc)+1,LEN(u_manualdoc)- CHARINDEX('#',u_manualdoc))

3 Comments

It won't work in case of CGY PST - 58277. Maybe you could use patindex with %[#-]% pattern.
'#' is a separator here, it will be there as you can see in values given by @shiela. She want to extract from '#' not any other charecter.
Thanks for the response. It remove the other but there are others are still have characters. But thanks!
1

EDIT

In addition to the other answers, you can use this simple method:

select
    substring(
        u_manualdoc,
        len(u_manualdoc) - patindex('%[^0-9]%', reverse(u_manualdoc)) + 2,
        len(u_manualdoc)
    ),
    cardcode, cardname
from ODLN

In this example, patindex finds the first non-digit (as specified by ^[0-9]) from the right side of the string, and then uses that as the starting point of the substring.

This will work on all of your sample strings (including 'PO# 0568 , 0570 CGY DR# 7446').

Or use SQL Server Regex, which lets you use more powerful regular expressions within your queries.

3 Comments

There should be a constant separator to extract , suppose if string is ' 0570 CGY-DR# 7446' then it will fail, as it will give output as 'DR# 7446' rather than '7446'
That is not correct @sCom. It returns 7446 for that string.
Exactly what i thought after coming back to this question! Brilliant solution :) +1
0

TRY THIS

DECLARE @table TABLE(DirtyCol VARCHAR(100));
INSERT INTO @table
VALUES('AB ABCDE # 123'), ('ABCDE# 123'), ('AB: ABC# 123 AB: ABC# 123'), ('AB#'), ('AB # 1 000 000'), ('AB # 1`234`567'), ('AB # (9)(876)(543)');
WITH tally
     AS (
     SELECT TOP (100) N = ROW_NUMBER() OVER(ORDER BY @@spid)
     FROM sys.all_columns),
     data
     AS (
     SELECT DirtyCol,
            Col
     FROM @table
          CROSS APPLY
(
    SELECT
(
    SELECT C+''
    FROM
(
    SELECT N,
           SUBSTRING(DirtyCol, N, 1) C
    FROM tally
    WHERE N <= DATALENGTH(DirtyCol)
) [1]
    WHERE C BETWEEN '0' AND '9'
    ORDER BY N FOR XML PATH('')
)
) p(Col)
     WHERE p.Col IS NOT NULL)
     SELECT DirtyCol,
            CAST(Col AS INT) IntCol
     FROM data;

1 Comment

please format your code in order to be helpful to the OP

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.