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
Solved

If Statement in SQL Stored Procedure

Posted on 2012-03-10
4
260 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 143

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 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server - Sum Decimal Columns 11 45
sql 2014,  lock limit 5 37
SQL Insert parts by customer 12 42
TSQL - How to declare table name 26 43
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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