DeePak
DeePak

Reputation: 145

Split a value in T-SQL

I have a value in one column like this: PD 10256-P1.

I want to split this information & only need the number 10256.

How to achieve this?

Upvotes: 0

Views: 53

Answers (3)

nimajv
nimajv

Reputation: 433

you can use it :

declare @var varchar(50)= 'PD 10256-P1'
select substring(@var,charindex(' ',@var,1), charindex('-p',@var,1)-charindex(' ',@var,1))

Upvotes: 0

bhuvnesh pattnaik
bhuvnesh pattnaik

Reputation: 1463

Here is generalised Format for you

SELECT SUBSTRING('PD 10256-P1', CHARINDEX(' ', 'PD 10256-P1'), CHARINDEX('-', SUBSTRING('PD 10256-P1', CHARINDEX(' ', 'PD 10256-P1'), LEN('PD 10256-P1')))-1) AS RequiredString;

You can replace 'PD 10256-P1' by your column name.

Upvotes: 1

A Farmanbar
A Farmanbar

Reputation: 4798

You can easily do it by

SUBSTRING('PD 10256-P1', 4, 5)

it cuts your input to 5 character string starting from 4th character.

result is

10256

Read more

Upvotes: 3

Related Questions