Solved

SQL Statement needs to use if and else

Posted on 2014-04-09
5
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 49

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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