473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Prepared Statement where value IS null or value = :x

Hello,

I have a table which contains some nullable columns. I want to write a
single query, which can be prepared (ie. prepared statement), that can
handle null or non-null values for the where clause. Is this possible
in a standard-conforming manner?

The simple for of the query is this:

SELECT * FROM <table> WHERE <column> = <value>

But when the value to be matched on is NULL, the syntax of the query
must change to be:

SELECT * FROM <table> WHERE <column> IS <value>

In the second case <value> is NULL.

I know one option might be to change the ANSI NULL handling option,
but I am loathe to do this (I have five RDBMS's to support, not just
MSSSQL).

I thought I might have been able to cheat using an IN clause to make
the SQL consistent, but no luck.

Thanks

Kevin
Jul 20 '05 #1
6 15545
On 4 Apr 2004 23:46:17 -0700, Kevin Frey wrote:
Hello,

I have a table which contains some nullable columns. I want to write a
single query, which can be prepared (ie. prepared statement), that can
handle null or non-null values for the where clause. Is this possible
in a standard-conforming manner?

The simple for of the query is this:

SELECT * FROM <table> WHERE <column> = <value>

But when the value to be matched on is NULL, the syntax of the query
must change to be:

SELECT * FROM <table> WHERE <column> IS <value>

In the second case <value> is NULL.

I know one option might be to change the ANSI NULL handling option,
but I am loathe to do this (I have five RDBMS's to support, not just
MSSSQL).

I thought I might have been able to cheat using an IN clause to make
the SQL consistent, but no luck.

Thanks

Kevin


The syntax you show in the subject of this message should work:

SELECT <columns>
FROM <table>
WHERE <column> = <value>
OR <column> IS NULL

If that doesn't help you, please post actual DDL and sample data (in
the form of insert statements) to help us reproduce and then solve
your problem.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
ke**********@hotmail.com (Kevin Frey) wrote in message news:<e5**************************@posting.google. com>...
Hello,

I have a table which contains some nullable columns. I want to write a
single query, which can be prepared (ie. prepared statement), that can
handle null or non-null values for the where clause. Is this possible
in a standard-conforming manner?

The simple for of the query is this:

SELECT * FROM <table> WHERE <column> = <value>

But when the value to be matched on is NULL, the syntax of the query
must change to be:

SELECT * FROM <table> WHERE <column> IS <value>

In the second case <value> is NULL.

I know one option might be to change the ANSI NULL handling option,
but I am loathe to do this (I have five RDBMS's to support, not just
MSSSQL).

I thought I might have been able to cheat using an IN clause to make
the SQL consistent, but no luck.

Thanks

Kevin


Try the following (assuming -1 isnt a possible value for <table>.<column>)

SELECT * FROM <table> WHERE IsNull(<column>, -1) = IsNull(<value>, -1)

I think this is sql-92 compatible as well
Jul 20 '05 #3
Will one of these do the trick?

select * from <table> where ((column is null) or (column = value))

select * from <table> where isnull(column, value) = value


ke**********@hotmail.com (Kevin Frey) wrote in message news:<e5**************************@posting.google. com>...
Hello,

I have a table which contains some nullable columns. I want to write a
single query, which can be prepared (ie. prepared statement), that can
handle null or non-null values for the where clause. Is this possible
in a standard-conforming manner?

The simple for of the query is this:

SELECT * FROM <table> WHERE <column> = <value>

But when the value to be matched on is NULL, the syntax of the query
must change to be:

SELECT * FROM <table> WHERE <column> IS <value>

In the second case <value> is NULL.

I know one option might be to change the ANSI NULL handling option,
but I am loathe to do this (I have five RDBMS's to support, not just
MSSSQL).

I thought I might have been able to cheat using an IN clause to make
the SQL consistent, but no luck.

Thanks

Kevin

Jul 20 '05 #4
On 5 Apr 2004 05:32:06 -0700, Mystery Man wrote:
SELECT * FROM <table> WHERE IsNull(<column>, -1) = IsNull(<value>, -1)

I think this is sql-92 compatible as well


ISNULL is not sql-92 compatible. COALESCE is (and it does exactly the
same, plus offers some extra possibilites).

I never use ISNULL anymore.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #5
> Try the following (assuming -1 isnt a possible value for <table>.<column>)

SELECT * FROM <table> WHERE IsNull(<column>, -1) = IsNull(<value>, -1)

I think this is sql-92 compatible as well


Yes, this is the kind of solution I am looking for. Does anyone have
any idea how the query optimiser will handle this when it comes time
to choosing appropriate indexes? On a simple test it seems it will
still utilise the correct index, but I'm not particularly confident.
Jul 20 '05 #6
ke**********@hotmail.com (Kevin Frey) wrote in message news:<e5**************************@posting.google. com>...
Try the following (assuming -1 isnt a possible value for <table>.<column>)

SELECT * FROM <table> WHERE IsNull(<column>, -1) = IsNull(<value>, -1)

I think this is sql-92 compatible as well


Yes, this is the kind of solution I am looking for. Does anyone have
any idea how the query optimiser will handle this when it comes time
to choosing appropriate indexes? On a simple test it seems it will
still utilise the correct index, but I'm not particularly confident.


A showplan indicates that the query optimiser does utilise the
appropriate indexes on our test cases. We have been using this
approach for autogenerated procs on different databases.
Jul 20 '05 #7

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

Similar topics

0
by: phil | last post by:
I am having a problem with the C api with prepared statements, the data recorded in the database does not match the data I am sending. It seems to be some sort of bit shifted version of the data,...
0
by: Rujuta Gandhi | last post by:
Hi all, I want to use prepared statement in which the where clause is to be passed as a parameter. I dont know how to get the where clause from a cube as a tuple as the error coming after the...
3
by: TD | last post by:
I have an unbound form that adds and updates records to one table. I use an INSERT sql statment to add a new record and a UPDATE statement to update a record. I created a function named C2F to...
7
by: kumar.senthil | last post by:
Hi, I'm using XmlSerializer to create an object from the XML string. I would like to know whether I can get a null value for an empty XML element. Actually the XmlSerializer assigns "" (empty...
1
by: jfturcott | last post by:
Prepared statement '(@p1 int,@p2 tinyint,@p3 varchar(8000),@p4 varchar(8000),@p5 bit' expects parameter @p1, which was not supplied. I get the above error when trying to update a datagridview to...
1
by: kaushikm | last post by:
How to access sql server inbuilt functions from java prepared statement ? I have used getdate() of sql in a java prepared statement and am connecting to sqlserver. But it's not working. What to do ?
4
by: TechieGrl | last post by:
Prepared statements are new to me and having to do this with a multi- dimensional array is beyond me. Here is the prepared statement block: // Prepare to insert a record into table1...
1
by: robtyketto | last post by:
Greetings, After changing all my jsp code to use Prepared statements I have an error in the below code. The error is as follows:- It doesnt appear related at all, line position isnt...
14
by: MNNovice | last post by:
My main report contains 3 separate expense sub reports. For my sub report footers I used (one of the sub report shown here) I have this statement =IIf(IsNull(),0,Sum()) My goal is to show a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.