question

MituMa avatar image
MituMa asked

How to retrieve records with only N in B column.

A B

101 N

102 N

103 N

103 Y

104 N

104 N

105 Y

result should have only 101,102,104.How to implement this.

sqlserver2012
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sankum avatar image
sankum answered

'104N'

select SUBSTRING(b,0, LEN(b)-1) AS Bun from tablename where b like '%N'

select * from tablename where B like '%N'

select * from tablename where B = 'N'


10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image
ThomasRushton answered

So you want to SELECT all rows from your table where there's a record for field B = 'N', and nothing else? And if there is something else, then you don't want to see that record even if there's an 'N' value?


Something like

select * from tablename 
where a in (select a from tablename where b = 'N')
and a not in (select a from tablename where b <> 'N')


That'll return duplicates for the records where A = 104, though, so you might want to look at SELECT DISTINCT...

There are almost certainly other ways to do it - some of them may be more efficient. But that should get you started.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.