August 24, 2009 at 4:48 am
Hi All
I have Nvarchar Column in a table which contains values like
1,2,3,4
1,4
25,26,27,28,29,30,31,32
2
I want to find out number of Rows in a table containg only '2'.
(ie. Query should return me 2 as Result in Above case)
i have tried 'IN' and 'LIKE' operators ,However both are failing me
to give correct results.
If anyone can suggest me on this!!!
August 24, 2009 at 5:12 am
Are you not making this more complicated that it needs to be ?
Select * from table where col = '2'
Will do what you need.
August 24, 2009 at 5:25 am
Hi,
This query will return me only 1 record. ie. Record with only 2 in it.
Regards,
August 24, 2009 at 5:31 am
Isnt that what you wanted ?
I want to find out number of Rows in a table containg only '2'.
August 24, 2009 at 5:42 am
Nope.
I wanted number of rows containing ONLY 2
Regards,
August 24, 2009 at 5:47 am
If what you posted is interpreted as 'result should show the 2 rows which contains the digit 2' then
Select * from table where ','+col+',' LIKE '%,2,%'
and
Select COUNT(*) from table where ','+col+',' LIKE '%,2,%'
will give you the number of rows
But performance will be bad as a table or index scan will be required
*Editied to remove = signs :blush:
Far away is close at hand in the images of elsewhere.
Anon.
August 24, 2009 at 5:47 am
Still not clear ...
Please post a script to create the table and insert values
August 24, 2009 at 5:47 am
That's what Dave's given you. I think what you mean is the number of rows that contain a "2", but not "12" or "20" etc. So the row 1,2,3,4 would count, but 10,11,12 would not...
You could search for where (col = '2' or col = ',2' or col = ',2,' or col = '2,'). It's very messy, and could probably be done better in a case statement or a substring.
August 24, 2009 at 5:48 am
So, to get the number of rows you'd just modify the query to do a count
SELECT COUNT(*)
FROM table
WHERE col = '2'
It's just a straight aggregate function. If you need to add other columns besides the count, simply GROUP BY the appropriate column.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
August 24, 2009 at 5:53 am
drop table #t1
go
create table #t1
(
RowId integer,
ValueStr nvarchar(50)
)
go
insert into #t1 values(1,'1,2,3,4')
insert into #t1 values(2,'4')
insert into #t1 values(3,'25,26,27,28,29,30,31,32')
insert into #t1 values(4,'2')
go
select * from #t1 where ValueStr = '2'
go
select * from #t1 where ValueStr like '%2%'
With this are you expecting Rows with the id of 1 and 4 or only 4 ????
August 24, 2009 at 6:04 am
Id of 1 and 4.
I should get result as
RowId ValueStr
1 1,2,3,4
4 2
Regards,
Nilesh.
August 24, 2009 at 6:08 am
Id of 1 and 4
I should get result as
RowId ValueStr
1 1,2,3,4
4 2
August 24, 2009 at 6:09 am
hI,
This will work
like '[2]' or i like '%,2,%'
August 24, 2009 at 6:09 am
if apply David Burrows's suggestion in the Dave's code it would be
select * from #t1 where ','+ValueStr+',' like '%,2,%'
no?
August 24, 2009 at 6:11 am
If the answer to that is 1 & 4, then Dave had is spot on with:
drop table #t1
go
create table #t1
(
RowId integer,
ValueStr nvarchar(50)
)
go
insert into #t1 values(1,'1,2,3,4')
insert into #t1 values(2,'4')
insert into #t1 values(3,'25,26,27,28,29,30,31,32')
insert into #t1 values(4,'2')
go
select * from #t1 where ','+ValueStr+',' LIKE '%,2,%'
go
That will return 1 & 4. If you adjust that to
select count (*) from #t1 where ','+ValueStr+',' LIKE '%,2,%'
go
then you'll get your count.
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply