Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
yaditzal
Starting Member
22 Posts |
Posted - 2010-04-05 : 11:38:46
|
Hello.please I need help with this:table specialties SPEC_DESC SPEC_CODE PRACTYPEOptometrist OY 23-5Gastroenterology GA 7-14Endocrinology ED 1Nephrology NP The problem is I need to split the PRACTYPE in 2 records. it have to look like this:SPEC_DESC SPEC_CODE PRACTYPEOptometrist OY 23Optometrist OY 5Gastroenterology GA 7Gastroenterology GA 14Endocrinology ED 1Nephrology NP Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-05 : 15:38:49
|
Do you need to handle records like this:Optometrist OY 23-5-42Gastroenterology GA 7-14-6-21There are 10 types of people in the world, those that understand binary, and those that don't. |
|
|
yaditzal
Starting Member
22 Posts |
Posted - 2010-04-05 : 16:27:34
|
YES THAT THE PROBLEM.I HAVE A COLUMN WITH MORE THAN ONE VALUE FOR THE SPECIALTY TYPE AND I NEED TO SPLIT IN ANOTHER ROW FOR EACH SPECIALTY TYPELIKE THISOptometrist OY 23Optometrist OY 5Optometrist OY 42Gastroenterology GA 7Gastroenterology GA 14Gastroenterology GA 21-14-6-21 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-05 : 16:53:40
|
Assuming the tablename is Spec, this should do what you want.DECLARE @Count INT SET @Count = 1 WHILE @Count > 0 BEGIN INSERT INTO Spec SELECT SPEC_DESC, SPEC_CODE, LEFT(PRACTYPE, CHARINDEX('-', PRACTYPE) - 1) FROM Spec WHERE PRACTYPE LIKE '%-%' SET @Count = @@ROWCOUNT IF @Count > 0 BEGIN UPDATE Spec SET PRACTYPE = SUBSTRING(PRACTYPE, CHARINDEX('-', PRACTYPE) + 1, 8000) WHERE PRACTYPE LIKE '%-%' END END SELECT * FROM Spec There are 10 types of people in the world, those that understand binary, and those that don't. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-06 : 01:02:52
|
[code]--sample data only for illustrationselect * into #TablFROM( SELECT 'Optometrist' AS SPEC_DESC, 'OY' AS SPEC_CODE, '23-5' AS PRACTYPE UNION ALLSELECT 'Gastroenterology' ,'GA' ,'7-14-15-64-39' UNION ALLSELECT 'Endocrinology', 'ED', '1-123-345-567-890' UNION ALLSELECT 'Nephrology', 'NP','' )t--Your actual solutionSELECT t.SPEC_DESC,t.SPEC_CODE,CASE WHEN t.PRACTYPE <> '' THEN SUBSTRING('-'+t.PRACTYPE,v.number+1,CHARINDEX('-','-'+t.PRACTYPE+'-',v.number+1)-v.number-1) ELSE t.PRACTYPE ENDFROM #Tabl tINNER JOIN master..spt_values vON v.type='p'AND (v.number <=LEN(t.PRACTYPE) OR t.PRACTYPE = '')AND SUBSTRING('-'+t.PRACTYPE,v.number,1)='-'DROP TABLE #Tabloutput-----------------------------SPEC_DESC SPEC_CODE PRACTYPE Optometrist OY 23Optometrist OY 5Gastroenterology GA 7Gastroenterology GA 14Gastroenterology GA 15Gastroenterology GA 64Gastroenterology GA 39Endocrinology ED 1Endocrinology ED 123Endocrinology ED 345Endocrinology ED 567Endocrinology ED 890Nephrology NP [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|