Solved

SQL Statement needs to use if and else

Posted on 2014-04-09
5
148 Views
Last Modified: 2014-04-10
I am trying to write what is seemingly a very simple statement that will retrieve 2 columns
1 part_no
2 qty_on_hand
However I need qty on hand to say if qty_on_hand =>30, 30, else qty_on_hand

the results should look like
1234.12345 30
1235.13265 5

SELECT item_no,
FROM MYTABLE

sadly above is all I have so far.
0
Comment
Question by:Jeremy Kirkbride
5 Comments
 
LVL 6

Accepted Solution

by:
Dulton earned 500 total points
Comment Utility
select item_no, case when qty_on_hand >= 30 THEN 30 ELSE qty_on_hand END as [Qty_On_hand]
from MyTable
0
 
LVL 28

Expert Comment

by:Bill Bach
Comment Utility
You list a couple of SQL engines in the TOPICS list, so it is unclear which you are using.  You may also be able to use the IF statement:

select item_no, IF(qty_on_hand >= 30,30,qty_on_hand) from MyTable
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Although Bill is likely to disagree with me I would suggest you stick with CASE expressions for SQL queries.

It is true that MySQL allows the syntax that Bill has provided, and a similar syntax is permitted in SQL Server 2012 like this:

select item_no, IIF(qty_on_hand >= 30,30,qty_on_hand) from MyTable

but both dbms types, and all versions I'm familiar with, support the CASE expression provided in the first answer.

No points please.
0
 

Author Closing Comment

by:Jeremy Kirkbride
Comment Utility
Thanks so much
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now