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 |
shohelic
Starting Member
6 Posts |
Posted - 2007-01-24 : 01:01:49
|
I have a table:----------------Code----------------DHMOT1DHMOT2DHMOT3DHMOT3_18JSTWN1JSTWN2JSTWN2_18JSTWN3JSTWN4BSTWN1_18BSTWN2BSTWN3---------------Now I need to know the codes which have "_18" and also have one without "_18"The result will be like:----------------Code----------------DHMOT3_18JSTWN2_18---------------Can anyone help me pls.-Shohel |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-24 : 01:10:03
|
select * from yourtable where code like '%_18'Peter LarssonHelsingborg, Sweden |
 |
|
shohelic
Starting Member
6 Posts |
Posted - 2007-01-24 : 02:10:24
|
quote: Originally posted by Peso select * from yourtable where code like '%_18'Peter LarssonHelsingborg, Sweden
No Peter, I know this query but look carefully it have DHMOT3,DHMOT3_18(one have _18 and another not have). I want to fatch those data. If I use your syntex then it also return BSTWN1_18. But it is not valid, because it don't have any code without _18.Could I make it clear?-Shohel |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-24 : 02:17:08
|
[code]-- prepare sample datadeclare @t table (code varchar(20))insert @tselect 'DHMOT1' union allselect 'DHMOT2' union allselect 'DHMOT3' union allselect 'DHMOT3_18' union allselect 'JSTWN1' union allselect 'JSTWN2' union allselect 'JSTWN2_18' union allselect 'JSTWN3' union allselect 'JSTWN4' union allselect 'BSTWN1_18' union allselect 'BSTWN2' union allselect 'BSTWN3'-- show the resultSELECT t.CodeFROM @t AS tINNER JOIN ( SELECT LEFT(Code, 6) AS Code FROM @t GROUP BY LEFT(Code, 6) HAVING COUNT(*) > 1 ) AS d ON t.Code LIKE d.Code + '_%'ORDER BY t.Code[/code]Peter LarssonHelsingborg, Sweden |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-01-24 : 02:58:12
|
Or may be somthing like this ???Select t.Code From ( Select Code From @T Where code Like '%[_]%') As t Inner Join ( Select Code From @T Where code Not Like '%[_]%') as fOn t.Code Like '%' + f.Code + '%' Chiraghttp://chirikworld.blogspot.com/ |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-24 : 03:03:32
|
Nice!You could even drop the first %, like thist.Code Like '%' + f.Code + '%'Peter LarssonHelsingborg, Sweden |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-01-24 : 03:16:30
|
quote: Originally posted by Peso Nice!You could even drop the first %, like thist.Code Like '%' + f.Code + '%'Peter LarssonHelsingborg, Sweden
Oh yeah.. Chiraghttp://chirikworld.blogspot.com/ |
 |
|
shohelic
Starting Member
6 Posts |
Posted - 2007-01-24 : 04:46:26
|
Thanks,Thanks everybody for your cooperation.-Shohel |
 |
|
|
|
|
|
|