Solved

If Statement in SQL Stored Procedure

Posted on 2012-03-10
4
257 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
  • 2
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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 142

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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 …
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

21 Experts available now in Live!

Get 1:1 Help Now