1

SELECT a.[PREMISES],a.[Class] FROM [Legacy].[dbo].[MyTables] as a;

Above query returns :

enter image description here

Can you tell me how to remove 210, and 211, from the query column a.[Class] ?

Note : I just gave an example.I need to have a generic solution which will apply for all the rows of that a.[Class] column.

Note 2 : Format of the Class column is like this.numeric then , after that string.I need to remove first 2 parts.Need to keep only the string part.

4
  • SELECT a.[PREMISES], SUBSTRING(a.[Class], 5, 100) FROM [Legacy].[dbo].[MyTables] as a; Commented Jun 20, 2016 at 9:39
  • is your class column have specific format data?? Commented Jun 20, 2016 at 9:40
  • @JaydipJ Yes,It's like this numeric then , after that string Commented Jun 20, 2016 at 9:44
  • 1
    ok then you can refer Sujeet Ans, this may help you Commented Jun 20, 2016 at 9:46

4 Answers 4

2

You don't necessarily need to use a regular expression here. You could try using REPLACE:

SELECT  a.[PREMISES],
        REPLACE(REPLACE(a.[Class],'210, ',''),'211, ','') as [Class] 
FROM [Legacy].[dbo].[MyTables] as a;

To remove all before first ,:

SELECT  a.[PREMISES],
        LTRIM(RIGHT(NULLIF(a.[Class],''),LEN(a.[Class])-CHARINDEX(',',a.[Class]))) as [Class]
FROM [Legacy].[dbo].[MyTables] as a;

This query, for that data

PREMISES            Class
Victoria Ln         210, Single Family (no style specified)
West 10th Street    211, Ranch, Single Family
East 2nd Street     Single Family

Will give you this output:

PREMISES            Class
Victoria Ln         Single Family (no style specified)
West 10th Street    Ranch, Single Family
East 2nd Street     Single Family
Sign up to request clarification or add additional context in comments.

12 Comments

This won't work.I just gave an example.I need to have generic solution which will apply for all the rows.Thanks.
Then you need to improve the quality of your question.
@ChrisPickford Done that.
You need to remove first 5 symbols from [Class] row? Or there are some cases?
It's like this numeric then , after that string.I need to remove first 2 parts.Need to keep only the string part.
|
2

You can use the following query to return the result starting from ,.

SELECT  a.[PREMISES],
        SUBSTRING(a.[Class], CHARINDEX(',',a.[Class]) + 1, LEN(a.[Class])) as [Class] 
FROM [Legacy].[dbo].[MyTables] as a;

Comments

0

Just remove all numeric characters from a string.

SELECT a.[PREMISES],
    SUBSTRING(a[Class], PATINDEX('%[a-z]%', a.[Class]), LEN(a.[Class])) 
  FROM [Legacy].[dbo].[MyTables] a

Comments

0

Try this:

CREATE FUNCTION Fun_Refiner ( @val VARCHAR(MAX) )
RETURNS VARCHAR(MAX)
AS
BEGIN
    WHILE 1 = 1
        BEGIN
            IF PATINDEX('[0-9]%', @val) > 0 OR SUBSTRING(@val, 1, 1) = ','
                SET @val = RTRIM(LTRIM(REPLACE(@val,SUBSTRING(@val,PATINDEX('[0-9,]%',@val), 1), '')));
            ELSE
                BREAK;
        END;
      RETURN @val
END;

And just call it like this:

SELECT a.[Class], dbo.Fun_Refiner(a.[Class]) FROM YourTable AS a

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.