How to set If condition in Where clause in Sql - sql-server-2008

I want to display a criteria having multiple conditions, but if any condition is null or not having any value than how to set in where clause.
Example:
where
SR.fk_Designation_ID = #pk_Designation_ID
and SR.fk_SeniorityCategory_ID = #pk_SeniorityCategory_ID
and SR.IsCurrent_Appointment = 1 and SR.fk_SeparationType_ID is null
order by Senority_Joining_For_Order,E.Date_Of_Birth
here if #pk_SeniorityCategory_ID is null than I don't want to include the and SR.fk_SeniorityCategory_ID = #pk_SeniorityCategory_ID condition in where clause.

Try this:
WHERE
SR.fk_Designation_ID = #pk_Designation_ID
AND (#pk_SeniorityCategory_ID IS NULL
OR
SR.fk_SeniorityCategory_ID = #pk_SeniorityCategory_ID)
AND SR.IsCurrent_Appointment = 1 AND SR.fk_SeparationType_ID IS NULL
ORDER BY Senority_Joining_For_Order,E.Date_Of_Birth

Related

MySQL - Update Field to Another Field's Value

I am sturggling to get the following query working:
UPDATE user_stock SET user_stock1 = user_stock2
WHERE user_stock1 = NULL AND user_id = 'mike';
The query is accepted with no syntax errors, although it does not set user_stock1 to the value of user_stock2, any ideas?
In SQL, a column is never equal to NULL. You have to use IS NULL or IS NOT NULL:
UPDATE user_stock SET user_stock1 = user_stock2
WHERE user_stock1 IS NULL AND user_id = 'mike';

Select null or not null depend on parameter

I have a query like this:
SELECT old.NPP,
old.NAMA,
old.JOB,
pgh.status
FROM a old
LEFT JOIN b pgh ON old.person_id = pgh.person_id
AND pgh.periode = 2015
AND pgh.LAST_STATUS = 1
WHERE old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI'
AND pgh.status = #status
ORDER BY pgh.status
i want my query can get every value from pgh status, like when i type status 5, the query must to return status is 5, but when i input null, the query will return data from database when the status is null.
how query will be set?
You can write the condition this way:
where old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI' and
(pgh.status = #status or pgh.status is null and #status is null)
If i understand your situation correctly, i think there is empty strings, so to make sure the condition is correct. just use ISNULL
SELECT old.NPP,
old.NAMA,
old.JOB,
pgh.status
FROM a old
LEFT JOIN b pgh ON old.person_id = pgh.person_id
AND pgh.periode = 2015
AND pgh.LAST_STATUS = 1
WHERE old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI'
AND pgh.status = ISNULL(#status,'')
ORDER BY pgh.status

WHERE Clause only IF NOT NULL

I need help with my SELECT.
I got a field that can be NULL and in it there is stored a foreign-key.
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerBrandID = beerID <--- this can be NULL
So if it's NULL nothing happens.
How can I check if beerID is NOT NULL so I can use "beerBrandID = beerID"?
You probably need something like this:
First example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND (beerID IS NULL OR beerBrandID = beerID)
Second Example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerID IS NOT NULL AND beerBrandID = beerID
The first example will allow you to show records which have beerID null along with the beers which beerBrandID is equal to beerID (both).
The second one will return exactly beers which correspond to beerBrandID (excluding NULL beerIDs).
How about using with CASE statement in where clause like
WHERE
CASE WHEN beer.id IS NOT NULL
THEN beer.brand_id = 12345
ELSE TRUE
END
If you want to include records where there's no match, you need an outer join
SELECT beer.id AS beerID,
beer.barrelID AS barrelID,
beer.imageID AS imageID,
beer.title AS title,
beer.map AS map,
beer.longitude AS longitude,
beer.latitude AS latitude,
brand.name AS brandName,
brew.type AS brewType,
image.name AS imageName,
variation.name AS variationName
FROM brand, brew, image, beer
LEFT OUTER JOIN variation ON variation.ID = beer.VariationID
WHERE beer.id = %s
AND md5(beer.memberID) = %s
AND beer.review = 1
AND brand.ID = beer.brandID
AND brew.ID = beer.brewID
AND image.ID = beer.imageID
To check for null / not null, use IS NULL / IS NOT NULL
AND beerID IS NOT NULL
You can use "IS NULL" or "IS NOT NULL" MySQL comparison functions.
Read more about this here:
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_is-null

Why doesn't this Rails SQL query work: companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)

I have the following full query, and it returns nothing.
#companies.where("(companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)) OR companies.status_override = 'active'")
I expect that the first part (companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)) would return all items that have status set to 'active' and status_override set to nothing.
Without knowing the DB structure, the = NULL is a suspect. Try IS NULL instead.
Arithmetic comparison with NULL doesn't behave as you might expect; more details can be found in the Mysql Reference
The most likely explanation is that there aren't any rows that satisfy the predicates in the WHERE clause. Your where clause is of the form:
( a AND ( b OR c ) ) OR d
where
a = companies.status = 'active'
b = companies.status_override = ''
c = companies.status_override = NULL
d = companies.status_override = 'active'
So, basically, we know that there aren't any rows in the table that satisfy condition d.
And we know there aren't any rows that satisfy both conditions a and b.
Condition c is impossible; it will never be satisfied.
To test whether an expression contains a NULL, use expr IS NULL. i.e.
companies.status_override IS NULL
If you aren't concerned with an index range scan operation on the status_override column (it's likely that you aren't) you can simplify a bit using the IFNULL() function...
(companies.status = 'active' AND IFNULL(companies.status_override,'') = '') OR companies.status_override = 'active'
or using the NULLIF() function
(companies.status = 'active' AND NULLIF(companies.status_override,'') IS NULL) OR companies.status_override = 'active'

MySQL Left Join, SELECT a field either from one or the other if null

I am trying to LEFT JOIN 2 tables. which is working out fine. But i am getting back two sets of fields named setting_value. iam trying to get tblSettings.setting_value only if tblAgencySettings.setting_value is NULL. How would i go about doing this? I know i can rename the fields, then in PHP i can check the tblAgencySettings.setting_value and if NULL then grab the tblSettings.setting_value but i prefer to keep this at MySQL.
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
`tblSettings`.`setting_value`, `tblAgencySettings`.`setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
slight issue i just noticed. i failed to mention this. if tblAgencySettings.setting_value does have a value. but changeable is not 1 then just select tblSettings.setting_value
Just add a COALESCE:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
The COALESCE function returns the first non-NULL value you give it so this:
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
Will be tblAgencySettings.setting_value if that's not NULL and tblSettings.setting_value if tblAgencySettings.setting_value is NULL.
If tblAgencySettings.setting_value can also be zero and you want to ignore that as well as NULL, then you could use this instead of the COALESCE above:
COALESCE(
IF(`tblAgencySettings`.`setting_value` = 0, NULL, `tblAgencySettings`.`setting_value`),
`tblSettings`.`setting_value`
)
The IF returns the second argument if the first is true and the third if the first argument is false so the above use converts zero to NULL. Or, you could go all the way to a CASE statement:
case
when `tblAgencySettings`.`setting_value` = 0 then `tblSettings`.`setting_value`
when `tblAgencySettings`.`setting_value` IS NULL then `tblSettings`.`setting_value`
else `tblSettings`.`setting_value`
end
Change your SQL Statement to this:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
CASE WHEN `tblSettings`.`setting_value` IS NULL THEN `tblAgencySettings`.`setting_value`
ELSE `tblSettings`.`setting_value` END AS `setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
Here's a link to MYSQL CASE Statement for your reference.