What is the best way to handle optional parameter if null value is passed in select query in oracle
E.g. select * from emp where deptno = 10 and empno in (7782,7934)
In above query, if user will not select any empno then it should fetch all records where deptno = 10
Usually I handle this case in the programming environment that is issuing the query. Within the code, I check for the null case, and then exclude that part of the where clause as needed. Is that something you can do?
edit
If you were using ColdFusion:
<cfquery>
select
*
from
emp
where
deptno = 10
<cfif Len(empno)>
and empno in (<cfqueryparam value="#empno#" list="true" cfsqltype="cf_sql_integer">)
</cfif>
</cfquery>
(query params added for correctness)
Here is a little bit dirty solution for your problem if you just want to instert the value into the string:
SELECT *
FROM emp
WHERE
deptno = 10
AND (empno IN (%user_input%,%not_used_value%)
OR COALESCE(%user_input%,NULL) IS NULL);
Here %user_input% is a placeholder for values, selected by user and %not_used_value% is empno id, which is never used.
So, the resulting query could look like this:
SELECT *
FROM emp
WHERE
deptno = 10
AND (empno IN (7782,7934,-999)
OR COALESCE(7782,7934,NULL) IS NULL);
Related
When run below query results is empty. Is it possible use some eg. NVL for return some value eg. -1
Select ename from emp where empno = 1
If you are expecting one row, you can use aggregation:
Select coalesce(max(ename), '-1')
from emp
where empno = 1;
This always returns one row, even when there is no match.
I have a table named emp like this:
And I want to get the enames which start with 'A' or 'B'. So I write the query like this:
select ename from emp where ename like '[AB]%'
But it return nothing instead of 'ALLEN' or 'BLAKE'. So I changed the query into:
select ename from emp where ename REGEXP '[AB]%'
But it still didn't work and said there is no such function name REGEXP. Is there anything wrong ?
The following link may help you when using REGEXP. Sqlite3 supports REGEXP only
How do I use regex in a SQLite query?
However, I think the solution can be like this and no regex is need to be used:
select ename from emp where ename like 'A%' or ename like 'B%'
You have to add it as a user-defined function.
It would be simpler to just rewrite your query without regex. I.e.
select ename from emp where ename like 'A%' or ename like 'B%'
I have a table in ORACLE DB where there are employee names column (ENAME) with 5 letters. Query used was:
SELECT ENAME FROM EMP WHERE LENGTH(ENAME) = 5;
This is not returning names with 5 letters. Can someone please suggest me the correct one?
I think you need 1st trim then use length
SELECT ENAME FROM EMP WHERE
LENGTH (Trim(ENAME))=5
I guess you're looking to count the chars excluding the white spaces
SELECT ENAME
FROM YourTableName
WHERE LENGTH(REPLACE(ENAME, ' ', NULL)) = 5;
select ENAME from EMP where Ename LIKE '____%'
Use REGEXP_COUNT to count only letters
SELECT ENAME FROM EMP where regexp_count(ENAME, '[A-Z]') = 5 ;
REGEXP_COUNT complements the functionality of the REGEXP_INSTR function by returning the number of times a pattern occurs in a source string. The function evaluates strings using characters as defined by the input character set. It returns an integer indicating the number of occurrences of pattern. If no match is found, then the function returns 0.
SELECT ENAME FROM EMP WHERE ENAME LIKE ('_____');
I am looking to format an ouput which is created by a sub-query, this sub-query produces a calculated field which i would like to format as $XX.XX.
Sub query:
(select avg(retail) from cars
where brand = 'FORD' or brand = 'TOYOTA') as AVG_BRAND_PRICE_01
I basically just want to add a $ sign and round the output to two decimal places.
Any help or direction would be much appreciated.
I am using isql plus oracle 11g
You could try this:
'$' || Cast((select avg(retail) from cars
where brand = 'FORD' or brand = 'TOYOTA') as decimal(4,2)) as AVG_BRAND_PRICE_01
If you want more than $XX.XX e.g $XXXXXXX.XX then you will need to set the decimal higher e.g. decimal(9,2)
Example SQL Fiddle: http://www.sqlfiddle.com/#!4/9f684/2/0
I basically just want to add a $ sign and round the output to two decimal places.
I am using isql plus oracle 11g
It is a simple display setting. You need to set the NUMFORMAT properly.
If all you need is to control the display format on SQL*Plus, then it is better to use the SQL*Plus commands to control the display format rather than applying functions at SQL level.
In SQL*Plus:
SQL> SELECT AVG(sal) FROM emp;
AVG(SAL)
----------
2073.21429
SQL> set numformat $9999D99
SQL> SELECT AVG(sal) FROM emp;
AVG(SAL)
---------
$2073.21
SQL>
If you explicitly ROUND the value to two decimal places, then use the ROUND function:
SQL> set numformat $9999D99
SQL> SELECT ROUND(AVG(sal),2) FROM emp;
ROUND(AVG(SAL),2)
-----------------
$2073.21
SQL>
If you want a specific format for a particular column, then you could use the COLUMN FORMAT command, which would override the SET NUMFORMAT property for that column.
For example,
SQL> set numformat $99999.99
SQL> column comm format 99999.99
SQL> select sal, comm from emp where rownum <=5;
SAL COMM
----------- ---------
$800.00
$1600.00 300.00
$1250.00 500.00
$2975.00
$1250.00 1400.00
I'm trying to query if the characters 'th' is in a column enames for the table emp.
I use the command
select ename from emp where ename like '%th';
However SQL says no row selected.
Any suggestions?
EDIT:
SOLVED
This worked in the end.
SELECT * FROM EMP WHERE ENAME LIKE '%TH%';
Thanks for help!
where ename like '%t%' or ename like '%h%';
or with a regular expression:
where regexp_like(ename, '[th]');
Try:
where UPPER(ename) like '%TH%';