| Author |
Topic |
|
divyaram
Posting Yak Master
180 Posts |
Posted - 2012-05-11 : 11:00:24
|
| Hi team,i am have column which have data like thiscol1 |col2--------------pressue*0.897 |(height*1.65) |breadth/12 |i want to get data like thiscol1 |col2--------------------------------pressue*0.897 | pressure(height*1.65) | heightbreadth/12 | breadthRegards,Divya |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-05-14 : 05:35:33
|
This might help.--Creating TableCreate Table Ex(col1 varchar(30), col2 varchar(30) )--Inserting Sample DataInsert Into ExSelect 'pressue*0.897', ''Union ALLSelect 'height*1.65', ''Union ALLSelect 'breadth/12', ''--Query for Your Requirement(Method 1)Select Col1,(Case When Col1 = 'pressue*0.897' then 'pressure' When Col1 = 'height*1.65' then 'height' When Col1 = 'breadth/12' then 'breadth' Else '' End) As Col2From Ex--Query for Your Requirement(Method 2)Select Col1,(Case When Col1 = 'pressue*0.897' then SUBSTRING(Col1, 1, (CHARINDEX('*',Col1,1)-1) ) When Col1 = 'height*1.65' then SUBSTRING(Col1, 1, (CHARINDEX('*',Col1,1)-1) ) When Col1 = 'breadth/12' then SUBSTRING(Col1, 1, (CHARINDEX('/',Col1,1)-1) ) Else '' End) As Col2From ExN 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
divyaram
Posting Yak Master
180 Posts |
Posted - 2012-05-14 : 06:55:16
|
Hi Vinu,Thx for your help... but data given above is jus a sample data only...the real scenario has a lots of data... i got out with logic like this.... i am posting here because it may help others in futureselect distinct DB,pColumn,ptable, TColumn,case when PATINDEX('%*%',TColumn)<> 0 then replace(SUBSTRING(TColumn,PATINDEX('(',TColumn), PATINDEX('%*%',TColumn)),'(','')when PATINDEX('%/%',TColumn)<> 0 then replace(SUBSTRING(TColumn,PATINDEX('(',TColumn), PATINDEX('%/%',TColumn)),'(','') else '' end as repfrom table quote: Originally posted by vinu.vijayan This might help.--Creating TableCreate Table Ex(col1 varchar(30), col2 varchar(30) )--Inserting Sample DataInsert Into ExSelect 'pressue*0.897', ''Union ALLSelect 'height*1.65', ''Union ALLSelect 'breadth/12', ''--Query for Your Requirement(Method 1)Select Col1,(Case When Col1 = 'pressue*0.897' then 'pressure' When Col1 = 'height*1.65' then 'height' When Col1 = 'breadth/12' then 'breadth' Else '' End) As Col2From Ex--Query for Your Requirement(Method 2)Select Col1,(Case When Col1 = 'pressue*0.897' then SUBSTRING(Col1, 1, (CHARINDEX('*',Col1,1)-1) ) When Col1 = 'height*1.65' then SUBSTRING(Col1, 1, (CHARINDEX('*',Col1,1)-1) ) When Col1 = 'breadth/12' then SUBSTRING(Col1, 1, (CHARINDEX('/',Col1,1)-1) ) Else '' End) As Col2From ExN 28° 33' 11.93148"E 77° 14' 33.66384"
Regards,Divya |
 |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-05-14 : 07:21:38
|
quote: Originally posted by divyaram Hi Vinu,Thx for your help... but data given above is jus a sample data only...the real scenario has a lots of data... i got out with logic like this.... i am posting here because it may help others in futureselect distinct DB,pColumn,ptable, TColumn,case when PATINDEX('%*%',TColumn)<> 0 then replace(SUBSTRING(TColumn,PATINDEX('(',TColumn), PATINDEX('%*%',TColumn)),'(','')when PATINDEX('%/%',TColumn)<> 0 then replace(SUBSTRING(TColumn,PATINDEX('(',TColumn), PATINDEX('%/%',TColumn)),'(','') else '' end as repfrom table quote: Originally posted by vinu.vijayan This might help.--Creating TableCreate Table Ex(col1 varchar(30), col2 varchar(30) )--Inserting Sample DataInsert Into ExSelect 'pressue*0.897', ''Union ALLSelect 'height*1.65', ''Union ALLSelect 'breadth/12', ''--Query for Your Requirement(Method 1)Select Col1,(Case When Col1 = 'pressue*0.897' then 'pressure' When Col1 = 'height*1.65' then 'height' When Col1 = 'breadth/12' then 'breadth' Else '' End) As Col2From Ex--Query for Your Requirement(Method 2)Select Col1,(Case When Col1 = 'pressue*0.897' then SUBSTRING(Col1, 1, (CHARINDEX('*',Col1,1)-1) ) When Col1 = 'height*1.65' then SUBSTRING(Col1, 1, (CHARINDEX('*',Col1,1)-1) ) When Col1 = 'breadth/12' then SUBSTRING(Col1, 1, (CHARINDEX('/',Col1,1)-1) ) Else '' End) As Col2From ExN 28° 33' 11.93148"E 77° 14' 33.66384"
Regards,Divya
Hi Divya,My query was just to give you a head start and I was waiting for the reply where you tell me that there is more data.That is when I was about to give you this query. But, you have worked it out already. Good work!! N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-05-14 : 09:33:19
|
| select col1,substring(col1,1,patindex('%[^a-z]%',col1)-1) as col2 from tableMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|