Execute where clause depending on the column value
Hi, so here's my problem.
I need to have a query with a where clause bu the condition is depending on the value of a certain field.
Heres the structure:
ID NAME CUSTOMERTYPE STUDENTNO
basically here;s the lofic:
SELECT * FROM TRANSACTTABLE WHERE ID <> ''AND(NAME <> ''
--CASE WHEN CUSTOMERTYPE='STUDENT' THEN EXCEUTE THIS QUERY:
AND STUDENTNO <> '')
--ELSE WHEN CUSTOMERTYPE <> 'STUDENT' THEN EXECUTE JUST THIS:
)
PLEASE HELP. THANKS
Your query is showing some strange design - ID and CUSTOMERTYPE of type 'string', are you sure that it should be done this way?
SELECT *
FROM TRANSACTTABLE
WHERE ID <> '' AND
NAME <> '' AND
(CUSTOMERTYPE <> 'STUDENT' OR STUDENTNO <> '')
In MySQL you can use the IF function like this
SELECT * FROM TRANSACTTABLE
WHERE ID <> '' AND IF(CUSTOMERTYPE='STUDENT',STUDENTNO <> '',1=1)
Related
I'm trying to do a wildcard retrieve if a defined variable is blank. But I can't figure out the correct syntax.
SELECT ...
FROM ...
WHERE customers = CASE
WHEN ISNULL(#customerID, '') = '' THEN LIKE '%'
ELSE #customerID
END
It is as simple as this:
WHERE customers = NULLIF(#customerID, '') OR NULLIF(#customerID, '') IS NULL
The expression NULLIF(x, '') will convert '' to null. Then we take advantage of how null comparison works:
If #customerID is not null then first condition will be true only for exact match
If #customerID is null then first condition will be false (nothing equals null) but second condition will be true for all rows
DB<>Fiddle
You can just simply put it like this
SELECT...
FROM...
WHERE customers = ISNULL(#customerID,'') OR (ISNULL(#customerID,'') = '')
This should work because if the first condition was met, it will select the specific customer id. Otherwise, if the customer id is null,it will select all data because of this condition below:
(ISNULL(#customerID,'') = ''
If NULL, '' = '' will be true.
I'm trying to execute the query bellow on a SQL server database
but it throws the following error:
Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS.
Code:
IF #status = 1
BEGIN
..... -- execute something
END
ELSE
BEGIN
select top #URL.nof# *
from lead_feed
where
lead_tstamp > '2017-09-01'
and egoiID is not null
and rtrim(ltrim(lead_name)) <> ''
and rtrim(ltrim(lead_surname)) <> ''
and rtrim(ltrim(lead_gender)) <> ''
and rtrim(ltrim(lead_interest)) <> ''
and lead_country in (select campaign_country
from client_campaigns
where campaignID = '#URL.ccampaignID#')
and leadID not in (select leadID
from lead_integration
where campaignID = '#URL.ccampaignID#')
order by
lead_tstamp desc
END
It points to the leadID not in ( .... part, if I delete this part it points the same error to and lead_country in ( ... part.
Can you please explain me whats happening?
Thanks.
I've seen plenty of SO questions where someone is testing for a NULL value within a case statement, but nothing of the nature of this question. I want the return of the case statement to be a 'is not null' expression for a where clause.
This is what I have:
INSERT INTO table1 (Id) (
SELECT DISTINCT AId AS Id
FROM table2 As t2
INNER JOIN table3
ON (
...
)
WHERE CASE
WHEN test_value <> '' THEN (another_value IS NOT NULL)
END
)
This gives the error:
Incorrect syntax near the keyword 'is'.
In short, if the test value is not empty-string, then I do not want rows with another_value=NULL in my table1.
My program needs to pass 2 cases
Case 1:
Row# test_value another_value ....
1 'tst' '919'
2 'tst' NULL
ONLY ROW ONE SHOULD BE INSERTED
Case 2:
Row# test_value another_value ....
1 '' '919'
2 '' NULL
BOTH ROWS SHOULD BE INSERTED
Try this:
WHERE ((isnull(test_value,'') <> '' and ISNULL(another_value,'') <> '') OR ISNULL(test_value,'') = '')
You can do something like that:
WHERE CASE
WHEN test_value <> '' AND another_value IS NOT NULL THEN another_value
END
I am trying to write a SQL Select statement to return records based on a user input through a front end.
I want to write the Select statement like this:
SELECT somefields
FROM sometable
WHERE CASE variable
WHEN 'blank' THEN field IS NULL
ELSE field = field
END
Basically I either want to filter a column to find NULL values or ignore the filter and return all values depending on the value of the variable. I know that the results of the CASE statement is not executable but how can I do this?
When variable is 'blank', the following query will give you rows where field is NULL. When variable is anything else, it will give you all rows:
SELECT somefields
FROM sometable
WHERE
(variable = 'blank' AND field IS NULL)
OR (variable <> 'blank')
You can use NULLIF() (link is for SQL Server, but NULLIF() should be standard):
SELECT somefields
FROM sometable
WHERE field = NULLIF(variable, 'blank')
The following snippet should behave as follows:
when #variable is null, return all rows
when #variable = 'blank', return all rows where field is null or field = 'blank'
otherwise, return rows where #variable equals field
Code snippet:
WHERE 1 = CASE
WHEN #variable is null then 1
WHEN #variable = 'blank' and field is null then 1
WHEN #variable = field then 1
END
SELECT somefields
FROM sometable
WHERE ((variable IS NULL OR variable = 0) OR (variable = field))
WHERE Criteria is apply when variable have value
For Example:
DECLARE #CityName VARCHAR(50)
SET #CityName = NULL
SELECT CityName
FROM City
WHERE ((#CityName IS NULL ) OR (#CityName = CityName ))
When City is null then tables return all rows
I think I get what you're after. Something like this maybe?
SELECT field1,
field2,
CASE variable
WHEN 'blank' THEN NULL
ELSE field3
END as field3
FROM sometable
Think I understand what you mean....for example....
SELECT
House, Postcode
from
SomeTable
where
(House=isnull(#House,House) or (House is null and #House is null))
and
(Postcode=isnull(#Postcode,Postcode) or (Postcode is null and #Postcode is null))
First bit of the conditional where is to use the variable, when present (the isnull bit is to ignore the variable if it's null)
Second bit of the conditional where is in case your evaluative field is null also as effectively fields don't = null they are 'is null'.
Confused? Good. Works on what I'm doing though!
Here is my solution based on #Andomar answer above aimed at anyone testing an input varchar value, you need to test the parameter in the right order as per the example below:
FIELD1 = CASE
WHEN #inputParameter = '' THEN FIELD1
WHEN #inputParameter <> FIELD1 THEN NULL -- if input is some text but does not match
WHEN #inputParameter IS NULL THEN FIELD1
WHEN #inputParameter != '' AND FIELD1 = #inputParameter THEN FIELD1
END
Hope this helps someone.
I have a table containing Remarks as a column. Now i have to display the data inside remarks as 0 if the row is blank i.e empty but not Null. Please give me a query that will solve my problem in MS SQL server 2005.
Use SELECT ... CASE
SELECT remarksDisplay = CASE remarks WHEN '' THEN '0' ELSE remarks END
FROM tableName;
use a case statement in your SQL i.e
select (case when Remarks = '' then '0' else Remarks end) as Remarks from RemarksTable
You can further extend this to handle null values too if you want i.e
select (case when isnull(Remarks, '') = '' then '0' else Remarks end) as Remarks from RemarksTable
SELECT
case column1
when '' then 'unknown' /*empty*/
when ' ' then 'unknown' /*empty with space*/
end
FROM table