[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

If Statement in SQL Stored Procedure

Posted on 2012-03-10
4
Medium Priority
?
265 Views
Last Modified: 2012-03-10
I have the select statement below.

I am trying to create an if statement if the alias DAY is Null or > 0 then a new alias Late should = 1 otherwise it would be zero.  Everything I tried messes up the prior selections.

SELECT    distinct  ph.NAME_VND_ORDFM as Name_Vend,ph.ID_PO as PO_NBR,  pl.ID_REL_ORD AS PO_REL,pl.ID_LINE_PO AS PO_LN,  CONVERT(VARCHAR,pl.DATE_RCV,101)as RECEIPT,
                      CONVERT(VARCHAR,pl.DATE_RQST,101) as REQUEST, DATEDIFF(DAY, pl.DATE_RQST,pl.DATE_RCV) AS DAY

Any help will be appreciated.
0
Comment
Question by:Scamquist
[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
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 37704998
this should do:
SELECT    distinct  ph.NAME_VND_ORDFM as Name_Vend,ph.ID_PO as PO_NBR,  pl.ID_REL_ORD AS PO_REL,pl.ID_LINE_PO AS PO_LN,  CONVERT(VARCHAR,pl.DATE_RCV,101)as RECEIPT,
                      CONVERT(VARCHAR,pl.DATE_RQST,101) as REQUEST, DATEDIFF(DAY, pl.DATE_RQST,pl.DATE_RCV) AS DAY
, CASE WHEN ISNULL( DATEDIFF(DAY, pl.DATE_RQST,pl.DATE_RCV), 100) > 0 THEN 1 ELSE 0 END IsLate 

Open in new window


you cannot use the "alias" directly in the same query level, you have to repeat the expression. the only place where you could use the alias is in the ORDER BY part.
0
 
LVL 1

Author Comment

by:Scamquist
ID: 37705092
Your answer worked.  Can you explain the syntax, mainly the ,100)>0

CASE WHEN ISNULL( DATEDIFF(DAY, pl.DATE_RQST,pl.DATE_RCV), 100) > 0 THEN 1 ELSE 0 END IsLate
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37705139
if the value returned is null, the isnull(xxx, 100) will return 100.
as you said that null value returned should also display 1 as result, I used 100 as value, 1 or 99999 would have worked the same way.
0
 
LVL 1

Author Closing Comment

by:Scamquist
ID: 37705157
Thank you for the assist and the explaination.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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