//SQL SERVER
CREATE TABLE A
(
Name varchar(40)
);
INSERT INTO A
(Name)
VALUES
('1-nikhil-s-a--sada'),
('55--sunil-s-a--sada'),
('-2--asd-ww');
CHARINDEX(charachter, field)
SUBSTRING(field, tart position, length upto)
//To get 1st
CHARINDEX('-', Name)
//TO get 2nd
SELECT CHARINDEX('-', Name, CHARINDEX('-', Name) + 1)
from A
//TO GEt 3rd
SELECT CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1)
from A
//TO GEt 4th
SELECT CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1) + 1)
from A
//Length 3-2
SELECT (CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1)-CHARINDEX('-', Name, CHARINDEX('-', Name) + 1)) -1
from A
//Length 4-3
SELECT (CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1) + 1) - CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1) ) -1
from A
//To get string between 2 and 3rd '-'
select substring(Name,CHARINDEX('-', Name, CHARINDEX('-', Name) + 1)+1,(CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1)-CHARINDEX('-', Name, CHARINDEX('-', Name) + 1)) -1)
from A
//To get string between 3 and 4th '-'
select substring(Name,CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1) +1 , (CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1) + 1) - CHARINDEX('-', Name, CHARINDEX('-', Name, CHARINDEX('-', Name) + 1) + 1) ) -1 )
from A