How to split a column using only Sql? - 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;

Related

Oracle SQL Regexp capture only the number from the string

I am trying to extract the number from the row.
i am trying this in Oracle 11G
CREATE TABLE test1
(
COL_NAME VARCHAR2(100)
);
insert into test1 values ('DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS');
select COL_NAME, REGEXP_REPLACE(COL_NAME, '^([A-Z0-9$]{17,})_.*', '\17') as BEGINNING from TEST1
and COL_NAME='DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS'
EXISTING RESULT
COL_NAME BEGINNING
DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS
EXPECTED RESULT
COL_NAME BEGINNING
DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS 25
Total Patterns
DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS
DWH_SCHEMA.ABC_02_BC_ORDER_STATUS
DWH_BID.ABC_11_BC_ORDER_STATUS
Please note i have total 3 patterns, how to achieve the desired result , seems my REGEXP logic is not working as per the EXPECTED RESULTS
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=8aafffde010ae586f8a79974ee1ff140
Use regexp_substr():
select t1.*,
regexp_substr(col_name, '[0-9]+')
from test1 t1;
Here is a db<>fiddle.

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

simple SQL not working on table

Have this odd behavior, was wondering if anyone can help.
Table:
STATUS,TYPE,ID
passed,requirement,J1X11986
passed,requirement,J1X11986-ABC
SQL> select id from MY_TABLE;
ID
--------------------------------------------------------------------------------
J1X11986
SQL> select status from MY_TABLE where id='J1X11986';
no rows selected
SQL> select status from MY_TABLE where id='J1X11986-ABC';
RESULT
------
passed
Try with a TRIM. There may be white spaces.
If you are using SQL server use below query.
Select status
From MyTable
Where LTRIM (RTRIM (id))='J1X11986'

add $ and round 2 decimal places 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

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