SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 IF statement with a OR?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Maverick_
Posting Yak Master

107 Posts

Posted - 08/20/2013 :  06:48:54  Show Profile  Reply with Quote
Hi guys,

I am trying to write an SQL query which goes something like:

count( if( {Feature Label} LIKE "%AB%" OR {Feature Label} LIKE "%CR",1,0) )

But it doesn't quite work. How can you write an OR inside an If statement?

Robowski
Posting Yak Master

101 Posts

Posted - 08/20/2013 :  07:25:54  Show Profile  Reply with Quote
SELECT
COUNT(1) AS TotalCount
FROM
Table
WHERE
Feature_Label LIKE '%ABC%' OR Feature_Label LIKE '%CR' --Not sure what the 1,0 is for sorry?

Or if you want a count for the labels

SELECT
Feature_label, COUNT(1) AS TotalCount
FROM
Table
WHERE
Feature_Label LIKE '%ABC%' OR Feature_Label LIKE '%CR'
GROUP BY
Feature_Label
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3760 Posts

Posted - 08/20/2013 :  08:23:24  Show Profile  Reply with Quote
quote:
Originally posted by Maverick_

Hi guys,

I am trying to write an SQL query which goes something like:

count( if( {Feature Label} LIKE "%AB%" OR {Feature Label} LIKE "%CR",1,0) )

But it doesn't quite work. How can you write an OR inside an If statement?

Are you using Microsoft SQL Server? Assuming you are, what you need is something like this:
SUM(
		CASE WHEN
			[Feature Label] LIKE '%AB%' OR [Feature Label] LIKE '%CR%' THEN 1
			ELSE 0
		END
	)
Note, I am using a CASE expression rather than if. IF is a control flow construct which cannot be used in this situation. Also, use SUM rather than COUNT.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22772 Posts

Posted - 08/20/2013 :  09:16:49  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also see why SUM is preferred over COUNT http://beyondrelational.com/modules/2/blogs/70/posts/19240/conditional-aggregation-sum-vs-count.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000