0

I have varchar column in my table which has some data like:

"#306 loream ipsum aaabbbqqqcc"
"#302 loream ipsum aaabbbceefec"
"#330 loream ipsum aaabbbccsds"

I want to replace #3XX like regex syntax in C# or JavaScript.

How to implement this in SQL Server?

7
  • Can you show sample output? Commented Jul 26, 2016 at 13:25
  • just replace string with blank like 'loream ipsum aaabbbcc' code# REPLACE(StringValue,'#3XX','') Commented Jul 26, 2016 at 13:26
  • Is it always the first 4 characters? Commented Jul 26, 2016 at 13:27
  • Then this is a simple SUBSTRING. Commented Jul 26, 2016 at 13:28
  • @SeanLange: but length is not fixed od data Commented Jul 26, 2016 at 13:29

4 Answers 4

2

You can make use of the STUFF function to achieve this.

Declare @String Varchar(100) = '#306 loream ipsum aaabbbqqqcc'

SELECT STUFF(@String , 1,5,'')

Result: loream ipsum aaabbbqqqcc

You can make use of the RIGHT function to achieve this.

Declare @String Varchar(100) = '#306 loream ipsum aaabbbqqqcc'

SELECT RIGHT(@String , LEN(@String) - 5)

RESULT:  loream ipsum aaabbbqqqcc
Sign up to request clarification or add additional context in comments.

Comments

1

You can use substring if it is always first 4 characters..

declare @str varchar(max)
set 
@str='#306 loream ipsum aaabbbqqqcc'

select substring(@str,5,len(@str))

Comments

1

if it is possible to split string by space then follow below one

Declare @str Varchar(100) = '#306 loream ipsum aaabbbqqqcc'
set @str =substring( @str ,CHARINDEX(' ', @str) , len(@str) - CHARINDEX(' ', @str)+1)

In this one you don't need any default length or default replacement text.

Comments

1

To Remove it , use this query.Since the length of the string which you want to remove is always 4.

     UPDATE table-name SET column-name = RIGHT(column-name, LEN(column-name) - 4)

But run only one time because it is UPDATE query

For MySql you have to use the length() function like that,

     UPDATE table-name SET column-name = RIGHT(column-name, LENGTH(column-name) - 4)

2 Comments

The point from the OP is they don't know what string it is. It is a pattern, not a fixed string.
Not sure if the Replace function will be a good choice since the string to be replaced is dynamic.

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.