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 |
|
razeena
Yak Posting Veteran
54 Posts |
Posted - 2011-07-22 : 01:32:27
|
| Consider a table with academic information Declare @TestTable table(strname varchar(20))insert into @testtable select 'some board some' union allselect 'some secondary blah' union allselect 'some university' union allselect 'schools of something'If the column contains value either 'board' or 'secondary' or 'schools' anywhere in the name , I need returnvalue as precollege ,otherwise college.Name Category******** *************some board some precollegesome secondary blah precollegesome university college schools of something precollege |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-07-22 : 02:02:32
|
select *, case when strname like '% board #' then 'precollege'when strname like '% secondary #' then 'precollege'when strname like '% schools #' then 'precollege'else 'college'endfrom dbo.table1 N 56°04'39.26"E 12°55'05.63" |
 |
|
|
razeena
Yak Posting Veteran
54 Posts |
Posted - 2011-07-22 : 06:21:39
|
Thanks. I am putting the scenario in the other way.If there is board/schools/secondary keyword in the string, need as precollegeIn other cases if there is a slash present, cut the data before the slashIf both the above doesn't match,get the string as it is.For eg,like below data. *************************************** some board some------precollegesome secondary blah-------Precollegemasters/mba----- mastersacademy ---academy schools of something--------precollegeGirls/Board ------------------Precollege**********************************************I tried the code below, some error is obtainedDeclare @TestTable table(strname varchar(20))insert into @testtable select 'some board some' union allselect 'some secondary blah' union allselect 'masters/mba' union allselect 'academy ' union allselect 'schools of something' union allselect 'Girls/Board'select *, case when strname like '%board%' then 'precollege' when strname like '%secondary%' then 'precollege' when strname like '%schools%' then 'precollege' WHEN CHARINDEX('/',strname) >0 THEN LEFT(strname,CHARINDEX('/',strname))-1 else LEN(strname) end AS Categoryfrom @testtable quote: Originally posted by SwePeso select *, case when strname like '% board #' then 'precollege'when strname like '% secondary #' then 'precollege'when strname like '% schools #' then 'precollege'else 'college'endfrom dbo.table1 N 56°04'39.26"E 12°55'05.63"
|
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2011-07-22 : 06:46:36
|
select *,casewhen strname like '%board%' then 'precollege' when strname like '%secondary%' then 'precollege'when strname like '%schools%' then 'precollege'WHEN CHARINDEX('/',strname) >0 THEN LEFT(strname,CHARINDEX('/',strname)-1)else convert(varchar,LEN(strname))end AS Categoryfrom @testtable Corey I Has Returned!! |
 |
|
|
razeena
Yak Posting Veteran
54 Posts |
Posted - 2011-07-22 : 07:28:59
|
Great!!quote: Originally posted by Seventhnight select *,casewhen strname like '%board%' then 'precollege' when strname like '%secondary%' then 'precollege'when strname like '%schools%' then 'precollege'WHEN CHARINDEX('/',strname) >0 THEN LEFT(strname,CHARINDEX('/',strname)-1)else convert(varchar,LEN(strname))end AS Categoryfrom @testtable Corey I Has Returned!!
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|