How can I convert Oracle round statement to SQL server syntax? - oracle

How can I convert:
round(DECODE(PC_TTL.WAGETYPE, 3085, PC_TTL.WORKHOURS, DECODE(PC_TTL.UNIT, '010', PC_TTL.WORKHOURS/8, PC_TTL.WORKHOURS)) , 2)
to SQL server syntax?

Unless I misunderstood the DECODE logic, this should do the trick:
ROUND(
CASE WHEN PC_TTL.WAGETYPE = 3085 THEN PC_TTL.WORKHOURS
ELSE
CASE WHEN PC_TTL.UNIT = '010' THEN PC_TTL.WORKHOURS/8
ELSE PC_TTL.WORKHOURS
END
END, 2)
This will work in SQL Server and Oracle.

You can do it using a simple case statement.
ROUND (
CASE
WHEN PC_TTL.WAGETYPE = 3085 THEN PC_TTL.WORKHOURS
WHEN PC_TTL.UNIT = '010' THEN PC_TTL.WORKHOURS / 8
ELSE PC_TTL.WORKHOURS
END, 2 )

Related

not in condition not working with Case statement PLSQL

NOT IN condition, it is not working. I have these records in the table
proj_comp_id
proj_no
1050-WE-0005
8661
TOGG0000001
1581
5207-00GAC01AA502
8662
so it should add an error for the 2nd value..but not working pls help. below code is adding errors for 1 and 3rd lines also.
SELECT p.proj_comp_no AS primarykey,
p.proj_comp_id AS proj_comp_id,
pr.proj_id AS proj_id,
p.proj_no,
CASE
WHEN p.proj_comp_id NOT IN ('5207-%',
'1051-%',
'1050-%',
'1048-%',
'1049-%',
'2006-%',
'5203-%',
'1055-%',
'1056-%',
'1054-%') and p.proj_no NOT like (1581)
THEN
'1TAG_ID does not start with project prefix, pls add project prefix(5207-, 1049-,1048)'
ELSE
'NO ERROR'
END AS tag_ID_ERROR,
p.descr AS descr
FROM proj_comp p
LEFT JOIN std_mat_cls l
ON l.std_mat_cls_no = p.std_mat_cls_no
LEFT JOIN std_mat b
ON p.proj_comp_id = b.std_mat_id
LEFT JOIN proj pr
ON p.proj_no = pr.proj_no
WHERE (SELECT m.mat_stat_ext_id
FROM mat_stat_ext m
WHERE p.mat_stat_ext_no = m.mat_stat_ext_no) IN ('N', 'P')
AND b.std_mat_id IS NULL
***
That must be because IN isn't appropriate in this context. You should switch to something else, e.g. separate LIKE conditions:
No:
WHEN p.proj_comp_id NOT IN ( '5207-%', '1051-%', '1050-%', etc.
Yes:
WHEN p.proj_comp_id NOT LIKE '5207-%'
AND p.proj_comp_id NOT LIKE '1051-%'
AND p.proj_comp_id NOT LIKE '1050-%'
etc.
The other way to test this could be solved using the InStr() function...
WHEN
InStr('5207-, 1051-. 1050-, 1048-, 1049-, 2006-, 5203-, 1055-, 1056-, 1054-', SubStr(p.proj_comp_id, 1, 5)) = 0

ODP.NET, Managed reading LONG column results in ORA-01009

I'm trying to get the source of a view in my .net app.
To do this, I query DBA_VIEWS: it has a column TEXT with exactly what I need. The type is LONG.
If I do it using the Unmanaged driver, everything works as expected.
The same code with Managed driver results in ORA-01009: missing mandatory parameter.
Adding a transaction around the command and using breakpoint and "slow" steps sometimes results in the same code working.
ODP.NET version is 19, Oracle DB is 18c Express Edition. Strangely enough, the same code works just fine with Oracle Database 12c regardless of driver type.
Is there maybe some setting I need to change on the database or in code? I'm completely lost here.
Code I'm using for testing:
Imports System.Data.Common
Imports Oracle.ManagedDataAccess
'Imports Oracle.DataAccess
Module Views
Function CreateCommand(Connection As DbConnection) As System.Data.Common.DbCommand
Dim cmd As Data.Common.DbCommand = Connection.CreateCommand()
With CType(cmd, Client.OracleCommand)
.BindByName = True
.FetchSize = &H100000 '1 Mb
.InitialLONGFetchSize = -1 'the entire LONG or LONG RAW data is prefetched and stored in the fetch array.
.InitialLOBFetchSize = -1 'the entire LOB data is prefetched and stored in the fetch array.
End With
Return cmd
End Function
Sub query()
Try
Using DBConnection = New Client.OracleConnection("User ID=TESTUSER;Password=TESTPWD;Data Source=TESTDB;Pooling=True")
DBConnection.Open()
Using DBConnection.BeginTransaction()
Using cmdSQL = CType(CreateCommand(DBConnection), Client.OracleCommand)
cmdSQL.CommandText = "select TEXT from DBA_VIEWS where VIEW_NAME = :0"
Dim p = cmdSQL.CreateParameter()
p.ParameterName = "0"
p.Value = "TEST_VIEW"
cmdSQL.Parameters.Add(p)
Dim sw = Stopwatch.StartNew
Using rdr = cmdSQL.ExecuteReader
rdr.FetchSize = 2 ^ 20
While rdr.Read
Dim row(rdr.FieldCount - 1) As Object
rdr.GetProviderSpecificValues(row)
Dim x = row(0)
Console.WriteLine($"{x.ToString.Length} bytes")
End While
End Using
Console.WriteLine($"{sw.ElapsedMilliseconds} ms")
End Using
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
End Module
You can use an anonymous block with output parameter and a call to ExecuteNonQuery. Your command text will be
"begin select TEXT into :0 from DBA_VIEWS where VIEW_NAME = :1; end;"
Add 2 parameters. Make sure that
' Parameter #1 has
p.Direction = ParameterDirection.Output
p.OracleDbType = OracleDbType.Long
p.Size = 1000000
And use command cmd.ExecuteNonQuery(). Then, when parameter is retrieved, just use its value
Dim txt As String = cmd.Parametersp[0].Value.ToString()
It's a pity, Oracle deprecated LONG data type for ages but LONG data is still used many times for internal data.
You could write a function and then get the data by calling the function:
create or replace function GetViewText(v in varchar2) return clob is
ret CLOB;
BEGIN
FOR aRow IN (SELECT TEXT FROM DBA_VIEWS WHERE VIEW_NAME = v) LOOP
ret := aRow.TEXT;
-- or ret := TO_CLOB(aRow.TEXT);
END LOOP;
RETURN ret;
END;
Yet another way from this answer is to (ab)use dbms_xmlgen.getxml.
We can either use it to query a single view's code (as in my original question)
with input as (
select
:0 as VIEW_NAME
from dual
)
SELECT
substr(
text_xml,
instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'),
instr(text_xml, '</LONGCOL>', -1) - (instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'))
) as TEXT
from
(
-- getxml can return malformed xml (which is good in this case)
-- while getxmltype can not.
select dbms_xmlgen.getxml(q'{
SELECT TEXT as LONGCOL
FROM SYS.DBA_VIEWS
WHERE VIEW_NAME = '}' || input.VIEW_NAME || q'{'
}') as text_xml
from input
)
or create our own DBA_VIEWS version.
create or replace view APP_SCHEMA.DBA_VIEWS
as
select
OWNER, VIEW_NAME, TEXT_LENGTH,
case
when (TEXT_VC is not null and TEXT_LENGTH <= 4000)
then to_clob(TEXT_VC)
when TEXT is NULL
then NULL
else (
SELECT
substr(
text_xml,
instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'),
--instr(text_xml, '</LONGCOL>', -1) - (instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'))
TEXT_LENGTH
) as TEXT
from
(
-- getxml can return malformed xml (which is good in this case)
-- while getxmltype can not.
select dbms_xmlgen.getxml(q'{
SELECT TEXT as LONGCOL
FROM SYS.DBA_VIEWS
WHERE OWNER = '}' || OWNER || q'{'
and VIEW_NAME = '}' || VIEW_NAME || q'{'
}') as text_xml
from dual
)
)
end as TEXT,
TEXT_VC, TYPE_TEXT_LENGTH, TYPE_TEXT, OID_TEXT_LENGTH, OID_TEXT, VIEW_TYPE_OWNER, VIEW_TYPE, SUPERVIEW_NAME, EDITIONING_VIEW, READ_ONLY, CONTAINER_DATA, BEQUEATH, ORIGIN_CON_ID, DEFAULT_COLLATION, CONTAINERS_DEFAULT, CONTAINER_MAP, EXTENDED_DATA_LINK, EXTENDED_DATA_LINK_MAP, HAS_SENSITIVE_COLUMN
from sys.dba_views
;

MERGE using Rowid Oracle Conversion in Postgres

Can some help me the best way convert the below Oracle code to Postgres
MERGE INTO INCLUSION_EXCLUSION_ALL A
USING (SELECT DISTINCT IEALL.rowid rid, IE.BLACKOUT_DURATION, ie.BLACKOUT_CATEGORY
FROM INCLUSION_EXCLUSION_ALL IEALL, INCLUSION_EXCLUSION_RULES IE
WHERE IE.PRI_PROMOTION_ID = IEALL.PRI_PROMOTION_ID
AND IE.PRI_OFFER_ID = IEALL.PRI_OFFER_ID
AND IE.SEC_PROMOTION_ID = IEALL.SEC_PROMOTION_ID
AND IE.SEC_OFFER_ID = IEALL.SEC_OFFER_ID
AND IE.BLACKOUT_DURATION > 0
AND (IEALL.BLACKOUT_DURATION <> IE.BLACKOUT_DURATION OR IEALL.BLACKOUT_CATEGORY <> IE.BLACKOUT_CATEGORY)
--AND IE.INCL_EXCL_IND <> 'I'
AND IE.SECONDARY_TYPE = 'B') B
ON (A.rowid = B.rid)
WHEN MATCHED THEN UPDATE SET A.BLACKOUT_DURATION = B.BLACKOUT_DURATION, A.BLACKOUT_CATEGORY = B.BLACKOUT_CATEGORY;
Thank You,
I want to know different ways on how we can do it as the rowid's are involved in here and are not compatible with Postgres esp in Joining using Alias even by using CTID,
Please advise

Error with sql oracle

i'm using spagobi with oracle DBMS but when i want to get values where year between 2010 and 2014 a got error : right parenthesis missing
select (sum(d.taux_depot *100)/count(r.trimestre) ) as taux , trimestre as trimestre
from datamart_cnss d , ref_temps r
where d.ID_TEMPS = r.ID_TEMPS
and (case when $P{anneecnss}=123 then (r.annee between 2010 and 2014 ) else $P{anneecnss} end) = r.annee
and (case when To_CHAR($P{regimecnss})=123 then To_CHAR(d.id_regime) else To_CHAR($P{regimecnss}) end) = To_CHAR(d.id_regime)
and (case when To_CHAR($P{bureau_cnss})=123 then To_CHAR(d.id_bureau) else To_CHAR($P{bureau_cnss}) end) = To_CHAR(d.id_bureau)
group by trimestre
order by trimestre asc
Thank you
This is not a valid construct:
case when $P{anneecnss}=123 then (r.annee between 2010 and 2014 ) else $P{anneecnss} end
You cannot have a condition inside the then part, just a value or expression that you can then compare with something else.
To apply that filter selectively you don't need to use a case statement, use and and or; I think this is equivalent:
where d.ID_TEMPS = r.ID_TEMPS
and (($P{anneecnss} = 123 and r.annee between 2010 and 2014)
or ($P{anneecnss} != 123 and $P{anneecnss} = r.annee))
and ($P{regimecnss} = 123 or To_CHAR($P{regimecnss}) = To_CHAR(d.id_regime))
and ($P{bureau_cnss} = 123 or To_CHAR($P{bureau_cnss}) = To_CHAR(d.id_bureau))
...

Problem in oracle query

Hai guys,
I've a query in which i need to interchange the values of two fields.
The query is as follows:
SELECT TO_DATE(A.G_LEDGER_DATE,'dd/mm/YYY')as G_LEDGER_DATE,C.ACC_MASTER_NAME,
A.G_LEDGER_REF_NO ,
NVL(CASE WHEN B.G_LEDGER_SECTION = 1 THEN
CASE WHEN
(SELECT COUNT(*)FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_DR_VALUE,
NVL(CASE WHEN B.G_LEDGER_SECTION = -1 THEN
CASE WHEN
(SELECT COUNT(*) FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_CR_VALUE,
B.G_LEDGER_SECTION,C.ACC_MASTER_ID,SUBSTR(A.G_LEDGER_REF_NO,0,3) AS Types,'Z' as OrderChar ,
CASE WHEN A.G_LEDGER_REMARK IS NULL THEN B.G_LEDGER_REMARK ELSE A.G_LEDGER_REMARK END AS Narration
FROM SOSTRANS.ACC_GEN_LEDGER A
LEFT OUTER JOIN SOSTRANS.ACC_GEN_LEDGER B ON A.G_LEDGER_ID = B.G_LEDGER_ID
LEFT OUTER JOIN SOSMASTER.ACC_ACCOUNT_MASTER C ON A.ACC_MASTER_ID = C.ACC_MASTER_ID WHERE A.G_LEDGER_CANCEL='N' AND
B.ACC_MASTER_ID = 'MSOS000001' AND
A.ACC_MASTER_ID <> 'MSOS000001' AND
A.G_LEDGER_SECTION <> B.G_LEDGER_SECTION AND
A.G_LEDGER_DATE >= '25/sep/2009' AND
A.G_LEDGER_DATE<='26/sep/2009'
ORDER BY OrderChar,G_LEDGER_DATE
Now i get the output as
... G_LEDGER_DR_VALUE G_LEDGER_CR_VALUE .....
... 2000 0 .....
... 3000 0 .....
... -1000 0 .....
I need to get the negetive value of the G_LEDGER_DR_VALUE side in G_LEDGER_CR_VALUE and if negetive value exists in G_LEDGER_CR_VALUE then it should be in the G_LEDGER_DR_VALUE field
Can anyone help me to solve this?
If I understood your question well, you select a value (that I will call g_ledger_value) that you want to appear in a different column depending on its sign.
This is how I would do it :
SELECT
CASE WHEN t.g_ledger_value>0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_dr_value,
CASE WHEN t.g_ledger_value<0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_cr_value
FROM
(SELECT g_ledger_value FROM mytable) t;
It sounds like a combination of SIGN() and CASE is what you need ...
CASE WHEN SIGN(G_LEDGER_DR_VALUE) = -1 then ...
ELSE ...
END
etc
SELECT G_LEDGER_DR_VALUE,
CASE WHEN G_LEDGER_DR_VALUE < 0
THEN G_LEDGER_CR_VALUE
ELSE G_LEDGER_DR_VALUE
END
FROM (...)
Is it that you mean? I suggest calculate values of CR___VALUE and DR_VALUE in subquery, and then in wrapping query make CASE which returns you correct value.

Resources