Reputation: 69
I need to separate a string in SQL,
Example,
a='001A01-001A05'
What I need in OUTPUT is,
x='001A01'
y='001A05'
Upvotes: 0
Views: 2059
Reputation: 1542
try this using LEFT
and RIGHT
DECLARE @str varchar(max)= '001A01-001A05'
select left(@str, charindex('-', @str)-1),
right(@str, charindex('-', @str)-1)
Upvotes: 1
Reputation: 81950
Prdp's answer would be my first choice, but if SQL Server 2012+, another option is PARSENAME()
Declare @a varchar(25)='001A01-001A05'
Select x=ParseName(Replace(@a,'-','.'),2)
,y=ParseName(Replace(@a,'-','.'),1)
Returns
x y
001A01 001A05
Upvotes: 2
Reputation: 93704
Use LEFT
, CHARINDEX
and SUBSTRING
DECLARE @char VARCHAR(500)= '001A01-001A05'
SELECT LEFT(@char, Charindex('-', @char) - 1),
Substring(@char, Charindex('-', @char) + 1, Len(@char))
Upvotes: 3