CASE conversion from IIF - sql

I started off with SQL (access)
IIf(Len([CAT]) < 3,
Left([CAT],1) & 0 & Right([CAT],1),
[CAT]) AS CAT1,
[HD0] &
IIf([TABLE].[HD1]<>"00",
" / " & [HD1_ABR],
Null) &
IIf([HD2]<>"00",
" / " & [HD2_NAME],
Null) &
IIf([HD3]<>"000",
" / " & [HD3_NAME],
Null) &
IIf([HD4]<>"00",
" / " & [HD4_NAME]) AS NAME,
and did Oracle (Sql Developer)
Case
When length(cat) < 3
Then SubStr(cat,1,1) || '0' || SubStr(cat,-1,1)
Else cat
End cat1,hd0
Case
When TABLE <>"00"
then " / "
else HD1_ABR,null
When I run query in SQLDev I get error
Error at Command Line:9 Column:4
Error report:
SQL Error: ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"

MS Access syntax is entirely different to Oracle syntax. No square brackets, and different names for the SQL functions. http://docs.oracle.com/cd/E11882_01/server.112/e17118/functions.htm#SQLRF006
Case
When length(cat) < 3
Then SubStr(cat,1,1) || '0' || SubStr(cat,-1,1)
Else cat
End cat1

Related

Object required when using GetList function for concatenation

OBJECTIVE: To concatenate row values
Record in COMP table
player_id cc_number
1 123
2 152
1 254
2 154
3 256
Result to be
player_id cc_number
1 123, 254
2 152, 154
3 256
CODE:
MYSQL = "Select T.player_id, "
MYSQL = MYSQL & GetList("Select cc_number From COMP As T1 Where T1.player_id = " & [T].[player_id], "", ", ") & " AS NewCCNumber"
MYSQL = MYSQL & "From COMP AS T"
MYSQL = MYSQL & "Group By T.player_id"
ERROR:
i used GetList function but it gives me error Object Required.
Need space in front of FROM and GROUP or after NewCCNumber and T. Otherwise text strings run together. Also, the GetList() function should probably be embedded in string
MYSQL = "Select T.player_id, "
MYSQL = MYSQL & "GetList('Select cc_number From COMP As T1 Where T1.player_id = " & [T].[player_id] & "', '', ', ') As NewCCNumber "
MYSQL = MYSQL & "From COMP As T "
MYSQL = MYSQL & "Group By T.player_id"

How to avoid that syntax : (Format(R.Date_VL,'') = Format(M.Date,'') => making macro very slow

I just realized that (Format(R.Date_VL,'yyyy/mm') = Format(M.Date,'yyyy/mm') is why my script is very slow.
Do you know why and if there is a way to avoid that syntax ?
its very very slow when I use format = format !!
Edit : I want to join on months and years not days
SQL = "SELECT M_Net_Flow.ID_Perf, M_Net_Flow.Net_Flow AS XX, laps"
SQL = SQL + " INTO AAA"
SQL = SQL + " FROM M_Net_Flow INNER JOIN R_Dates ON (Format(R_Dates.Date_VL,'mm/aaaa') = Format(M_Net_Flow.Date,'mm/aaaa'))"
SQL = SQL + " GROUP BY M_Net_Flow.ID_Perf, M_Net_Flow.Net_Flow, laps"
SQL = SQL + " HAVING (LAPS < " + deb + ");"
DoCmd.RunSQL SQL
You could also try using DateDiff:
SQL = "SELECT M_Net_Flow.ID_Perf, M_Net_Flow.Net_Flow AS XX, Laps "
SQL = SQL + "INTO AAA "
SQL = SQL + "FROM M_Net_Flow, R_Dates "
SQL = SQL + "WHERE DateDiff("m", R_Dates.Date_VL, M_Net_Flow.Date) = 0 "
SQL = SQL + "GROUP BY M_Net_Flow.ID_Perf, M_Net_Flow.Net_Flow, laps "
SQL = SQL + "HAVING Laps < " + deb + ";"

SQL0206N "AS" is not valid in the context where it is used. SQLSTATE=42703

I have a SSRS report with sql query as below:
="select ar_sum.client_id as Customer_Code,"&
" ar_sum.bill_number as PRO_NO,"&
" ar_sum.company_id as ORG,"&
" tlorder.pick_up_by as Pick_Up_Date,"&
" ar_sum.aging_date as Bill_Date,"&
" ar_sum.aging_date as Invoice_Date,"&
Switch(
Parameters!CompName.Value = "355", " case when tlorder.pick_up_by is null then ar_sum.adj_amt else ar_sum.orig_amt end",
Parameters!CompName.Value = "301" or Parameters!CompName.Value = "354", " case when ((tlorder.pick_up_by is null) and (substr(ar_sum.bill_number,1,1) not in('1','2','3','4','5','6','7','8','9','0'))) then ar_sum.adj_amt else ar_sum.orig_amt end"
)&
" as Original_Amount,"&
" case when tlorder.pick_up_by is null then ar_sum.adj_amt else ar_sum.orig_amt end as Invoice_Amount,"&
" case when tlorder.pick_up_by is null then ar_sum.pay_amt else (ar_sum.pay_amt - ar_sum.adj_amt) end as Amount_paid,"&
" ar_sum.bal_amt as Amount_Open,"&
" ar_sum.currency_code as Currency_code"&
" from TMWIN.ar_sum ar_sum"&
" left join TMWIN.tlorder tlorder on ar_sum.detail_line_id = tlorder.detail_line_id"&
" where ar_sum.company_id = " & Parameters!CompID.Value &
" and (ar_sum.bal_amt > 0.01 or ar_sum.bal_amt < -0.01)"&
Switch(
Parameters!CompName.Value = "355", "",
Parameters!CompName.Value = "301", " and ar_sum.ar_control_acct in ('301-1052-000-00-8','301-1053-000-00-8','301-1053-500-00-0','301-1052-500-00-0')",
Parameters!CompName.Value = "354", " and ar_sum.ar_control_acct not in ('301-1052-000-00-8','301-1053-000-00-8','301-1053-500-00-0','301-1052-500-00-0', '301-1052-000-00-7' ,'301-1053-000-00-7')")
When I run it, I got below error:
An error has occurred during report processing. (rsProcessingAborted)
Query execution failed for dataset 'DataSet1'. (rsErrorExecutingCommand)
ERROR [42S22] [IBM][CLI Driver][DB2/NT64] SQL0206N "AS" is not valid in the context where it is used. SQLSTATE=42703
I have tried to remove the AS word but no luck. Actually I did not change anything in the query and it is working before. And all my other queries have the AS word too.
Please help me I could not find where is the error.
Thanks for the help!
EDITS:
I am not really good at SSRS report. I found above query from below:

HQL unexpected token "(" subquery select

I have this query. Translate it from my sql query to hql. I have this error
"unexpected token: ( near line 2, column"
String query = "SELECT MAX(number)\n" +
" FROM (SELECT number FROM EmployeeTripCard \n" +
" WHERE EXTRACT(YEAR FROM issueDate) = '2015'\n" +
" UNION ALL\n" +
" SELECT trip_card_number FROM PostgraduateTripCard\n" +
" WHERE EXTRACT(YEAR FROM issueDate) = '2015'\n" +
" UNION ALL\n" +
" SELECT trip_card_number FROM StudentTripCard \n" +
" WHERE EXTRACT(YEAR FROM issueDate) = '2015'\n" +
" )";
Integer result = (Integer) getSessionFactory().getCurrentSession().createQuery(query).uniqueResult();
I don't underastand what'w wrong
My bad Vytsalo, didnt look at the databse tag.
HQL subqueries is not supported on from clauses
Note that HQL subqueries can occur only in the select or where clauses:Hibernate Query Language

Syntax error (missing operator) in query expression

Dim sql As String = "SELECT DISTINCT LED.IDX As IDX, CORPCODE, UNITTYPECODE, UPPERFORMATIONCODE, FORMATIONCODE, LED.UNITCODE As UNITCODE,LED.STORECODE As STORECODE, LED.SECTIONNO As SECTIONNO, LED.PARTNO As PARTNO, "
sql &= " LED.BATCHNO As BATCHNO, UNITITEMCATEGORYCODE, LEDGERDATE, ENTITLEMENT, HOLDING, SURPLUS, SHORTAGE, COSTHOLDING, COSTENTITLEMENT, INITIALSTOCK, EMPLOYQTY, FIGHTQTY, ITEMAGE1, ITEMAGE2, ITEMAGE3,"
sql &= " ITEMAGE4, LED.BAH_BEREK, LED.JENIS_BEREK, JPTD_NO, LASTUPDATE, LED.ITEMTYPECODE As ITEMTYPECODE, LED.TOOLTYPECODE As TOOLTYPECODE, LED.PAKAIBHGCODE, LED.PAKAICODE, ITEMNIDX, LED.CREATEID As CREATEID, "
sql &= " LED.UPDATEID As UPDATEID, LED.CREATEDATE As CREATEDATE, LED.UPDATEDATE As UPDATEDATE, "
sql &= " USERID, RANKCODE, SERVICENO, USERNAME, DESIGNATIONCODE, PASSWORD, "
sql &= " 'OPER' AS GROUPCODE,5 AS ACESSID, 0 AS STATUS "
sql &= " FROM (LEDGER AS LED) "
sql &= " LEFT JOIN NUSER On NUSER.USERID = LED.UPDATEID WHERE (1=1) "
If condation <> "" Then
sql += condation
End If
sql &= " UNION SELECT DISTINCT LED.IDX As IDX, CORPCODE, UNITTYPECODE, UPPERFORMATIONCODE, FORMATIONCODE, LED.UNITCODE As UNITCODE, LED.STORECODE As STORECODE, LED.SECTIONNO As SECTIONNO, LED.PARTNO As PARTNO, "
sql &= " LED.BATCHNO As BATCHNO, UNITITEMCATEGORYCODE, LEDGERDATE, ENTITLEMENT, HOLDING, SURPLUS, SHORTAGE, COSTHOLDING, COSTENTITLEMENT, INITIALSTOCK, EMPLOYQTY, FIGHTQTY, ITEMAGE1, ITEMAGE2, ITEMAGE3,"
sql &= " ITEMAGE4, LED.BAH_BEREK, LED.JENIS_BEREK, JPTD_NO, LASTUPDATE, LED.ITEMTYPECODE As ITEMTYPECODE, LED.TOOLTYPECODE As TOOLTYPECODE, LED.PAKAIBHGCODE, LED.PAKAICODE, ITEMNIDX, LED.CREATEID As CREATEID, "
sql &= " LED.UPDATEID As UPDATEID, LED.CREATEDATE As CREATEDATE, LED.UPDATEDATE As UPDATEDATE,"
sql &= " ('' AS USERID), ('' AS RANKCODE),('' AS SERVICENO),('' AS USERNAME),('' AS DESIGNATIONCODE), ('' AS PASSWORD), 'OPER',5 , STATUS "
sql &= " FROM LEDGER_HISTORY LED WHERE (1=1) "
error is Syntax error (missing operator) in query expression '('' AS USERID)'.
Can anyone help me to solve this
The error is correct. The problem is the parentheses.
This syntax is not appropriate for SQL:
SELECT ('' AS USERID)
However, this is:
SELECT ('') AS USERID
Or just:
SELECT '' AS USERID
You have the same problem with FROM (LEDGER AS LED).