add $ and round 2 decimal places SQL - sql

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

Related

Display all the employees whose name contain 5 characters length in ORACLE

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 ('_____');

How to split a column using only Sql?

Lets say my table is like,
data
----
smith700
then my output should be
name sal
----- ---
smith 700
I only can use SQL statement no PL/SQL statement is allowed...How to do it?
Use SubStr
SELECT SUBSTR('smith700',1,5),SUBSTR('smith700',6,3)
UPDATE
For varying lengths of numbers and characters you could try
SELECT REGEXP_REPLACE('smith700', '[A-Za-z]','') FROM DUAL;
SELECT REGEXP_REPLACE('smith700', '[0-9]') FROM DUAL;
select substr(data,1,5) name, substr(data,6,3) sal from your_tab;

Oracle SQL query to see if characters are in a column

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%';

How can I sort by a table column in varying cases (Oracle)

How can I sort a table with a column of varchar2 with characters in varying cases (UPPER and lower)?
For example, when I do an order by of the Name column, I get the following results:
ANNIE
BOB
Daniel
annie
bob
What I want is something like this:
ANNIE
annie
BOB
bob
Daniel
Use lower(field), e.g.
select * from tbl order by lower(name)
If you need to address special characters for non-english languages then the other answers about NLSSORT may be what you need. If you don't I would try and KISS and use lower() as it is very easy to remember and use and be read by others (maintainability).
Another option is the use of the NLSSORT function to perform linguistic sorting:
SQL> with test as (select 'ANNIE' as col from dual
2 union all select 'BOB' from dual
3 union all select 'Daniel' from dual
4 union all select 'annie' from dual
5 union all select 'bob' from dual
6 union all select 'Ångström' from dual
7 union all select 'ångström' from dual)
8 select col
9 from test
10 order by nlssort(col, 'NLS_SORT = WEST_EUROPEAN')
11 /
COL
----------
Ångström
ångström
ANNIE
annie
BOB
bob
Daniel
The advantages are more flexibility. One can sort characters with accents as well as different cases together. One can choose to treat some characters in a language specific way by specifying different values for NLS_SORT. Defines an order within the set of equivalent characters. So 'A' and 'a' are sorted together, but within the 'a's, the upper case comes first. Disadvantages I expect that NLSSORT uses more CPU than LOWER, though I have not bench marked it. And NLSSORT will only use a prefix of longer strings:
The string returned, also known as the collation key, is of RAW data
type. The length of the collation key resulting from a given char
value for a given collation may exceed 2000 bytes, which is the
maximum length of the RAW value returned by NLSSORT. In this case,
NLSSORT calculates the collation key for a maximum prefix, or initial
substring, of char so that the calculated result does not exceed 2000
bytes. For monolingual collations, for example FRENCH, the prefix
length is typically 1000 characters. For multilingual collations, for
example GENERIC_M, the prefix is typically 500 characters. The exact
length may be lower or higher depending on the collation and the
characters contained in char.
If you're on relatively recent versions of Oracle, you should look at setting NLS_SORT/NLS_COMP, rather than using the LOWER() function.
If you don't want to globally affect the instance, you can use the NLSSORT() function to set the NLS_SORT for the scope of a specific query.
SQL> create table case_insensitive(a varchar2(10));
Table created.
SQL> insert into case_insensitive values('D');
1 row created.
SQL>
SQL>
SQL> c/'D/'c
1* insert into case_insensitive values('c')
SQL> /
1 row created.
SQL> c/'c/'B
1* insert into case_insensitive values('B')
SQL> /
1 row created.
SQL> c/'B/'a
1* insert into case_insensitive values('a')
SQL> /
1 row created.
SQL> commit;
Commit complete.
SQL> select * from case_insensitive;
A
----------
D
c
B
a
SQL> select * from case_insensitive order by a;
A
----------
B
D
a
c
SQL> select * from case_insensitive order by nlssort(a,'NLS_SORT=BINARY_CI');
A
----------
a
B
c
D
A good example of this can be found here.
You can use INITCAP e.g.
SELECT fld FROM tbl ORDER BY INITCAP(fld) ASC;
You can use the Order by cluse for this
select col_name from table_name
order by col_name ;

Best way to handle optional parameter with null value in query

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);