How to write IIF condition in Oracle [duplicate] - ssms

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.

Related

IF statement within a SELECT - SQL [duplicate]

This question already has answers here:
Not equal <> != operator on NULL
(10 answers)
Closed 4 years ago.
I'm trying to write a query, that selects a number of values. I only want it to select one of the values if it isn't null.
I'm trying to use a case when but it is erroring.
SELECT pick_no,
pd.product,
from_warehouse,
to_warehouse,
qty_pick,
qty_check,
qty_picked,
qty_checked,
long_description,
ROUND(qty_pick / stk.pallet_unit_qty, 2) as [PalletQty],
ph.date_picking,
stk.bin_no,
CASE WHEN qty_picked <> null
THEN ROUND(qty_picked / stk.pallet_unit_qty, 2) as [pltCheck]
ELSE '0' END
FROM
Null is a tricky beast, you can't use equality operands on it. Also your else clause was returning a CHAR when the first clause returns a FLOAT :
CASE WHEN qty_picked IS NOT NULL
THEN ROUND(qty_picked / stk.pallet_unit_qty, 2) as [pltCheck]
ELSE 0 END
A simpler way to achieve your goal is to use COALESCE :
COALESCE(ROUND(qty_picked / stk.pallet_unit_qty, 2),0) as [pltCheck]

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;

PL/SQL split varchar [duplicate]

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

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