| Author |
Topic |
|
new_user
Starting Member
3 Posts |
Posted - 2007-04-26 : 09:58:56
|
| I have column that has records like1.2.11.2.21.2.1.11.2.1.1.11.2.91.2.9.3i need to display only 1.2.1 , 1.2.2 ,1.2.9 as the others contain one of these.I am able to do this in java. I want to do this using sql statement. Is this possible? appreciate ur reply. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-26 : 10:07:15
|
something like this ?declare @table table( col varchar(10))insert into @tableselect '1.2.1' union allselect '1.2.2' union allselect '1.2.1.1' union allselect '1.2.1.1.1' union allselect '1.2.9' union allselect '1.2.9.3'select a.colfrom @table a inner join @table bon a.col like b.col + '%'group by a.colhaving count(*) = 1/* result :col ---------- 1.2.11.2.21.2.9*/ KH |
 |
|
|
sshelper
Posting Yak Master
216 Posts |
Posted - 2007-04-26 : 10:07:23
|
| Try this:SELECT A.*FROM YourTable A INNER JOIN YourTable BON B.YourColumn LIKE A.YourColumn + '%' AND A.YourColumn != B.YourColumnSQL Server Helperhttp://www.sql-server-helper.com |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-26 : 11:21:20
|
Much simpler:declare @table table( col varchar(10))insert into @tableselect '1.2.1' union allselect '1.2.2' union allselect '1.2.1.1' union allselect '1.2.1.1.1' union allselect '1.2.9' union allselect '1.2.9.3'select colfrom @tablewhere col like '[0-9].[0-9].[0-9]' Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-26 : 11:26:37
|
Harsh, What if there is a record like '1.1' ? KH |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-26 : 11:35:15
|
Well, in that case I have to modify my solution to take of that as well.declare @table table( col varchar(10))insert into @tableselect '1.2.1' union allselect '1.2.2' union allselect '1.2.1.1' union allselect '1.2.1.1.1' union allselect '1.2.9' union allselect '1.2.9.3' union allselect '1.2' union allselect '1'select colfrom @tablewhere col like '[0-9].[0-9].[0-9]' -- x.x.x or col like '[0-9].[0-9]' -- x.x or col like '[0-9]' -- x One other situation where this solution won't work is when there are more than one digit in each section like, 11.274.42But, let the OP clearly specify if those conditions are part of his requirements. As a general rule, whenever, the solutions are made more generic, they are more complex and bit costly.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
new_user
Starting Member
3 Posts |
Posted - 2007-04-26 : 15:27:31
|
| thanks |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 16:44:25
|
| select colfrom table1where datalength(col) - datalength(replace(col, '.', '')) = 2Peter LarssonHelsingborg, Sweden |
 |
|
|
|