Pouya
Pouya

Reputation: 1918

How to Split string value in sqlserver

I have a following string

90-PMR-450
90-PMRA-340

I want to get part 3 of string. example 450 or 340.

plese help me. thanks

Upvotes: 2

Views: 1126

Answers (3)

Aaron Bertrand
Aaron Bertrand

Reputation: 280644

DECLARE @x TABLE(v VARCHAR(32));

INSERT @x SELECT '90-PMR-450'
UNION ALL SELECT '90-PMRA-340';

SELECT Part3 = PARSENAME(REPLACE(v, '-', '.'), 1) FROM @x;

Upvotes: 2

Mikael Eriksson
Mikael Eriksson

Reputation: 139010

declare @T table
(
  Value varchar(15)
)

insert into @T values
('90-PMR-450'),
('90-PMRA-340')  

select stuff(Value, 1, 1+len(Value)-charindex('-', reverse(Value)), '')
from @t

Upvotes: 3

Vikram
Vikram

Reputation: 8333

i think you will find this user defined function to split the string helpful:

http://www.codeproject.com/Articles/7938/SQL-User-Defined-Function-to-Parse-a-Delimited-Str

Upvotes: 1

Related Questions