Rafał Developer
Rafał Developer

Reputation: 2045

Remove text after character (cut string sql)

I would like to cut left part of text when I will find character '#' :

Text:

1) 12341#123

2) 123#1

Correct Result:

1) 123

2) 1

Do you know how?

I try use this but does not work:

LEFT(A.Vat,CHARINDEX('#',A.Vat)-1)

Upvotes: 0

Views: 14922

Answers (2)

Alex K.
Alex K.

Reputation: 175766

Assuming always only 1 #:

SELECT SUBSTRING(f, CHARINDEX('#', f) + 1, LEN(f))

For more than 1 #

SELECT RIGHT(f, CHARINDEX('#', REVERSE(f)) - 1)

Upvotes: 1

Giannis Paraskevopoulos
Giannis Paraskevopoulos

Reputation: 18411

General rule:

select RIGHT(fieldName,len(fieldName) - patindex('%#%',fieldName))

Examples:

select RIGHT('12341#123',len('12341#123') - patindex('%#%','12341#123'))

select RIGHT('123#1',len('123#1') - patindex('%#%','123#1'))

Upvotes: 2

Related Questions