PL/SQL split varchar [duplicate] - sql

This question already has answers here:
Is there a function to split a string in Oracle PL/SQL?
(12 answers)
Closed 8 years ago.
I get this from a table John Doe.
What is the best way to split this to have into two variable this :
lastName = 'Doe';
firstName = 'John';

I guess this should work :
SELECT SUBSTR(NameColumn, 1, INSTR(NameColumn,' ',1)) AS firstName,
SUBSTR(NameColumn, INSTR(NameColumn,' ',1)+1) FROM tablName

Related

Oracle SQL Developer - SUBSTR and LIKE [duplicate]

This question already has answers here:
ORACLE:- SELECT First Names , by removing space after the first word
(1 answer)
How to check if the length of a string is more than one word and keep only the first word else keep the entire string in SQL?
(2 answers)
Closed last year.
I have a column NAME_SURNAME (made of names and surnames) from a table people:
John Smith
Tim Burton
Harry Potter
I need to write a query to obtain only the names:
John
Tim
Harry
I am trying in this way nad it doesn't work:
select NAME_SURNAME, substr(NAME_SURNAME, 1, LIKE '% %')
from PEOPLE
I don't figure out how to use LIKE in this exercise.
Thank you really much
Use INSTR with SUBSTR to take a substring up to the first space:
SELECT NAME_SURNAME, SUBSTR(NAME_SURNAME, 1, INSTR(NAME_SURNAME, ' ') - 1)
FROM PEOPLE;
Demo
For a regex approach, we can use REGEXP_SUBSTR:
SELECT NAME_SURNAME, REGEXP_SUBSTR(NAME_SURNAME, '^\S+') AS FIRST_NAME
FROM PEOPLE;

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