ahmed barbary
ahmed barbary

Reputation: 670

How to make select statement return rows have comma separated from field?

I work on SQL server 2012 I face issue:I can't make select statement

return only rows have comma separated from field ValueHaveComma ?

I need to do

select * from #seachvaluesHaveComma where ValueHaveComma contain comma

my sample as below

create table #seachvaluesHaveComma
(
ID INT IDENTITY(1,1),
ValueHaveComma nvarchar(100)
)
insert into #seachvaluesHaveComma(ValueHaveComma)
 values 
 ('1,2'),
 ('3.5,5.4'),
 ('a,b,c'),
 ('A')

Expected result as :

ID  ValueHaveComma
1   1,2
2   3.5,5.4
3   a,b,c

Upvotes: 0

Views: 37

Answers (2)

Jim Macaulay
Jim Macaulay

Reputation: 5155

You can get the results in two ways,

CHARINDEX

select * from #seachvaluesHaveComma where CHARINDEX(ValueHaveComma, ',') > 0;

LIKE

select * from #seachvaluesHaveComma where ValueHaveComma like '%,%';

Upvotes: 1

B.Muthamizhselvi
B.Muthamizhselvi

Reputation: 642

Try this query. this is simple query to get value having comma. WE need to use Like operator because you mentioned sql server.

select * from #seachvaluesHaveComma where ValueHaveComma like '%,%'

Upvotes: 2

Related Questions