Simulating an if condition in the WHERE clause of an SQL query - sql

I am trying to write a google BigQuery to select some records based on this condition:
if ID_parameter is not null:
SELECT * WHERE ID=ID_parameter
else:
SELECT * WHERE country_id=country_id_parameter and (name LIKE %name_parameter% OR name is null)
The name_parameter should be optional but one of the ID_parameter or country_id_parameter must be provided.
Sorry, I am a newbie with SQL but I hope I stated what I want clearly.

consider below approach
select *
from your_table
where case
when not id_parameter is null then id = id_parameter
else country_id = country_id_parameter and (regexp_contains(name, name_parameter) or name is null)
end

I miss the table name in the query, let's assume it's 'personnel'.
Suppose ID-parameter = 200, country_id_parameter = 'NL', name_parameter = '%bart%'
Maybe like this when the name is 'bart':
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL' and
name LIKE '%bart%')
Use this when the name is NULL:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL')

Related

Use CASE statement to test values in table 1, set values in table 2

I am new to SQL, so if you could include in answer correct syntax (for PostgreSQL) that will be great.
I have two tables
table 1 "geo_temp", with columns [geo_type1] [geo_type2]....[geo_type6] [geo_typeR];
table 2 "geo_summary", with column [geo].
Here what I want to do,
CASE
WHEN (geo_type1 NOTNULL) THEN (geo = geo_type1)
WHEN (geo_type2 NOTNULL) THEN (geo = geo_type2)
...
ELSE (geo = geo_typeR)
Your help is much appreciated. Thanks.
Use the coalesce() function:
geo = coalesce(geo1, geo2, ...,geoR)
coalesce() returns the first non-null value found in the list, which is what your intention is.
As an update statement:
update geo_summary set
geo = (select coalesce(geo1, geo2, ...,geoR)
from geo_temp
where geo_temp.id = geo_summary.id)
I am assuming you want to use this in a WHERE or ON Clause.
You could do something like...
geo =
CASE
WHEN (geo_type1 is NOT NULL) THEN geo_type1
WHEN (geo_type2 is NOT NULL) THEN geo_type2
...
ELSE geo_typeR
END

Select only specific results in sql (postgresql)

SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery
FROM table
I have this. The result of this query has 2 fields: id, ?column?
The ?column? field value is true or false.
how can I get only the true results?
SELECT * FROM (
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery
FROM table
) AS search
WHERE column = 't'
You can also use WITH: http://www.postgresql.org/docs/9.1/static/queries-with.html
use WHERE clause.... (if i understand your need correctly)
WHERE condition = true
Add a WHERE clausure to fill this values. Something like this.
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery FROM table WHERE ?column? = 1 (or True)
use alias to reference your column
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery AS condition
FROM table
WHERE condition = true

sql select with one value of two where

This is the only place that I get all answer ;)
I want to select :
SELECT
RTRIM(LTRIM(il.Num_bloc)) AS Bloc,
RTRIM(LTRIM(il.num_colis)) AS Colis,
cd.transporteur AS Coursier,
cd.origine AS Origine,
cd.destination AS Destinataire,
cd.adresse AS [Adresse Destinataire],
cd.poids AS Poids,
il.Signataire, il.num_cin AS CIN, il.date_livraison AS [Date Livraison]
FROM
dbo.cd
INNER JOIN
dbo.il ON cd.bloc = il.Num_bloc AND dbo.cd.colis = dbo.il.num_colis
WHERE
(il.Num_bloc = RTRIM(LTRIM(#ParamBloc)))
AND (il.num_colis = RTRIM(LTRIM(#ParamColis)))
In the way of getting result if the user put ether #ParamBloc or #ParamColis
Try using IsNull() function.
A simple query would go like this
Select * from yourTable
Where
Num_bloc = ISNULL(#ParamBloc, Num_block) AND
num_colis = ISNULL(#ParamColis, num_colis)
The second parameter would make the expression to true if the #parameter Bloc or Colis is null. This query would be useful for all 4 possible combination of these two parameter.

TSQL Case in where statement if parameter is null

I have a SP that gives me a lot of hard times.
The sp gets a two parameters #madeByUserId and #reportedByUserId.
I want to have something like:
select * from table
where MadeByUserId = #madeByUserId (if(#reportedByUserID != null) and ReportedByUserID = #reportedByUserID)
Basically I want to make a case in the where clause to include another filter condition based of the null/not null state of the #reportedByUserId
Is that possible?
Thanks a lot,
Radu
Try:
select * from table
where MadeByUserId = #madeByUserId
AND (#reportedByUserID IS null OR ReportedByUserID = #reportedByUserID)
You could use COALESCE.
SELECT *
FROM Table
WHERE MadeByUserId = #madeByUserId
AND ReportedByUserID = COALESCE(#reportedByUserID, ReportedByUserID)
This translates to
if #reportedByUserID is `NOT NULL` then
compare ReportedByUserID with #reportedByUserID
else
compare ReportedByUserID with ReportedByUserID
From MSDN
COALESCE
Returns the first nonnull expression
among its arguments.
Add an if statement
IF (#reportedByUserId IS NOT NULL)
SELECT *
FROM table t
WHERE t.MadeByUserId = madeByUserId etc
I think this will give you what you're after.
IF (#reportedByUser IS NULL)
select * from table
where MadeByUserId = #madeByUserId
ELSE
select * from table
where MadeByUserId = #madeByUserId and ReportedByUserID = #reportedByUserID)

basic oracle question

I have this query:
select total.lecgrouplecture(l.groupcode) lecturename,
total.lecgrouptime(l.groupcode) lecttime
from total.lecgroup l
where term = (select term
from total.CURENTTERM)
and rownum < 10
order by lecturename
I want to know what total.lecgrouptime(l.groupcode) is, and get this information from where?
total is the package name
lecgrouplecture is a function within that package
Look in user_source for the code or use a GUI like SQL Developer or TOAD
it looks like TOTAL is the name of a schema (SELECT * FROM all_users WHERE username = 'TOTAL'). If this is the case then lecgrouplecture must be a pl/sql function. You will find what it does with Robert's query:
SELECT *
FROM all_source
WHERE owner = 'TOTAL'
AND name = 'LECGROUPLECTURE'
ORDER BY line;