Solved

SQL Statement needs to use if and else

Posted on 2014-04-09
5
150 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
ID: 39990102
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
ID: 39990201
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
ID: 39990543
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
ID: 39991332
Thanks so much
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

27 Experts available now in Live!

Get 1:1 Help Now