Optional condition in Oracle 10g - sql

For a dummy test, I want to show a list of employees in a web form.
There is a drop down on the web form that contains a short list of departments, like this:
All Depts
Sales Dept
Marketing Dept
Communication Dept
HR Dept
Finance Dept
IT Dept
The drop down item of All Depts has a value of 0.
The following fiddle shows you what I am trying to do:
http://sqlfiddle.com/#!4/59d1f/2
I know I can do this:
IF (deptid = 0) THEN
select firstname, lastname from employees;
ELSE
select firstname, lastname from employees where deptid = :p_deptid
END IF;
But my real situation has a much more convoluted select query that involves joins of multiple tables. So, I don't wanna clutter up my script with repetitive codes.
Can I achieve my goal using CASE WHEN? Or do I have to use dynamic SQL?
Thanks.

SELECT firstname, lastname
FROM employees
WHERE 0 = :p_deptid
OR dept_id = :p_deptid

Related

db2 between query for char columns

I have a scenario where there is a table like below
Table Name:Employee
id EmpName(char 4) -always 4 chars only
1 ARUN
2 BALA
3 LOKI
I wanted a Db2 query which should get me the range of employees who's name starting with A to D. Meaning employees whose name starts with A,B,C and D should be displayed
I have tried this query
select * from employee where empname between 'A000' and 'DZZZ'
Its not working properly
Switch to 'AAAA' as start value.
where empname between 'AAAA' and 'DZZZ'
Will perform much better, if empname is indexed.
ANSI SQL compliant and portable, should work fine on both DB2 and Postgresql.
EDIT:
If empnames shorter than 4 characters will be stored, you can change to:
where empname between 'A' and 'DZZZ'
(Will not miss employee 'A'.)
Use the following version:
select e.*
from employee e
where e.empname >= 'A' and
e.empname < 'E'
This does exactly what you are asking for -- and making no assumptions about the second character. In addition, it is index-safe, if you have an index on empname (although you are selecting so many rows that indexes probably are not relevant).
Try this
select empname from employee where empname like 'A%' or
empname like 'B%' or empname like 'C%' or
empname like 'D%'
order by empname
Try this too
select empname from employee where empname
REGEXP'^[ABCD]'

How do I chaining in an exercise?

the question is :
The company decided to create an automatic user name, consisting of the first 5 letters (left) of the last name + first letter of the first name, chained. View, Employee Table, Last Name, First Name, and New User Name
my answer:
select lastname , FirstName from Employees
select LEFT(lastname,5)as 'lastname',LEFT(firstname,1) as 'firstname' from Employees
select lastname , FirstName from Employees
select LEFT(lastname,5)as 'lastname',LEFT(firstname,1) as 'firstname' from Employees
select LEFT(lastname,5)+LEFT(firstname,1) as Username from Employees
that should do what you wanted.
Depending on what SQL-server version you are querying, you can use concat as well.
SELECT CONCAT(LEFT(lastname,5), LEFT(firstname,1))

How to extract tuples from a table using the projection criteria as month name in oracle?

I have a table 'emp' whose schema is defined as(empno, ename,hiredate) ,I am trying to project out the names of all those employees who joined in the moth of January , so for this I wrote the below query but it not printing any records ,It works fine if I don't use the where clause , else it is not working .
select ename from emp where ( to_char(to_date(extract(month from hiredate),'MM'),'MONTH')) like 'JANUARY'​
Please guide me where I am making mistake .
It is unnecessarily too complicated.
Try to run this select:
select hiredate,extract(month from hiredate) from emp
Then if January is 1 from this select and there arent any mistakes, you can use:
select ename from emp where extract(month from hiredate)=1
Try this:
select ename from emp where ( to_char(to_date(extract(month from hiredate),'MONTH'),'MONTH')) like 'JANUARY'​

Need to pick up tuples that have got different values in other table SQL

I have three tables:
Section (SectID, Description, State)
Employee (EmpID, SectID, Fname, Gname)
JobDone (JobID, EmpID*)
Each Job can be done by several employees and each Section can include several employees.
I need to pick up all Jobs that are done by more that 4 employees and all these employees come from different sections
I've got the following SQL query that should show (haven't tested I yet) EmpID, JobID and SectID for the Jobs that require more than 4 employees but I can't figure out how to show Jobs that have all different Sections
select EmpID, JobID, SectID
from JobDone natural join Employee
where JobID in
(select JobDone.JobID
from JobDone
group by JobDone.JobID
having count(*) > 4
)
Any help would be much appreciated
Thanks
Here is a query to get all the jobids that have at least 4 employees in at least 4 different sections. Since each employee works in the particular section, 4 different sections have at least 4 employees.
select JobID
from JobDone j
join Employee e
on j.EmpID = e.EmpID
group by JobID
having count(distinct e.SectID)>=4
Us above query instead of the sub query in your code.

How To Get Column Description

For example, in a [emp] table, the columns are :
emp_id emp_name emp_role
If the values being inserted in emp_role column values can be 0 (for Administrator), 1 (for Management), 2 (for Employees).
Now is there any way to get those details of a column emp_role (like, 0 for Administrator) along with the table concerned (i.e, [emp]) in SQL server database ?
Thanks.
If you have dictionary table with role definitions it will be something similar to:
select e.emp_id, e.emp_name, r.name
from emp e
inner join role r on e.emp_role = r.id
if not, but you know role names it will be something similar to:
select emp_id, emp_name,
case emp_role when 0 then 'Administrator' when 1 then 'Management' when 2 then 'Employees' end as RoleName
from emp