teradata - invalid select expression list - sql

Trying to make a select query on teradata, but i get this message error:
syntax error: invalid select expression list
I can't fix it. How can I formulate the query appropriately?
SELECT BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,BILS01_PROG_REC,
CASE WHEN BILS01_DATA_PRES_ISTSOSP<TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN BILS01_DATA_PRES_ISTSOSP
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP>0 THEN DATA_CONTROVERSIA2
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN TO_DATE('9999-12-31','YYYY-MM-DD')
WHEN BILS01_DATA_PRES_ISTSOSP<
(
CASE WHEN
(
CASE WHEN BILS01_DATA_SPED<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_SPED
WHEN BILS01_DATA_SPED=to_date('9999-12-31','YYYY-MM-DD') OR BILS01_DATA_RIC<to_date('9999-12-31','YYYY-MM-DD')THEN BILS01_DATA_RIC
WHEN BILS01_DATA_ACQ<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_ACQ ELSE BILS01_DATA_PROT END AS DATA_CONTROVERSIA
)
<TO_DATE('1972-01-01','YYYY-MM-DD') THEN TO_DATE('1972-01-01','YYYY-MM-DD') ELSE DATA_CONTROVERSIA END AS DATA_CONTROVERSIA2
)
THEN BILS01_DATA_PRES_ISTSOSP ELSE DATA_CONTROVERSIA2 END AS A) AS OUT_DATA_RICH_SOSP
FROM zucow.BILS01
--GROUP BY BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,
-- BILS01_PROG_REC, OUT_DATA_RICH_SOSP

Related

DB2 Error executing sql statement: [SQL0104] Token ) was not valid

I am getting the following error from DB2, using custom driver: com.ibm.as400.access.AS400JDBCDriver
Error executing sql statement: [SQL0104] Token ) was not valid. Valid tokens: , FROM INTO.; Caused by: [SQL0104] Token ) was not valid. Valid tokens: , FROM INTO.
Narrowed it down to this bit which works in SQL server, we are a bit stumped as syntax appears fine.
SELECT CASE
WHEN Abs(ho.ohmisc) > 0
THEN
(
SELECT LEFT(s, Translate('%[^0-9]%', s) - 1)
FROM (
SELECT Substr(
ohunpo,
Translate('%[0-9]%', ohunpo),
Len(ohunpo)
) AS s
) t
)
ELSE odrord
END ODRORD
FROM qs36f.hoehead HO
QS36F.hoehead HO
You are missing a FROM
SELECT CASE
WHEN Abs(ho.ohmisc) > 0
THEN
(
SELECT LEFT(s, Translate('%[^0-9]%', s) - 1)
FROM (
SELECT Substr(ohunpo,
Translate('%[0-9]%', ohunpo),
Len(ohunpo)
) AS s
-- **You need a FROM here**
) t
)
ELSE odrord
END ODRORD
FROM qs36f.hoehead HO
QS36F.hoehead HO
But that's not your only issue...
This is not valid either, are you trying to join the table to itself?
FROM qs36f.hoehead HO
QS36F.hoehead HO
Your embedded selects likely aren't right either....but without knowing your table structure and seeing some example source data and what you're wanting as output, I can't say for sure.

GETTING ERROR-- ORA-00936:MISSING EXPRESSION for below query please help on this

SELECT CASE (SELECT Count(1)
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(
SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988')
)
WHEN 0 THEN
(
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27' )
WHEN 1 THEN
(
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
ELSE NULL
END
FROM dual;
You need to recreate your query and make sure to follow the flow of the clauses properly, please check the next two links to get a better understanding :
[ORA-00936: missing expression tips]
How do I address this ORA-00936 error?
Answer: The Oracle oerr utility notes this about the ORA-00936 error:
ORA-00936 missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
The ORA-00936 happens most frequently:
1 - When you forget list of the column names in your SELECT statement.
2. When you omit the FROM clause of the SQL statement.
ora-00936-missing-expression
I hope this can help you.
You cannot use a simple select query like this. You have to use a PL/SQL block like below -
DECLARE NUM_CNT NUMBER := 0;
BEGIN
SELECT Count(1)
INTO NUM_CNT
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988');
IF NUM_CNT = 0 THEN
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27';
ELSIF NUM_CNT = 1 THEN
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
END IF;
END;

Denodo - Unable to case DATE>= addday(cast(now() as date),-365)

I've encountered an issue when trying to obtain the following output:
"If x_date >= now-365 then 1 else 0"
My select statement reads:
SELECT
id,
x_date,
CASE x_date
WHEN x_date >= addday(cast(now() as date),-365) then 1
else 0
end as output
I'm receiving an error message that reads:
"SQL Error [30100] [HY000]: CASE argument case((xdate,ge,[addday(trunc(cast('date', now(), 'DATE')) '-365')], utc_il8n), 'true', 'false') is not compatible with the rest of the values.
Has anyone else performed a similar operation with dates in a CASE statement? The Addday works fine and returns 2017-01-05.
Issue with the CASE syntax. I think reading this and other sources may have cause the confusion: https://www.techonthenet.com/sql_server/functions/case.php
Should read:
SELECT
id,
x_date,
CASE WHEN x_date >= addday(cast(now() as date),-365) then 1
else 0
end as output

Case select another column AS400 query

I have simple query which is not working:
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN BATCH_0002.GLIVD ELSE BATCH_0002.date end
as tarih FROM BATCH_0002
Error is:
ERROR: [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0581 - The
results in a CASE expression are not compatible. Error Code:
-581
but when I change column name to something different than GLIVD, it is working.
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN BATCH_0002.GLEXR ELSE BATCH_0002.date end
as tarih FROM BATCH_0002
Is there something wrong with my case statement?
In Case statement result datatype should be same. Here Using int and date as result in case that's why error showing for incompatible data
you should cast one datatype to another
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR, case when BATCH_0002.GLIVD >'0' THEN cast(BATCH_0002.GLIVD as char) ELSE cast(BATCH_0002.date as char) end as tarih FROM BATCH_0002
thank you for all answers regarding data types. worked on this information and now solved, this works very well:
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN
DATE(CHAR(1900000 + BATCH_0002.GLIVD))
ELSE BATCH_0002.date end as tarih FROM BATCH_0002

SQL Server : query if table exist

I have the following query:
SELECT
COUNT(*) over () as countNum,
[F1] AS STANDARDandOBJ,
[F2] AS CLUSTER,
[F3] AS OBJECTIVE,
[F4] AS EXTRA0
IF COL_LENGTH([tmpExcelDB].[dbo].['Blahsing$'], [F5]) IS NOT NULL
BEGIN
print 'exists'
END
ELSE
BEGIN
print 'Nope'
END,
CONCAT([F1], [F2]) AS combined
FROM
[tmpExcelDB].[dbo].['Blahsing$']
WHERE
LOWER(F3) NOT LIKE 'course tools-%'
But it seems that I have an error of:
Incorrect syntax near ','.
Which is pointing to row:
,CONCAT([F1], [F2]) AS combined
How does this need to be formatted in order to work?
You can't use IF inside a SELECT, you need a CASE expression. Also, it doesn't make sense to use PRINT inside a column:
SELECT
COUNT(*) over () as countNum
,[F1] AS STANDARDandOBJ
,[F2] AS CLUSTER
,[F3] AS OBJECTIVE
,[F4] AS EXTRA0
,CASE
WHEN COL_LENGTH('[tmpExcelDB].[dbo].[''Blahsing$'']', '[F5]') IS NOT NULL
THEN 'exists'
ELSE 'Nope'
END
,CONCAT([F1], [F2]) AS combined
FROM [tmpExcelDB].[dbo].['Blahsing$']
WHERE
LOWER(F3) NOT LIKE 'course tools-%';
SELECT
COUNT(*) over () as countNum
,[F1] AS STANDARDandOBJ
,[F2] AS CLUSTER
,[F3] AS OBJECTIVE
,[F4] AS EXTRA0 ,
( CASE When COL_LENGTH([tmpExcelDB].[dbo].['Blahsing$'], [F5]) IS NOT NULL
THEN
'exists'
ELSE
'Nope'
END),
CONCAT([F1], [F2]) AS combined
FROM [tmpExcelDB].[dbo].['0812 Orientation to Nursing$']
WHERE LOWER(F3)NOT LIKE 'course tools-%'
Couple things:
IF...ELSE is not supported in a select list, use CASE instead
PRINT is not supported in a select list, just the literal string value is needed
See the CASE Documentation for a relevant example.