473,804 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add IF THEN clause to SELECT statement

I have a SQL 2000 table named IMSRejects with the fields:
IMSRejects.Prod uctDescription
IMSRejects.Vend orName
IMSRejects.Cata logNumber
IMSRejects.Prod uctIMS2Desc
IMSRejects.CMA8 Desc
Many of the ProductIMS2Desc and CMA8Desc values are Null. I have an ASP.NET
2.0 web page which populates a datagrid with this data. I also have text
boxes on my web page so users can filter the datagrid based on keywords. The
problem I'm having is that the SELECT statement that populates the datagrid
does no handle null values for ProductIMS2Desc and CMA8Desc. Here's the
statement:

Dim _SqlAccounts As String = "Select * FROM IMSRejects WHERE " & _
"IMSRejects.Pro ductDescription LIKE '%" & strProductName & "%' AND "
& _
"IMSRejects.Ven dorName LIKE '%" & strVendorName & "%' AND " & _
"IMSRejects.Cat alogNumber LIKE '%" & strCatalogName & "%' AND " & _
"IMSRejects.Pro ductIMS2Desc LIKE '%" & strFranchiseNam e & "%' AND "
& _
"IMSRejects.CMA 8Desc LIKE '%" & strProductLineN ame & "%' " & _
"ORDER BY VendorName ASC, ProductIMS2Desc ASC, CMA8Desc ASC"

I'd like to default to True a checkbox on my web page named cbShowAll and
add an IF clause that will show all records including those where
ProductIMS2Desc and CMA8Desc contain NULL values. When the checkbox is FALSE,
I'd like the grid to only show records ProductIMS2Desc and CMA8Desc don't
have NULL values.

THANKS!!
Jan 25 '07 #1
3 4973
Set up the query as a stored procedure rather than write it on the fly. You
will find that it is much easier to control the paths by sending a single
boolean than building a statement blindly.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************** *************** **************
Think outside the box!
*************** *************** **************
"Mike C" <Mi***@discussi ons.microsoft.c omwrote in message
news:C0******** *************** ***********@mic rosoft.com...
>I have a SQL 2000 table named IMSRejects with the fields:
IMSRejects.Prod uctDescription
IMSRejects.Vend orName
IMSRejects.Cata logNumber
IMSRejects.Prod uctIMS2Desc
IMSRejects.CMA8 Desc
Many of the ProductIMS2Desc and CMA8Desc values are Null. I have an
ASP.NET
2.0 web page which populates a datagrid with this data. I also have text
boxes on my web page so users can filter the datagrid based on keywords.
The
problem I'm having is that the SELECT statement that populates the
datagrid
does no handle null values for ProductIMS2Desc and CMA8Desc. Here's the
statement:

Dim _SqlAccounts As String = "Select * FROM IMSRejects WHERE " & _
"IMSRejects.Pro ductDescription LIKE '%" & strProductName & "%' AND
"
& _
"IMSRejects.Ven dorName LIKE '%" & strVendorName & "%' AND " & _
"IMSRejects.Cat alogNumber LIKE '%" & strCatalogName & "%' AND " & _
"IMSRejects.Pro ductIMS2Desc LIKE '%" & strFranchiseNam e & "%' AND "
& _
"IMSRejects.CMA 8Desc LIKE '%" & strProductLineN ame & "%' " & _
"ORDER BY VendorName ASC, ProductIMS2Desc ASC, CMA8Desc ASC"

I'd like to default to True a checkbox on my web page named cbShowAll and
add an IF clause that will show all records including those where
ProductIMS2Desc and CMA8Desc contain NULL values. When the checkbox is
FALSE,
I'd like the grid to only show records ProductIMS2Desc and CMA8Desc don't
have NULL values.

THANKS!!
Jan 25 '07 #2
Mike,

As Cowboy suggested, I also think you should use a stored proc for this.

To address your NULL value problems; If you have the option, make the
database columns ProductIMS2Desc and CMA8Desc not nullable. Use empty
strings ('') instead of NULLs.

Sometimes this not always possible, so the second thing you could do is to
use the t-sql coalesce() or isnull() functions to help handle the database
NULL values.

Your where clause would be something like this:
....
coalesce(IMSRej ects.ProductIMS 2Desc, '') LIKE '%yabayaba%'
....

or if you used the isnull() function
.....
isnull(IMSRejec ts.ProductIMS2D esc, '') LIKE '%yabayaba%'
.....

Your VB code should look something like the code below. I've used a
StringBuilder to help with performance and readability on the string
contatenations. I'd recommend using SelectParameter s to help prevent sql
injection attacks (see
http://www.codeproject.com/cs/databa...gAdHocSQL.asp).
Also the _SqlSource variable is your SqlDataSource object that your GridView
is bound to.

Hope this helps,
Jason Vermillion

Dim _GenSql As StringBuilder = New StringBuilder(2 56)
Dim strProductName As String = ""
Dim strVendorName As String = ""
Dim strCatalogName As String = ""
Dim strFranchiseNam e As String = ""
Dim strProductLineN ame As String = ""
Dim _SqlAccounts As String = ""

strProductName = "%" & strProductName & "%"
strVendorName = "%" & strVendorName & "%"
strCatalogName = "%" & strCatalogName & "%"
strFranchiseNam e = "%" & strFranchiseNam e & "%"
strProductLineN ame = "%" & strProductLineN ame & "%"

_GenSql.AppendL ine("Select * FROM IMSRejects WHERE")
_GenSql.AppendL ine("IMSRejects .ProductDescrip tion LIKE @ProductName
AND")
_GenSql.AppendL ine("IMSRejects .VendorName LIKE @VendorName AND")
_GenSql.AppendL ine("IMSRejects .CatalogNumber LIKE @CatalogName AND")

' Use the t-sql Coalesce() or isnull() function to
If (Me.cbShowAll.C hecked = True) Then
_GenSql.AppendL ine("coalesce(I MSRejects.Produ ctIMS2Desc,'') LIKE
@FranchiseName AND")
_GenSql.AppendL ine("coalesce(I MSRejects.CMA8D esc,'') LIKE
@ProductLineNam e")

Else
_GenSql.AppendL ine("IMSRejects .ProductIMS2Des c LIKE
@FranchiseName AND")
_GenSql.AppendL ine("IMSRejects .CMA8Desc LIKE @ProductLineNam e")
End If

_GenSql.AppendL ine("ORDER BY VendorName ASC, ProductIMS2Desc ASC,
CMA8Desc ASC")

_SqlAccounts = _GenSql.ToStrin g()

_SqlSource.Sele ctCommand = _SqlAccounts
_SqlSource.Sele ctParameters.Ad d("ProductName" , strProductName)
_SqlSource.Sele ctParameters.Ad d("VendorName ", strVendorName)
_SqlSource.Sele ctParameters.Ad d("CatalogName" , strCatalogName)
_SqlSource.Sele ctParameters.Ad d("FranchiseNam e", strFranchiseNam e)
_SqlSource.Sele ctParameters.Ad d("ProductLineN ame", strProductLineN ame)

Jan 26 '07 #3
That worked great. Thank you! I have some parts of my code as stored
procedures but still trying to understand the syntax for others.

"Jason Vermillion" wrote:
Mike,

As Cowboy suggested, I also think you should use a stored proc for this.

To address your NULL value problems; If you have the option, make the
database columns ProductIMS2Desc and CMA8Desc not nullable. Use empty
strings ('') instead of NULLs.

Sometimes this not always possible, so the second thing you could do is to
use the t-sql coalesce() or isnull() functions to help handle the database
NULL values.

Your where clause would be something like this:
...
coalesce(IMSRej ects.ProductIMS 2Desc, '') LIKE '%yabayaba%'
...

or if you used the isnull() function
....
isnull(IMSRejec ts.ProductIMS2D esc, '') LIKE '%yabayaba%'
....

Your VB code should look something like the code below. I've used a
StringBuilder to help with performance and readability on the string
contatenations. I'd recommend using SelectParameter s to help prevent sql
injection attacks (see
http://www.codeproject.com/cs/databa...gAdHocSQL.asp).
Also the _SqlSource variable is your SqlDataSource object that your GridView
is bound to.

Hope this helps,
Jason Vermillion

Dim _GenSql As StringBuilder = New StringBuilder(2 56)
Dim strProductName As String = ""
Dim strVendorName As String = ""
Dim strCatalogName As String = ""
Dim strFranchiseNam e As String = ""
Dim strProductLineN ame As String = ""
Dim _SqlAccounts As String = ""

strProductName = "%" & strProductName & "%"
strVendorName = "%" & strVendorName & "%"
strCatalogName = "%" & strCatalogName & "%"
strFranchiseNam e = "%" & strFranchiseNam e & "%"
strProductLineN ame = "%" & strProductLineN ame & "%"

_GenSql.AppendL ine("Select * FROM IMSRejects WHERE")
_GenSql.AppendL ine("IMSRejects .ProductDescrip tion LIKE @ProductName
AND")
_GenSql.AppendL ine("IMSRejects .VendorName LIKE @VendorName AND")
_GenSql.AppendL ine("IMSRejects .CatalogNumber LIKE @CatalogName AND")

' Use the t-sql Coalesce() or isnull() function to
If (Me.cbShowAll.C hecked = True) Then
_GenSql.AppendL ine("coalesce(I MSRejects.Produ ctIMS2Desc,'') LIKE
@FranchiseName AND")
_GenSql.AppendL ine("coalesce(I MSRejects.CMA8D esc,'') LIKE
@ProductLineNam e")

Else
_GenSql.AppendL ine("IMSRejects .ProductIMS2Des c LIKE
@FranchiseName AND")
_GenSql.AppendL ine("IMSRejects .CMA8Desc LIKE @ProductLineNam e")
End If

_GenSql.AppendL ine("ORDER BY VendorName ASC, ProductIMS2Desc ASC,
CMA8Desc ASC")

_SqlAccounts = _GenSql.ToStrin g()

_SqlSource.Sele ctCommand = _SqlAccounts
_SqlSource.Sele ctParameters.Ad d("ProductName" , strProductName)
_SqlSource.Sele ctParameters.Ad d("VendorName ", strVendorName)
_SqlSource.Sele ctParameters.Ad d("CatalogName" , strCatalogName)
_SqlSource.Sele ctParameters.Ad d("FranchiseNam e", strFranchiseNam e)
_SqlSource.Sele ctParameters.Ad d("ProductLineN ame", strProductLineN ame)
Feb 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2843
by: Summa | last post by:
Hi NG, I have the following problem that I hope you can help me with (MS-SQL server 2000) Imagine a statement like this: "select id, firstname, (select top 1 id from testdata) as testid, lastname from nametable order by firstname"
1
3534
by: Kenneth P | last post by:
Hi, I'm trying to do some Custom Paging technique with the datagrid object and with the select command in Sql, thus forcing the server to only select those rows from the database that should be rendered in the datagrid object. This works fine with the code I have in MsSql2k but now I'm trying to do the same with MySql v4.1 database and it doesn't support the 'TOP' command/function/clause and I'm looking for some equivalence in MySql...
4
1923
by: louise raisbeck | last post by:
I have this scenario (simplified) function addnewdata () { check for partial match already in db for information entered by user if (partialmatch succeeds) { open new window aspx page (using javascript) with a datagrid of these partial match records (by doing a sqlcommand using some query string values taken from opener data entered) *** }
2
5573
by: Michael C | last post by:
Hi all. When I run the following query against my Access database - from within Access - it returns the correct results (5 records in this instance): SELECT * FROM WHERE = "Clothes" AND LIKE "*brown*" However, when I run it from my ASP.NET page via an OleDBCommand, it returns 0 records every time. Now for the kicker: When I drop the LIKE clause, the query returns the correct results (10 records in this instance) from within...
9
11089
by: Acupuncture | last post by:
Hi, I am developing a JDBC application and I encountered this problem (DB2 for ISeries). I want to do a select for update and also use the fetch first rows clause. This is my sql statement: SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte FROM USERS WHERE UsrNbr = ? FETCH FIRST 1 ROWS ONLY FOR UPDATE This runs fine (the cursor gets this name 'P00022'), but when I execute the
26
17216
by: GreatAlterEgo | last post by:
Hi, This is my query which is embedded in a COBOL program. EXEC SQL SELECT DATE, AGE, DURATION, AMT INTO :LDATE, :L.AGE, :L.DURATION, :L.AMT FROM TAB1 WHERE CODE = :KEY.CODE AND SET = :KEY.SET AND DATE <= :KEY.DATE
5
2666
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser, (@ageraw:=YEAR(CURRENT_DATE()) - YEAR(dateofbirth) -
1
2733
by: ebo2006 | last post by:
There are two combo boxes in my form: CORP and CLASS. The WHERE clause of the SELECT statement in the Row Source Property of the CLASS combo box works only every time I start the form. It does not change the list options when I select another ID from CORP combo box. Why is this so? Here is my select statement SELECT ., . FROM Courses WHERE .=form.ID ORDER BY .; tblTYPE is the table for CLASS.
3
13259
by: Rahul Babbar | last post by:
Hi, I had the following doubts about the "For Read Only" clause. 1. How does a "for Read only" clause improve the performance? 2. How does a "for Read only" clause compare with "With UR" clause in performance? Which is faster? Can someone clarify on that?
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.