Simple
Trick Using ISNULL:
Step-1:
Create Sample Student Table.
CREATE
TABLE
Student
(
Id
INT
IDENTITY(1,1)
PRIMARY
KEY,
StudentName
VARCHAR(100)
NOT
NULL,
LivingLocation
VARCHAR(100)
NULL
)
Step-2
:Insert Some Sample Data
INSERT
INTO
Student
SELECT
'Rakesh','Hyderabad'
UNION
ALL
SELECT
'Raju','Delhi'
UNION
ALL
SELECT
'Madhu','Hyderabad'
UNION
ALL
SELECT
'Naresh',NULL
UNION
ALL
SELECT
'Venaktesh','Chennai'
Step-3
:Select
Data from Student Table
SELECT
*
FROM
Student
If
we observe above data of student table we have student id 4 record
LivingLocation is NULL.
Step
-4 :Select Data from Student Table With Where Clause.
SELECT
*
FROM
Student WHERE
LivingLocation='Hyderabad'
In
the above query we are included the where clause with LivingLocation
is 'Hyderabad'
SELECT
*
FROM
Student WHERE
LivingLocation<>'Hyderabad'
In
the above query we want data from Student Table whose LivingLocation
is not equal to the 'Hyderabad' , but it is not selected the data
whose id is '4' ,because of the id of '4' LivingLocation is NULL ,
but we need that record also in this situation we can we ISNULL
function.
SELECT
*
FROM
Student WHERE
ISNULL(LivingLocation,'')<>'Hyderabad'
Good one !!
ReplyDeleteINSERT INTO Student
ReplyDeleteVALUES('SHABARINADH','VIJAYAWADA')
,VALUES('Raju','Delhi')
,VALUES('Madhu','Hyderabad')
,VALUES('Naresh',NULL)
,VALUES('Venaktesh','Chennai')
What about this?
INSERT INTO Student (StudentName,LivingLocation)
ReplyDeleteVALUES('SHABARINADH','VIJAYAWADA'),
('Raju','Delhi'),
('Madhu','Hyderabad'),
('Naresh',NULL),
('Venaktesh','Chennai')