PC Review


Reply
Thread Tools Rate Thread

Add IF THEN clause to SELECT statement

 
 
=?Utf-8?B?TWlrZSBD?=
Guest
Posts: n/a
 
      25th Jan 2007
I have a SQL 2000 table named IMSRejects with the fields:
IMSRejects.ProductDescription
IMSRejects.VendorName
IMSRejects.CatalogNumber
IMSRejects.ProductIMS2Desc
IMSRejects.CMA8Desc
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.ProductDescription LIKE '%" & strProductName & "%' AND "
& _
"IMSRejects.VendorName LIKE '%" & strVendorName & "%' AND " & _
"IMSRejects.CatalogNumber LIKE '%" & strCatalogName & "%' AND " & _
"IMSRejects.ProductIMS2Desc LIKE '%" & strFranchiseName & "%' AND "
& _
"IMSRejects.CMA8Desc LIKE '%" & strProductLineName & "%' " & _
"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!!
 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      25th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:C086C809-DF9B-4F12-A89F-(E-Mail Removed)...
>I have a SQL 2000 table named IMSRejects with the fields:
> IMSRejects.ProductDescription
> IMSRejects.VendorName
> IMSRejects.CatalogNumber
> IMSRejects.ProductIMS2Desc
> IMSRejects.CMA8Desc
> 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.ProductDescription LIKE '%" & strProductName & "%' AND
> "
> & _
> "IMSRejects.VendorName LIKE '%" & strVendorName & "%' AND " & _
> "IMSRejects.CatalogNumber LIKE '%" & strCatalogName & "%' AND " & _
> "IMSRejects.ProductIMS2Desc LIKE '%" & strFranchiseName & "%' AND "
> & _
> "IMSRejects.CMA8Desc LIKE '%" & strProductLineName & "%' " & _
> "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!!


 
Reply With Quote
 
=?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
Guest
Posts: n/a
 
      26th Jan 2007
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(IMSRejects.ProductIMS2Desc, '') LIKE '%yabayaba%'
....

or if you used the isnull() function
.....
isnull(IMSRejects.ProductIMS2Desc, '') 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 SelectParameters to help prevent sql
injection attacks (see
http://www.codeproject.com/cs/databa...ngAdHocSQL.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(256)
Dim strProductName As String = ""
Dim strVendorName As String = ""
Dim strCatalogName As String = ""
Dim strFranchiseName As String = ""
Dim strProductLineName As String = ""
Dim _SqlAccounts As String = ""

strProductName = "%" & strProductName & "%"
strVendorName = "%" & strVendorName & "%"
strCatalogName = "%" & strCatalogName & "%"
strFranchiseName = "%" & strFranchiseName & "%"
strProductLineName = "%" & strProductLineName & "%"

_GenSql.AppendLine("Select * FROM IMSRejects WHERE")
_GenSql.AppendLine("IMSRejects.ProductDescription LIKE @ProductName
AND")
_GenSql.AppendLine("IMSRejects.VendorName LIKE @VendorName AND")
_GenSql.AppendLine("IMSRejects.CatalogNumber LIKE @CatalogName AND")

' Use the t-sql Coalesce() or isnull() function to
If (Me.cbShowAll.Checked = True) Then
_GenSql.AppendLine("coalesce(IMSRejects.ProductIMS2Desc,'') LIKE
@FranchiseName AND")
_GenSql.AppendLine("coalesce(IMSRejects.CMA8Desc,'') LIKE
@ProductLineName")

Else
_GenSql.AppendLine("IMSRejects.ProductIMS2Desc LIKE
@FranchiseName AND")
_GenSql.AppendLine("IMSRejects.CMA8Desc LIKE @ProductLineName")
End If

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

_SqlAccounts = _GenSql.ToString()

_SqlSource.SelectCommand = _SqlAccounts
_SqlSource.SelectParameters.Add("ProductName", strProductName)
_SqlSource.SelectParameters.Add("VendorName", strVendorName)
_SqlSource.SelectParameters.Add("CatalogName", strCatalogName)
_SqlSource.SelectParameters.Add("FranchiseName", strFranchiseName)
_SqlSource.SelectParameters.Add("ProductLineName", strProductLineName)

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBD?=
Guest
Posts: n/a
 
      9th Feb 2007
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(IMSRejects.ProductIMS2Desc, '') LIKE '%yabayaba%'
> ...
>
> or if you used the isnull() function
> ....
> isnull(IMSRejects.ProductIMS2Desc, '') 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 SelectParameters to help prevent sql
> injection attacks (see
> http://www.codeproject.com/cs/databa...ngAdHocSQL.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(256)
> Dim strProductName As String = ""
> Dim strVendorName As String = ""
> Dim strCatalogName As String = ""
> Dim strFranchiseName As String = ""
> Dim strProductLineName As String = ""
> Dim _SqlAccounts As String = ""
>
> strProductName = "%" & strProductName & "%"
> strVendorName = "%" & strVendorName & "%"
> strCatalogName = "%" & strCatalogName & "%"
> strFranchiseName = "%" & strFranchiseName & "%"
> strProductLineName = "%" & strProductLineName & "%"
>
> _GenSql.AppendLine("Select * FROM IMSRejects WHERE")
> _GenSql.AppendLine("IMSRejects.ProductDescription LIKE @ProductName
> AND")
> _GenSql.AppendLine("IMSRejects.VendorName LIKE @VendorName AND")
> _GenSql.AppendLine("IMSRejects.CatalogNumber LIKE @CatalogName AND")
>
> ' Use the t-sql Coalesce() or isnull() function to
> If (Me.cbShowAll.Checked = True) Then
> _GenSql.AppendLine("coalesce(IMSRejects.ProductIMS2Desc,'') LIKE
> @FranchiseName AND")
> _GenSql.AppendLine("coalesce(IMSRejects.CMA8Desc,'') LIKE
> @ProductLineName")
>
> Else
> _GenSql.AppendLine("IMSRejects.ProductIMS2Desc LIKE
> @FranchiseName AND")
> _GenSql.AppendLine("IMSRejects.CMA8Desc LIKE @ProductLineName")
> End If
>
> _GenSql.AppendLine("ORDER BY VendorName ASC, ProductIMS2Desc ASC,
> CMA8Desc ASC")
>
> _SqlAccounts = _GenSql.ToString()
>
> _SqlSource.SelectCommand = _SqlAccounts
> _SqlSource.SelectParameters.Add("ProductName", strProductName)
> _SqlSource.SelectParameters.Add("VendorName", strVendorName)
> _SqlSource.SelectParameters.Add("CatalogName", strCatalogName)
> _SqlSource.SelectParameters.Add("FranchiseName", strFranchiseName)
> _SqlSource.SelectParameters.Add("ProductLineName", strProductLineName)
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where Clause Being Added to Select Statement JamesJ Microsoft Access Forms 2 19th Feb 2009 05:23 PM
If statement in Where Clause of SQL? BlueWolverine Microsoft Access Queries 1 10th Dec 2008 01:58 PM
Can I use a between clause or in clause on an IF statement =?Utf-8?B?c3NjaWFycmlubw==?= Microsoft Excel Programming 2 4th May 2007 04:48 PM
stored procedure select statement with where clause =?Utf-8?B?c3M=?= Microsoft ADO .NET 4 9th May 2006 05:25 PM
sql statement with if clause? Dan Nash Microsoft Access Queries 3 10th Dec 2003 02:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:47 PM.