ORA-00933 SQL command not properly ended - sql

I need to convert MSSQL query to Oracle but end up with SQL command not properly ended.
Here is MSSQL query
SELECT * FROM [dbo].[trade] AS [Extent1]
WHERE EXISTS (
SELECT 1 AS [C1] FROM
[dbo].[findetail] AS [Extent2]
INNER JOIN [dbo].[transact] AS [Extent3] ON [Extent2].[transact] = [Extent3].[transact]
WHERE [Extent1].[trade] = [Extent2].[trade]
AND 'ACCR' = [Extent3].[subledger]
AND [Extent3].[date] = '2016-03-18T00:00:00'
)
Converting it to Oracle SQL I end with this.
SELECT * FROM trade Extent1
WHERE EXISTS
(SELECT 1 C1 FROM findetail Extent2
JOIN transact Extent3
ON Extent2.transact=Extent3.transact
WHERE Extent1.trade=Extent2.trade
AND 'ACCR'=Extent3.subledger
AND Extent3.date='2016-03-18T00:00:00'
);
and receive error above.

Date formats are different in Oracle. Perhaps something like this:
SELECT *
FROM trade Extent1
WHERE EXISTS (SELECT 1
FROM findetail Extent2 JOIN
transact Extent3
ON Extent2.transact = Extent3.transact
WHERE Extent1.trade = Extent2.trade AND
Extent3.subledger = 'ACCR' AND
Extent3."date" = DATE '2016-03-18'
);

DATE is a reserved word so needs to the surrounded in double quotes and, I am assuming that it is of DATE data type so you will probably need to convert the string:
SELECT *
FROM trade t
WHERE EXISTS (
SELECT 1
FROM findetail f
JOIN transact r
ON f.transact = r.transact
WHERE t.trade = f.trade
AND 'ACCR' = r.subledger
AND r."DATE" = TO_DATE( '2016-03-18T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS' )
);
If you just use the string in r."DATE" = '2016-03-18T00:00:00' then Oracle will implicitly try to convert the string literal using the TO_DATE() function with the NLS_DATE_FORMAT session parameter as the format mask. If they match then it will work but this is a client variable so can be changed and then the query will break without the code having changed (and be a pain to debug). The simple answer is to ensure that you compare date value by either using TO_DATE() and specifying the format mask (as per the query above) or to use an ANSI date literal DATE '2016-03-18' (which is independent of the NLS settings).

Related

I am trying to convert Oracle view into SQL, but i am having issue with query performance

Oracle
(TO_CHAR(P.EstimatedInServiceDate, 'YYYY') =
(SELECT SUBSTR(ConfigurationValue, -4)
FROM PETE.vw_Configuration
WHERE configurationkey = 'CurrentPATYear'))
SQL - I tried to convert above code it is returning a data fine but my query is taking too long to run because of this particular chunk. If any better way to do this please let me know.
(Cast(DatePart(Year,P.EstimatedInServiceDate)as varchar) =
(SELECT right(ConfigurationValue, 4)
FROM Configuration
WHERE configurationkey = 'CurrentPATYear'))
You can use datepart() only :
where datepart(year, P.EstimatedInServiceDate) = (SELECT right(ConfigurationValue, 4)
FROM Configuration
WHERE configurationkey = 'CurrentPATYear')
)
It's a poor practice in SQL Server (or Oracle) to apply expressions to table columns in a WHERE-clause, as it makes the expression non-Sargable.
Instead write this something like:
P.EstimatedInServiceDate >=
(SELECT ConfigurationValue
FROM Configuration
WHERE configurationkey = 'CurrentPATYear')
and
P.EstimatedInServiceDate < dateadd(year,1,
(SELECT ConfigurationValue
FROM Configuration
WHERE configurationkey = 'CurrentPATYear'))

Issue With SQL Pivot Function

I have a SQL query where I am trying to replace null results with zero. My code is producing an error
[1]: ORA-00923: FROM keyword not found where expected
I am using an Oracle Database.
Select service_sub_type_descr,
nvl('Single-occupancy',0) as 'Single-occupancy',
nvl('Multi-occupancy',0) as 'Multi-occupancy'
From
(select s.service_sub_type_descr as service_sub_type_descr, ch.claim_id,nvl(ci.item_paid_amt,0) as item_paid_amt
from table_1 ch, table_" ci, table_3 s, table_4 ppd
where ch.claim_id = ci.claim_id and ci.service_type_id = s.service_type_id
and ci.service_sub_type_id = s.service_sub_type_id and ch.policy_no = ppd.policy_no)
Pivot (
count(distinct claim_id), sum(item_paid_amt) as paid_amount For service_sub_type_descr IN ('Single-occupancy', 'Multi-occupancy')
)
This expression:
nvl('Single-occupancy',0) as 'Single-occupancy',
is using an Oracle bespoke function to say: If the value of the string Single-occupancy' is not null then return the number 0.
That logic doesn't really make sense. The string value is never null. And, the return value is sometimes a string and sometimes a number. This should generate a type-conversion error, because the first value cannot be converted to a number.
I think you intend:
coalesce("Single-occupancy", 0) as "Single-occupancy",
The double quotes are used to quote identifiers, so this refers to the column called Single-occupancy.
All that said, fix your data model. Don't have identifiers that need to be quoted. You might not have control in the source data but you definitely have control within your query:
coalesce("Single-occupancy", 0) as Single_occupancy,
EDIT:
Just write the query using conditional aggregation and proper JOINs:
select s.service_sub_type_descr, ch.claim_id,
sum(case when service_sub_type_descr = 'Single-occupancy' then item_paid_amt else 0 end) as single_occupancy,
sum(case when service_sub_type_descr = 'Multi-occupancy' then item_paid_amt else 0 end) as multi_occupancy
from table_1 ch join
table_" ci
on ch.claim_id = ci.claim_id join
table_3 s
on ci.service_type_id = s.service_type_id join
table_4 ppd
on ch.policy_no = ppd.policy_no
group by s.service_sub_type_descr, ch.claim_id;
Much simpler in my opinion.
for column aliases, you have to use double quotes !
don't use
as 'Single-occupancy'
but :
as "Single-occupancy",

Pentaho CDE SQL Query with numerical param

I would like to know if it is possible to perform an SQL query and pass it a numerical parameter. Let's suppose that I have the following query:
SELECT
CONCAT(cuatrimestre,' Cuatrimestre') As quarter
, ROUND(SUM(fact_Ventas.cantidad * precioUnitario),0) as amount
FROM fact_Ventas
INNER JOIN dim_Tiempos ON
fact_Ventas.idAnio = dim_Tiempos.idAnio AND
fact_Ventas.idMes = dim_Tiempos.idMes AND
fact_Ventas.idDia = dim_Tiempos.idDia
INNER JOIN dim_Clientes ON dim_Clientes.idCliente = fact_Ventas.idCliente
INNER JOIN dim_Productos ON dim_Productos.idProducto = fact_Ventas.idProducto
WHERE
CAST(fact_Ventas.idAnio As Char) LIKE ${paramAnio} AND
CAST(fact_Ventas.idMes As Char) LIKE ${paramMes} AND
CAST(fact_Ventas.idVendedor As Char) LIKE ${paramVendedores} AND
CAST(fact_Ventas.origen As Char) LIKE ${paramOrigen} AND
dim_Productos.marca LIKE ${paramMarca} AND
dim_Clientes.segmentoCliente LIKE ${paramSegmento}
GROUP BY 1
ORDER BY 1
I want to divide the column amount, by a numerical value extracted from a simple parameter. I managed to use filters in the where clause, but I can not make a division in a column.
Try the following query:
SELECT
CONCAT(cuatrimestre,' Cuatrimestre') As cuatrimestre
, ROUND( (SUM(fact_Ventas.cantidad * precioUnitario)/${paramValue}),0) as Importe
FROM fact_Ventas
INNER JOIN dim_Tiempos ON
fact_Ventas.idAnio = dim_Tiempos.idAnio AND
fact_Ventas.idMes = dim_Tiempos.idMes AND
fact_Ventas.idDia = dim_Tiempos.idDia
INNER JOIN dim_Clientes ON dim_Clientes.idCliente = fact_Ventas.idCliente
INNER JOIN dim_Productos ON dim_Productos.idProducto = fact_Ventas.idProducto
WHERE
CAST(fact_Ventas.idAnio As Char) LIKE ${paramAnio} AND
CAST(fact_Ventas.idMes As Char) LIKE ${paramMes} AND
CAST(fact_Ventas.idVendedor As Char) LIKE ${paramVendedores} AND
CAST(fact_Ventas.origen As Char) LIKE ${paramOrigen} AND
dim_Productos.marca LIKE ${paramMarca} AND
dim_Clientes.segmentoCliente LIKE ${paramSegmento}
GROUP BY 1
ORDER BY 1
But it gives an error and the data is not loaded. The syntax of the query was tested in the database and is correct.
Did you set the parameter type to Numeric? It defaults to String which was probably fine for your other parameters.
The problem was that I forgot to assign the parameter to the component, I had associated it with SQL Query but not with the component.

Teradata - Comparing Varchar to decimal

I am very new to Teradata and SQL in general. I need to create a table by combining data from three tables. I was able to successfully join two of them. I am not able to write the joining condition for the third table properly. Here is the code:
select s.cola, s.colb,
t.colc, t.cold,
u.cole, u.colf, u.colg, u.colh, u.coli, u.colj, u.colk, u.coll
from table1 s
inner join table2 t
on s.colb = t.colc
inner join table3 u
on t.cold = cast(u.colm as decimal)
order by 3
where substr(cast(s.cola as varchar(10)),6,2) = 11 and substr(cast(s.cola as varchar(10)),1,4) = 2017 and substr(cast(s.cola as varchar(10)),9,2) between 06 and 10
The error I am getting is:
[Teradata Database] [2620] The format or data contains a bad character.
I think the problem is with the line: on t.cold = cast(u.colm as decimal). The u.colm is of type VARCHAR(50) while t.cold is of type DECIMAL(10, 0). I believe I have casted it properly. Please help.Thanks in advance.
There's some bad data in u.colm.
Depending on your Teradata release you can check it using
WHERE u.colm > '' AND TRYCAST(u.colm as decimal(10,0)) ISNULL
or
WHERE u.colm > '' AND TO_NUMBER(u.colm) IS NULL
You can also use those in the join-condition, e.g.
on t.cold = trycast(u.colm as decimal(10,0))
Don't forget to add the precision of the decimal, as it defaults to (5,0).
Your WHERE_condition is strange, what's the datatype of s.cola?
Seems it's a string with a date yyyy-mm-dd in it. Try
WHERE trycast(s.cola as date) between date '2017-11-06' and date '2017-11-10'
Finally the ORDER BY should be placed after WHERE.

ORA-01861 literal does not match format string on SELECT statement

Good day.
I am executing a query and encountering:
ORA-01861 literal does not match format string error.
I executed this query and IT WORKED.
SELECT * FROM GCACC_OPERATION_DETAIL WHERE id_notice in (75078741)
AND id_analytical_center in (100000002)
AND interface_date = '2013-06-30'
AND generic_client = 'someGenClient'
AND document_class = 'DOCCLA0001'
AND accounting_tag_identifier = 1
AND generated_actual_acc_doc
IN (select id_accounting_document
from gcacc_accounting_document
where document_status = 'DOCSTA0001');
My other query is written below which DID NOT WORK.
SELECT * FROM GCACC_OPERATION_DETAIL WHERE id_notice IN (75078741)
AND id_analytical_center in (100000002)
AND generic_client = 'someGenClient'
AND document_class = 'DOCCLA0001'
AND accounting_tag_identifier = 1
AND interface_date = '2013-06-30'
AND ind_pending_process = 1
AND operation_type
IN (select cod_develop from gcacc_operation_type where ind_operation = 'B');
This is really weird because the error happens in the date part but I am writing the same syntax for the date part. I maybe missing something silly here and fresher eyes are needed. Thanks in advance!
I don't know what the specific problem is, but assuming "interface_date" is a DATE type, it is bad practice to use literals in a query for a date. This makes the assumption that the default NLS_DATE_FORMAT agrees with your date literal. That will come back to bite you. To ensure that your date constraint is portable, change to this:
AND interface_date = to_date('2013-06-30','YYYY-MM-DD')