Converting MS Access IIF to SQL Server CASE statement - sql

I'm trying to convert a MS Access application to SQL Server. I'm getting an error
incorrect syntax near 'as'
when trying to convert this:
RegSales:
Sum(LOTOSMIS_ACC_TYPE.sing_ind*LOTOSMIS_RET_DAILY.grs_amn*IIf(gm_cd In (1105,2123,2124,2150,2152,2191,2192,5143,5145,5146,5245,5253),Switch(LOTOSMIS_RET_DAILY.gm_var=1,0,True,1),0))
To this
(SELECT CASE WHEN P.GM_VAR = 1 THEN 0 END)) AS RegSales,
I am getting error RegSales:
Sum(LOTOSMIS_ACC_TYPE.sing_ind*LOTOSMIS_RET_DAILY.grs_amn*IIf(gm_cd In (1105,2123,2124,2150,2152,2191,2192,5143,5145,5146,5245,5253),Switch(LOTOSMIS_RET_DAILY.gm_var=1,0,True,1),0))
Getting syntax errors
Incorrect syntax near the word 'as'
or
Incorrect syntax near ')'
What am I doing wrong?

If I follow correctly, the logic is:
sum(case when gm_cd not in (1105, 2123, 2124, 2150, 2152, 2191, 2192, 5143, 5145, 5146, 5245, 5253)
then LOTOSMIS_ACC_TYPE.sing_ind * LOTOSMIS_RET_DAILY.grs_amn
else 0
end)

Related

Problematic syntax error near ")" at line 14: SQL statement - SAP Hana

I am sure that you are used to this question but nonetheless I am having trouble with my SQL script. It is very frustrating.
When I execute the SQL against the SAP Hana database, I continuously receive this error:
Errors occurred while executing the SQL statement: SAP DBTech JDBC: [257]: sql syntax error: incorrect syntax near ")": line 14 col 35
Let me present the SQL concerned here:
SELECT
BKPF.BKTXT, BKPF.MONAT, BSEG.BELNR, BSEG.BUKRS, BSEG.DMBTR, BSEG.GJAHR, BSEG.MANDT, BSEG.PRCTR, BSEG.SAKNR, CEPCT.KTEXT, CEPCT.LTEXT, SKAT.MCOD1, SKAT.TXT20, SKAT.TXT50
FROM
SAPPR1.BSEG
INNER JOIN SAPPR1.BKPF ON BSEG.GJAHR = BKPF.GJAHR
INNER JOIN SAPPR1.CEPCT ON BSEG.PRCTR = CEPCT.PRCTR
INNER JOIN (SELECT
SAKNR, TXT20, TXT50, MCOD1
FROM
SAPPR1.SKAT
WHERE
SPRAS not LIKE '%[a-z0-9 .]%' ) AS SKAT_SUB ON BSEG.SAKNR = SKAT_SUB.SAKNR
WHERE
BKPF.MONAT = (SELECT Month('?')-1)
AND (BSEG.GJAHR = (SELECT Year('?')-1 HAVING Month('?')-1 < 4) OR BSEG.GJAHR = (SELECT Year('?') HAVING Month('?')-1 > 3))
AND BSEG.MANDT = ?
;
Unlike other DBMS HANA does not support selecting records out of nothing.
A SELECT without a FROM is not valid.
In order to create a single-row set one can use the DUMMY table.
SELECT current_date FROM DUMMY;
creates the single-row set with the result of the function expression.
Having said that, for comparisons a set is not required. Instead the function can be directly put into the expression:
WHERE
BKPF.MONAT = (MONTH(?)-1)
Also note that for string typed bind variables no quotation marks are required or allowed.

Syntax error when trying to store an array into sql table

Im trying to store an array like
[0102,0103,0103]
into a sql table.
Im using this query:
INSERT INTO public."OrderDetail" ("PaidsProducts")
VALUES ('{0102,0102,0103}')
WHERE "ID_Order" = '010'
but im getting this error:
ERROR: syntax error at or near "WHERE"
LINE 3: WHERE "ID_Order" = '010'
^
SQL state: 42601
Character: 81
Any idea?
Thanks all

SQL Error [156] [S0001]: Incorrect syntax near the keyword 'WHERE'

I have a SQL question. The following piece of SQL code gives a "SQL Error [156] [S0001]: Incorrect syntax near the keyword 'WHERE'." Why does this error occure?
INSERT INTO [Layer](ComponentNumber)
OUTPUT inserted.ComponentNumber
VALUES (:component_number)
WHERE LayerID = :layer_id
I appreciate your answer!
Presumably, you want an update:
UPDATE [Layer]
SET ComponentNumber = :component_number
OUTPUT inserted.ComponentNumber
WHERE LayerID = :layer_id;
It is unclear why you would use an OUTPUT clause for this. You are passing in the value for ComponentNumber.

ERROR: syntax error at or near "group" in SQL query

Hello guys I am running this query to get some data from database table.
select pickup, supplier, sum(price)
from myrent_prices_to
where (pickup BETWEEN '2018-01-01' and '2018-07-31')
and location SIMILAR TO '(%)'
and vehicleclass SIMILAR TO '(%)'
and supplier SIMILAR TO '(%)'
group by pickup, supplier
But it is giving me error below:
org.eclipse.birt.report.engine.api.EngineException: Error happened
while running the report. at
org.eclipse.birt.report.engine.api.impl.DatasetPreviewTask.doRun(DatasetPreviewTask.java:135)
at
org.eclipse.birt.report.engine.api.impl.DatasetPreviewTask.runDataset(DatasetPreviewTask.java:97)
at
org.eclipse.birt.report.engine.api.impl.DatasetPreviewTask.execute(DatasetPreviewTask.java:49)
at
org.eclipse.birt.report.designer.data.ui.dataset.DataSetPreviewer.preview(DataSetPreviewer.java:69)
at
org.eclipse.birt.report.designer.data.ui.dataset.ResultSetPreviewPage$5.run(ResultSetPreviewPage.java:336)
at
org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:121)
Caused by: org.eclipse.birt.data.engine.odaconsumer.OdaDataException:
Cannot get the result set metadata.
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object. SQL error #1:ERROR: syntax error
at or near "group" Position: 236 ;
org.postgresql.util.PSQLException: ERROR: syntax error at or near "group" Position: 236 at

Syntax Error In Query with selectRaw and time diff

please advice how to correct this query
$no_of_hours = DB::Table('shifts')
->where('time_sheet_id','=', $timesheet_id->id)
->selectRaw("SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' )))")
->get();
return $no_of_hours;
im getting following error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' ))) from `shifts`' at line 1 (SQL: select SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' ))) from `shifts` where `time_sheet_id` = 35)
You have a sintax error probably because you don't have to write the SELECT keyword in the selectRaw function ( the keyword is added implicity by the query builder in this case ):
->selectRaw("time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' )))")