How can i get output like below? using Oracle SQL [duplicate] - sql

This question already has answers here:
oracle database: split a string by characters
(1 answer)
Split string into tokens in pl sql
(4 answers)
Closed 4 years ago.
Here is my column with String 'ORACLE':
COL
---
ORACLE
I want the following output:
O
R
A
C
L
E

this will work
SELECT SUBSTR ('ORACLE', LEVEL, 1) FROM dual
Connect by level <= length ('ORACLE');

Try below
SELECT SUBSTR(COL, LEVEL, 1) x from tablename
CONNECT BY LEVEL <= LENGTH(COL)

Related

how to get the string before first occurrence of a special character [duplicate]

This question already has answers here:
How to Select a substring in Oracle SQL up to a specific character?
(8 answers)
Closed 1 year ago.
i have a column containing hostnames in the format of :
oraclehost.server.region.company.net
How to extract the oraclehost part from the hostname i.e the string before the first ..
Please sugges.Thanks.
SELECT REGEXP_SUBSTR(HOSTNAMES, '[^.]+', 1, 1) FROM MYTABLE;
Alternatively, substr + instr combination which would probably perform better for large data sets:
substr(hostnames, 1, instr(hostnames, '.') - 1)
For example:
SQL> with mytable (hostnames) as
2 (select 'oraclehost.server.region.company.net' from dual)
3 select substr(hostnames, 1, instr(hostnames, '.') - 1) result
4 from mytable;
RESULT
----------
oraclehost
SQL>

Regular Expression to parse 112^3^1^1^ [duplicate]

This question already has answers here:
How to parse data using REGEXP_SUBSTR?
(3 answers)
Making columns from a string in oracle
(1 answer)
Parsing pipe delimited, irregular strings in Oracle
(2 answers)
Closed 4 years ago.
I need to parse a string like 112^3^1^1^ into separate values.
The first ('112') is order_no, second one is line_no, third one is release_no, and the fourth one is receipt_no.
What kind of regex can I use in Oracle?
with demo (str) as
( select '112^3^1^1^' from dual )
select str
, regexp_substr(str,'[^^]+',1,1) as order_no
, regexp_substr(str,'[^^]+',1,2) as line_no
, regexp_substr(str,'[^^]+',1,3) as release_no
, regexp_substr(str,'[^^]+',1,4) as receipt_no
from demo;
STR ORDER_NO LINE_NO RELEASE_NO RECEIPT_NO
---------- ---------- --------- ------------ ----------
112^3^1^1^ 112 3 1 1
This depends on each component having a value, because the pattern it is using is 'one or more characters that are not ^'. It would need a bit of tweaking if a value was missing, e.g. '112^3^^1^' (no release_no).

How to query for a specific part of a string field with SQL? [duplicate]

This question already has answers here:
How to get substring in SQLIte?
(3 answers)
Closed 7 years ago.
Is it possible to build a query/SELECT-Statement with SQL like that:
SELECT name
FROM MyTable
WHERE name[2] = 'x'
WHERE substr(name, 2, 1) = 'x'
SQLFiddle demo

How to concatenate multiple rows into one field in sql server [duplicate]

This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 8 years ago.
Using simple query , I can do something like
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
and get:
shopping
fishing
coding
but instead I just want 1 row, 1 col:
shopping, fishing, coding
for ref-- Can I concatenate multiple MySQL rows into one field?
I want to do this in sql server ??
SQL Server doesn't have great support for aggregate string concatenation. But you can do:
select stuff((select ', ' + hobbies
from peoples_hobbies
where person_id = 5
for xml path ('')
), 1, 2, '') as hobbies;

SQL command to split by "/" [duplicate]

This question already has answers here:
How to split a comma-separated value to columns
(38 answers)
Closed 9 years ago.
I have a data column with values like this:
table: type
ID|Descriptions
1 |chair/table/plates/
2 |chair2/table2/plates2/
What will be the SQL command to split it by "/" ?
Expected output
ID|Description1|Description2|Description3|
1 |chair |table |plates
2 |chair2 |table2 |plates2
Try this
;WITH Split_Descr ([ID],[Descriptions], xmldescr)
AS
(
SELECT [ID],
[Descriptions],
CONVERT(XML,'<Descr><desc>'
+ REPLACE([Descriptions],'/', '</desc><desc>') + '</desc></Descr>') AS xmldescr
FROM Table1
)
SELECT [ID],
xmldescr.value('/Descr[1]/desc[1]','varchar(100)') AS Descr1,
xmldescr.value('/Descr[1]/desc[2]','varchar(100)') AS Descr2,
xmldescr.value('/Descr[1]/desc[3]','varchar(100)') AS Descr3
FROM Split_Descr
SQL FIDDLE DEMO