Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2006, 05:37 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

HI Imar,

I'll give your theory a go and let you know how I get on. My full query does in include 2 conditional WHERE clauses (as in above) and some standard comparison ones so brackets will come in handy.

Rit
 
Old February 14th, 2006, 06:14 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

I can see a www.tributes2imar.com in the making!.

Imar, thanks very much, it worked great and easily digestable for my mind.

Rit
 
Old May 30th, 2006, 07:02 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Hi All

Based on Imar's theory of:

SELECT SomeField FROM SomeTable
WHERE SomeField = @someParam OR @someParam = 1

How could I set this up to work with NULLs i.e.

@someParam INT

SELECT SomeField FROM SomeTable
WHERE SomeField = @someParam OR @someParam = NULL

When I try it it doesn't respond how I would have expected it to.. It seems to have a problem with NULL and doesn't return any records.

Many thanks

Rit
 
Old May 30th, 2006, 07:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

No value, parameter or table column, can ever be equal to NULL, regardless of what value is stored there (even NULL).

You need to test if a value IS NULL, as:

SELECT SomeField FROM SomeTable
WHERE SomeField = @someParam OR @someParam IS NULL

This could also be written equivalently as:

SELECT SomeField FROM SomeTable
WHERE SomeField = coalesce(@someParam, SomeField)

This works becuase if @someParam IS NULL, then the coalesce function will return the value of SomeField, thus testing a column value for equality with itself, which of course is always true.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old February 1st, 2007, 01:13 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How can I do this same thing outside of a stored procedure (In a simple select statement)?
What I need to do is select * where name = Param or Param = ALL (to return one name or everyone).
Thanks,
Claudia

Quote:
quote:Originally posted by Imar
 Hi Rit,

You don't need an IF for this. You can simply compare the *parameter* with the required value.

E.g.:

SELECT SomeField FROM SomeTable
WHERE SomeField = @someParam OR @someParam = 1

So, when you pass 1 for the @someParam param, all records are returned, because 1 always equals one....

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 1st, 2007, 01:24 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Never mind. I've answered my own question.
Thanks anyways.

 
Old February 1st, 2007, 05:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Would you mind posting *how* you answered your own question?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old March 15th, 2007, 08:33 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to jdziggy Send a message via Yahoo to jdziggy
Default

Hey all,
I am trying to convert the following code into a stored procedure. I wonder if any of you could help me. It appears that it is saying if a string startswith "z_" then replace "z_" with "" and if Count is = 0 then use the first Where clause with the proceeding SQL statement else the second where clause.

For each item in Request.QueryString
                    if Left(item,2)="z_" Then
                        intCurrentVar = Replace(item, "z_", "")
                        if iCount = 0 Then
                            strWhereClause = " WHERE tblPCRDdata.Option_ID=" & intCurrentVar

                        else
                            strWhereClause = strWhereClause & " or tblPCRDdata.Option_ID=" & intCurrentVar
                        end if

                        End if

                    iCount = iCount + 1
                Next

               strReportSQL = "SELECT tblMain.*, tblAgency.Name AS Agency " _
                                & " FROM tblMain INNER JOIN " _
                                & " tblPCRDdata ON tblMain.ID = tblPCRDdata.Report_ID INNER JOIN " _
                              & " tblAgency ON tblMain.HHSAgency = tblAgency.ID " & strWhereClause

Code?  We don't need no stinkin' code!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Statement In Where Clause Logic dwj119 SQL Server 2000 4 October 27th, 2011 03:14 PM
If statement in WHERE clause frakey Classic ASP Basics 30 July 8th, 2008 03:35 PM
Problems using case statement in where clause vghiya SQL Server 2000 3 May 28th, 2007 05:12 AM
Select statement with where clause with 2 conditon Yasho VB.NET 2002/2003 Basics 7 May 16th, 2007 01:40 PM
If statement in Where Clause jdziggy SQL Server 2000 2 March 16th, 2007 08:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.