Issues while using <> (not equal to) in sql Query [duplicate] - sql-server

This question already has answers here:
Why does NULL = NULL evaluate to false in SQL server
(21 answers)
Closed 4 years ago.
I am trying get a result for the following query.
Select * from FACHL where Comp_Year = '2018-2019' and
Trans_date between '04/01/2018' And '03/31/2019' And Order_By <> 'SMAN'
Order By Account_Code,Trans_Date,Document_No,S_no
This gives me no result.
If I remove Order_By <> 'SMAN' from Query , it shows me result.
The data in Order_By column is NULL for all rows.
So I tried Order_By = NULL but no result.
The image shows my data. RESULT IMAGE
Where am I committing mistake here???
Thanx..

SQL NULL means I don't know.
If you want to get NULL row need to use Order_By IS NULL rather than Order_By = NULL
Select *
from FACHL
where Comp_Year = '2018-2019' AND
Trans_date between '04/01/2018' And '03/31/2019' And Order_By IS NULL
Order By Account_Code,Trans_Date,Document_No,S_no
NOTE
NULL can't use <> or = to get the value when the ANSI_NULLS set ON

Related

What does it means that #param IS NULL OR value= #param in sql?

I am writing a stored procedure in SQL where i have a scenario that fetch all record if parameter is null or fetch matching record if parameter is not null. In this case, i always use ISNULL function like that:
table.value = ISNULL(#param,table.value)
But in this case if value is not null, it works fine, but if value is null then it fetch all record except those where table.value is null. So i searched and found a solution here answered by sII. but i don't understand the statement
#param IS NULL OR value= #param
It works fine for me but i am unable to understand? How it works? Thanks in advance for answer.
Below is my understanding about ALL IF NULL Statement.
Case 1: If the parameter #param IS NULL.
In this case the All if NULL statement becomes like this,
NULL IS NULL OR value= #param.
Here the left part of the OR statement becomes True, So records will fetch according to that part. so the query becomes,
SELECT *FROM TABLE WHERE NULL IS NULL which is same as
SELECT *FROM TABLE. So it will fetch all the records.
Case 2: If the parameter #param have a value (say value = 1)
In this case the All if NULL statement becomes like this,
1 IS NULL OR value= 1.
Here the left part of the OR statement becomes False, So records will fetch according to the right part.
So the query becomes,
SELECT *FROM TABLE WHERE value= 1.
Hope you understand Now..

Update rows from one table to another table based on Id [duplicate]

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 4 years ago.
Both tables have a client_id column. Need to insert data from #LocalDashboardtable into T004_Dashboard when the client_id column are equal. i have tried this but it does't help shows an error "Incorrect syntax near ','. "
update T004_Dashboard set T004_Dashboard.[GrossCharge],T004_Dashboard.[NetCharge]
= (select #LocalDashboardtable.[GrossCharge] , #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable where
#LocalDashboardtable.client_id =T004_Dashboard.client_id and
#LocalDashboardtable.[month] =T004_Dashboard.[month]
and #LocalDashboardtable.[year] =T004_Dashboard.[year] )
pls help me
This is your query (which looks a lot like SQL Server):
update T004_Dashboard
set T004_Dashboard.[GrossCharge],
T004_Dashboard.[NetCharge] = (select #LocalDashboardtable.[GrossCharge], #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable
where #LocalDashboardtable.client_id = T004_Dashboard.client_id and
#LocalDashboardtable.[month] = T004_Dashboard.[month] and
#LocalDashboardtable.[year] = T004_Dashboard.[year]
);
You cannot set a pair of columns to a pair of columns in a subquery. Instead, use a join:
update T004_Dashboard
set GrossCharge = ld.GrossCharge,
NetCharge = ld.NetCharge
from T004_Dashboard d join
#LocalDashboardtable ld
on ld.[month] = d.[month] and ld.[year] = d.[year] and
ld.client_id = d.client_id;
Also, SQL Server does not allow qualified column names in update/set statements

Handling NULL value in ORACLE Table Query

I have a question regarding handling NULL value in a column in ORACLE Table.
So, when i query a table, i get this error message in every NULL value occurences
Notice: Undefined index: STATUS in C:\xampp\htdocs\WeltesInformationCenter\AdminLTE\pages\tables\assignmenttable.php on line 481
my query is like this
SELECT MASTER_DRAWING.*, (SELECT PREPACKING_LIST.PACKING_STATUS FROM PREPACKING_LIST WHERE MASTER_DRAWING.HEAD_MARK = PREPACKING_LIST.HEAD_MARK) STATUS FROM MASTER_DRAWING WHERE PROJECT_NAME = :PROJNAME
My question is, how to handle NULL value so that when it sees a null value, it can return some value such as 0 or any string.
Thanks
Try
SELECT MASTER_DRAWING.*,
NVL((SELECT PREPACKING_LIST.PACKING_STATUS
FROM PREPACKING_LIST
WHERE MASTER_DRAWING.HEAD_MARK = PREPACKING_LIST.HEAD_MARK),'N/A'
) STATUS
FROM MASTER_DRAWING WHERE PROJECT_NAME = :PROJNAME

Query with integers not working

I've been searching here on stackoverflow and other sources but not found a solution to this
The query below works as expected expect for when either custinfo.cust_cntct_id or custinfo.cust_corrcntct_id = '' (blank not NULL) then I get no results. Both are integer fields and if both have an integer value then I get results. I still want a value returned for either cntct_email or corrcntct_email even if custinfo.cust_cntct_id or custinfo.cust_corrcntct_id = blank
Can someone help me out in making this work? The database is PostgreSQL.
SELECT
cntct.cntct_email AS cntct_email,
corrcntct.cntct_email AS corrcntct_email
FROM
public.custinfo,
public.invchead,
public.cntct,
public.cntct corrcntct
WHERE
invchead.invchead_cust_id = custinfo.cust_id AND
cntct.cntct_id = custinfo.cust_cntct_id AND
corrcntct.cntct_id = custinfo.cust_corrcntct_id;
PostgreSQL won't actually let you test an integer field for a blank value (unless you're using a truly ancient version - 8.2 or older), so you must be using a query generator that's "helpfully" transforming '' to NULL or a tool that's ignoring errors.
Observe this, on Pg 9.2:
regress=> CREATE TABLE test ( a integer );
CREATE TABLE
regress=> insert into test (a) values (1),(2),(3);
INSERT 0 3
regress=> SELECT a FROM test WHERE a = '';
ERROR: invalid input syntax for integer: ""
LINE 1: SELECT a FROM test WHERE a = '';
If you are attempting to test for = NULL, this is not correct. You must use IS NOT NULL or IS DISTINCT FROM NULL instead. Testing for = NULL always results in NULL, which is treated as false in a WHERE clause.
Example:
regress=> insert into test (a) values (null);
INSERT 0 1
regress=> SELECT a FROM test WHERE a = NULL;
a
---
(0 rows)
regress=> SELECT a FROM test WHERE a IS NULL;
a
---
(1 row)
regress=> SELECT NULL = NULL as wrong, NULL IS NULL AS right;
wrong | right
-------+-------
| t
(1 row)
By the way, you should really be using ANSI JOIN syntax. It's more readable and it's much easier to forget to put a condition in and get a cartesian product by accident. I'd rewrite your query for identical functionality and performance but better readability as:
SELECT
cntct.cntct_email AS cntct_email,
corrcntct.cntct_email AS corrcntct_email
FROM
public.custinfo ci
INNER JOIN public.invchead
ON (invchead.invchead_cust_id = ci.cust_id)
INNER JOIN public.cntct
ON (cntct.cntct_id = ci.cust_cntct_id)
INNER JOIN public.cntct corrcntct
ON (corrcntct.cntct_id = ci.cust_corrcntct_id);
Use of table aliases usually keeps it cleaner; here I've aliased the longer name custinfo to ci for brevity.

SQL Server 2008 Stored Proc Performance where Column = NULL

When I execute a certain stored procedure (which selects from a non-indexed view) with a non-null parameter, it's lightning fast at about 10ms. When I execute it with a NULL parameter (resulting in a FKColumn = NULL query) it's much slower at about 1200ms.
I've executed it with the actual execution plan and it appears the most costly portion of the query is a clustered index scan with the predicate IS NULL on the fk column in question - 59%! The index covering this column is (AFAIK) good.
So what can I do to improve the performance here? Change the fk column to NOT NULL and fill the nulls with a default value?
SELECT top 20 dbo.vwStreamItems.ItemId
,dbo.vwStreamItems.ItemType
,dbo.vwStreamItems.AuthorId
,dbo.vwStreamItems.AuthorPreviewImageURL
,dbo.vwStreamItems.AuthorThumbImageURL
,dbo.vwStreamItems.AuthorName
,dbo.vwStreamItems.AuthorLocation
,dbo.vwStreamItems.ItemText
,dbo.vwStreamItems.ItemLat
,dbo.vwStreamItems.ItemLng
,dbo.vwStreamItems.CommentCount
,dbo.vwStreamItems.PhotoCount
,dbo.vwStreamItems.VideoCount
,dbo.vwStreamItems.CreateDate
,dbo.vwStreamItems.Language
,dbo.vwStreamItems.ProfileIsFriendsOnly
,dbo.vwStreamItems.IsActive
,dbo.vwStreamItems.LocationIsFriendsOnly
,dbo.vwStreamItems.IsFriendsOnly
,dbo.vwStreamItems.IsDeleted
,dbo.vwStreamItems.StreamId
,dbo.vwStreamItems.StreamName
,dbo.vwStreamItems.StreamOwnerId
,dbo.vwStreamItems.StreamIsDeleted
,dbo.vwStreamItems.RecipientId
,dbo.vwStreamItems.RecipientName
,dbo.vwStreamItems.StreamIsPrivate
,dbo.GetUserIsFriend(#RequestingUserId, vwStreamItems.AuthorId) as IsFriend
,dbo.GetObjectIsBookmarked(#RequestingUserId, vwStreamItems.ItemId) as IsBookmarked
from dbo.vwStreamItems WITH (NOLOCK)
where 1 = 1
and vwStreamItems.IsActive = 1
and vwStreamItems.IsDeleted = 0
and vwStreamItems.StreamIsDeleted = 0
and (
StreamId is NULL
or
ItemType = 'Stream'
)
order by CreateDate desc
When it's not null, do you have
and vwStreamItems.StreamIsDeleted = 0
and (
StreamId = 'xxx'
or
ItemType = 'Stream'
)
or
and vwStreamItems.StreamIsDeleted = 0
and (
StreamId = 'xxx'
)
You have an OR clause there which is most likely the problem, not the IS NULL as such.
The plans will show why: the OR forces a SCAN but it's manageable with StreamId = 'xxx'. When you use IS NULL, you lose selectivity.
I'd suggest changing your index make StreamId the right-most column.
However, a view is simply a macro that expands so the underlying query on the base tables could be complex and not easy to optimise...
The biggest performance gain would be for you to try to loose GetUserIsFriend and GetObjectIsBookmarked functions and use JOIN to make the same functionality. Using functions or stored procedures inside a query is basically the same as using FOR loop - the items are called 1 by 1 to determine the value of a function. If you'd use joining tables instead, all of the items values would be determined together as a group in 1 pass.

Resources