473,403 Members | 2,366 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,403 software developers and data experts.

SQL NULL or Value

Hi
my prob is like this..

DECLARE @id int

SELECT *
FROM Table1
WHERE ID = @ID

if @ID is null , I want to select all the records, that ID is null
Thanks
Dish

Jul 23 '05 #1
6 7284
Dishan Fernando (di****@gmail.com) writes:
DECLARE @id int

SELECT *
FROM Table1
WHERE ID = @ID

if @ID is null , I want to select all the records, that ID is null


IF @ID IS NULL
SELECT * FROM Table1
ELSE
SELECT * FROM Table1 WHERE ID = @ID

You can collapse this into

SELECT * FROM Table1 WHERE ID = @ID OR @ID is NULL

However, if there is an index on ID, you probably want it to be used
if @ID has a value. The latter statement will result in the table
being scanned.

In case you have several columns you want to do this on, I have an
article on my web site that could be of interest:
http://www.sommarskog.se/dyn-search.html.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
No , I want to do in in single sql,

if I use this:
SELECT * FROM Table1 WHERE ID = @ID OR @ID is NULL
and passing some value to @ID

result also may have null ids.
what I want is if I pass value to @ID or Null . It will selecting
acording to that

Jul 23 '05 #3
On 7 Jan 2005 00:35:17 -0800, Dishan Fernando wrote:
No , I want to do in in single sql,

if I use this:
SELECT * FROM Table1 WHERE ID = @ID OR @ID is NULL
and passing some value to @ID

result also may have null ids.
what I want is if I pass value to @ID or Null . It will selecting
acording to that


Hi Dishan,

If I understand you correctly, you want to return only the rows that have
ID equal to NULL if you pass @ID as NULL. Correct?

Try
SELECT Column1, Column2, ..., ColumnN
FROM Table1
WHERE ID = @ID
OR ( @ID IS NULL AND ID IS NULL )

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4
As far as I know, if you have an ID which is null your database
structure is not very well thought out. The only way you could get a
null id is if you are doing an outter join on two tables, if the one
being joined does not have any values it would still return the values
from the first. You should not have any columns returning values on a
row without an id...
For instance:
SELECT Products.*, [Order Details].OrderID
FROM Products LEFT OUTER JOIN
[Order Details] ON Products.ProductID = [Order
Details].ProductID
WHERE ([Order Details].ProductID IS NULL)
Just my two cents.

Jul 23 '05 #5
On 7 Jan 2005 05:21:28 -0800, ujjc001 wrote:
As far as I know, if you have an ID which is null your database
structure is not very well thought out.


Hi ujjc001,

Yes, you are correct. I wanted to add that, but then forgot it when I
clicked Send. Thanks for the addition!!

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #6
Thanks .. thats what I want... ;)

Jul 23 '05 #7

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

Similar topics

3
by: John Morgan | last post by:
In an SQL statement which concatenates several fields I get a null value returned if any one of the fields are null. Is this to be expected? For example : SELECT tblMember.memberAddress + '...
3
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in...
8
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
13
by: Federico Balbi | last post by:
Hi, I was wondering if PGSQL has a function similar to binary_checksum() of MS SQL Server 2000. It is pretty handy when it comes to compare rows of data instead of having to write long boolean...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.