sql-server


SQL Multi-condition WHERE clause with one optional parameter?


Reworking an existing Stored Procedure to make a previously required parameter optional.
The query is massive, and deals with much more than my current issue, so I won't post it in it's entirety but I believe I can get my fix by editing one of the WHERE clauses.
WHERE
O.wsID = #wsid AND
((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate))
The first parameter #wsid is the value I need to make optional, it's initially declared as 0 and given a new value from a select box on the front-end of the website. I have attempted to use a CASE statement to alternate between to WHERE clauses depending on whether the value is 0 or an actual ID.
WHERE
#wsid = CASE
WHEN #wsid <> 0 THEN ((O.wsID = #wsid) AND ((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate)))
ELSE ((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate)))
END
But I get a bunch of syntax errors in the WHERE clause itself and also an error stating Expecting CONVERSATION or TRY for the SELECT statement below.
Is there an easier option for making the O.wsid = #wsid parameter optional?
WHERE
(O.wsID = #wsid OR #wsid = 0) AND
((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate))

Related Links

SQL Server High availability User Login
How to use a computed column in an if/else
update the table with the previous column with out any loops
T-SQL DELETE Statement returns Subquery hint “returns more than one value…”
Need help to format output of SQL Server XML sibling query
Visual Studio Published Project Crashing when trying to access Database files
ResolutionPath for SQL Server
SSIS SQL task Query creating error while casting parameters (string to Datetime)
How to display records for only 3 days
range sql query result range
Why is SSIS ignoring the last column of my import from an Excel source?
SqlDependency not working
Reset Identity of remote table
Query values from another table based on column value
CDC (Change Data Capture) is not harvesting logs and log file continues to grow
How I can select all gruop of the Active Directory?

Categories

HOME
paypal
caching
numpy
elixir
itext
visual-studio-2013
avro
google-sheets-api
automated-tests
sequelize.js
apiconnect
websphere-liberty
httpclient
flexbox
data-synchronization
tee
symfony-forms
datagrip
cloudflare
pdo
jquery-terminal
dendrogram
quartus
device-detection
flexboxgrid
excel-2010
deb
sensu
google-ima
facebook4j
tarantool
restfb
yosys
skmaps
edge-detection
pcre
positioning
websauna
hibernate-cache
rhel.net
coreclr
topic-modeling
ddms
react-native-router-flux
upsert
rich-text-editor
facebook-chatbot
bids
multi-agent
chrome-remote-desktop
uicollectionviewlayout
redux-router
libtiff.net
cfeclipse
galleriffic
rstudio-server
multiple-file-upload
shipitjs
fps
gmsmapview
self-hosting
getrusage
android-viewholder
pyrocms
achartengine
coypu
cpu-cores
pgm
docopt
funq
power-law
korma
ifft
android-json-rpc
arangodb-php
convex-polygon
sslexception
installshield-2009
apache2.2
vertical-scrolling
vim-powerline
couchdb-lucene
javascriptserializer
n-layer
static-variables
gdt
handwriting
ruby-debug
nvelocity
audiostreamer
symbol-server
hadoop-plugins
graph-layout
web-statistics
microsoft.ink
visual-studio-2010-beta-2
database-diagramming

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App