SQL command to split by "/" [duplicate] - sql

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

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>

How to write IIF condition in Oracle [duplicate]

This question already has answers here:
ORACLE IIF Statement
(3 answers)
Closed 2 years ago.
How do we write IIF condition in Oracle.
IIF(ItemType = '-1' , (Select CAST(CAST(ConfigXml as XML).query('data(/configurations/config/itemtypeid)') as nvarchar (64)) from EmailCaptureConfig where OwnerID = 142 and ConfigID = 1), ActionObjecttype) as ActionObject
I need to convert the above IIF into ORACLE but am unable to do so. How do we go forward with this.
Similar to this:
decode(ItemType, '-1' , (Select CAST(CAST(ConfigXml as XML).query('data(/configurations/config/itemtypeid)') as nvarchar (64)) from EmailCaptureConfig where OwnerID = 142 and ConfigID = 1), ActionObjecttype) as ActionObject
Obviously the xml datatype works differently in Oracle than Microsoft.

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

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)

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;