d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick / Easy Sql > Info Inside
Add Reply New Topic New Poll
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Sep 28 2013 02:55pm
Request 3: (3 pts) Write/execute a SQL statement that lists all the data in each column in the Departments table. If a column might contain NULL values, the phrase “-NONE-“ must print in that column for that row. For this last request use one or more SQL single-row functions to accomplish this. Do not use an iSQL*PLUS report formatting command such as SET COLUMN * NULL ‘-NONE-’ which affects all columns during a session.


SELECT department_id, department_name, NVL(manager_id, 0 ) AS manager_id, location_id
FROM departments


MY question: So the code above is my attempt at the request 3: but as you can see i am not having the nvl return "none" rather i am returning 0
I am not sure how to make it say none, like if i put none where the 0 is it doesnt run the query because i think NVL only allows for integers rather than string
Does anyone have a suggestion on how i can fix this?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 28 2013 02:58pm
use a case statement.
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Sep 28 2013 03:16pm
Quote (carteblanche @ Sep 28 2013 03:58pm)
use a case statement.


Code

1. CASE column_name
2. WHEN condition1 THEN result1
3. WHEN condition2 THEN result2
4. ...
5. ELSE result
6. END


So im having problem with condition 1 i think because i keep getting " "FROM keyword not found where expected"

My current case looks like :

Code

CASE manager_id
WHEN manager_id = null THEN manager_id = 'none'
ELSE manager_ID


sorry for this - im still noob at sql :X

This post was edited by Noobtard on Sep 28 2013 03:17pm
Member
Posts: 16,411
Joined: Apr 9 2007
Gold: 12,059.17
Sep 28 2013 03:24pm
CASE
WHEN manager_id is null THEN 'none'
ELSE manager_ID
end


edit:
nvl should also work and this is probably prefered way to solve this problem.

This post was edited by Ironfister on Sep 28 2013 03:29pm
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Sep 28 2013 03:37pm
Quote (Ironfister @ Sep 28 2013 04:24pm)
CASE
  WHEN manager_id is null THEN 'none'
ELSE manager_ID
end


edit:
nvl should also work and this is probably prefered way to solve this problem.


Yeah this is odd becaues:

Code
SELECT department_id, department_name, manager_id, location_id
CASE manager_id
WHEN manager_id = NULL THEN 'none'
ELSE manager_id
END
FROM departments;


I still get error with this current code?

"ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"

It says the error is where the 'm' is after the CASE -- That is where it is saying it is located. But i double checked the table column names etc, and it is pointing to the correct column?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 28 2013 04:13pm
Google for examples of the case statement. i find it odd that you'd use the column name right after CASE then use the column name again in the condition. i think it should only be in one place, not both. otherwise what's the point?

/edit:

http://www.techonthenet.com/oracle/functions/case.php

yeah notice how those examples only use it in one place?

This post was edited by carteblanche on Sep 28 2013 04:15pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll