Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to short circuit if value passed in is null, do something if is not null

Posted on 2015-02-09
2
Medium Priority
?
67 Views
Last Modified: 2015-02-09
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'unsp_ApprenticeAdvancementReportLoad')
	DROP PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad
GO

CREATE PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad
(
	@LocNums nvarchar(2000) = null,
	@DateBegin datetime = null,
	@DateEnd datetime = null,
	@SortBy nvarchar(50) = null
)
AS

SELECT DISTINCT mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM foo.Apprentices mainAppr
	LEFT JOIN foo.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN foo.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN foo.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN foo.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND ((@LocNums is null) OR lu.LocalNumber IN (UNAdmin.unf_SplitToString(@LocNums, ',')))
AND ((@DateBegin is null) OR (AdvancementDate >= @DateBegin))
AND @DateEnd is null OR AdvancementDate <= @DateEnd
ORDER BY ((@sortby is null) OR @SortBy)

Open in new window



This part:

AND ((@LocNums is null) OR lu.LocalNumber IN (UNAdmin.unf_SplitToString(@LocNums, ',')))
AND ((@DateBegin is null) OR (AdvancementDate >= @DateBegin))
AND @DateEnd is null OR AdvancementDate <= @DateEnd
ORDER BY ((@sortby is null) OR @SortBy)

Open in new window


Keeps saying:

Msg 156, Level 15, State 1, Procedure unsp_ApprenticeAdvancementReportLoad, Line 28
Incorrect syntax near the keyword 'is'.

line 28:

AND ((@DateBegin is null) OR (AdvancementDate >= @DateBegin))


Is my TSQL valid and am I doing this correctly or is there a better way.

[[SQL Server 2008, btw.]]
0
Comment
Question by:Tom Knowlton
[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 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 40599725
The problem is the ORDER BY clause, not the WHERE clause.

try this please:

ORDER BY ISNULL(@SortBy,'')

{+edit} there is a set of parentheses missing in the where clause

            AND (@DateEnd IS NULL
                  OR AdvancementDate <= @DateEnd)


IF EXISTS (
            SELECT
                  *
            FROM sysobjects
            WHERE type = 'P'
                  AND name = 'unsp_ApprenticeAdvancementReportLoad'
      )
      DROP PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad
GO

CREATE PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad (@LocNums nvarchar(2000) = NULL,
@DateBegin datetime = NULL,
@DateEnd datetime = NULL,
@SortBy nvarchar(50) = NULL)
AS

      SELECT DISTINCT
            MAINAPPR.LastName
          , MAINAPPR.FirstName
          , MAINAPPR.UBCID UBCID
          , LU.LocalNumber LOCALNUMBER
          , LU.ID LOCALUNIONID
          , AP.Period
          , AP.PercentOfJourneyWages ADVANCEMENTPER
          , AAH.dayAdvancementDate ADVANCEMENTDATE
      FROM foo.Apprentices MAINAPPR
            LEFT JOIN foo.ApprenticeAdvancementHistory AAH ON AAH.ID = MAINAPPR.CurrentAdvancementID
            LEFT JOIN foo.ApprenticePeriods AP ON AAH.ApprenticePeriodID = AP.ID
            LEFT JOIN foo.LocalUnions LU ON LU.ID = MAINAPPR.LocalUnionID
            LEFT JOIN foo.ApprenticeStatus APPRSTATUS ON MAINAPPR.ApprenticeTypeID = APPRSTATUS.ApprenticeTypeID
      WHERE APPRSTATUS.IsInProgram = 1
            AND ((@LocNums IS NULL)
                  OR LU.LocalNumber IN (UNAdmin.unf_SplitToString(@LocNums, ',')))
            AND ((@DateBegin IS NULL)
                  OR (AdvancementDate >= @DateBegin))
            AND (@DateEnd IS NULL
                  OR AdvancementDate <= @DateEnd)
ORDER BY ISNULL(@SortBy,'')

Open in new window

0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 40599753
That was it!!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

671 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