mismatched input ';' expecting <EOF>(line 1, pos 90) - apache-spark-sql

I am trying to fetch multiple rows in zeppelin using spark SQL.
Here's my SQL statement:
select id, name from target where updated_at = "val1", "val2","val3"
This is the error message I'm getting:
mismatched input ';' expecting < EOF >(line 1, pos 90)

Not sure what your exact requirement is but your match condition doesn't conform to SQL syntax standards. Below statement will work if your requirement does match this:
select id, name from target where updated_at in ('val1', 'val2','val3')

In case someone gets this error in the selectExpr function from Spark like me: the correct usage of selectExpr is not a string which contains a comma separated list of column names, but a list of column names:
spark_df.selectExpr("id, name") # ouch, wrong
> mismatched input ';' expecting <EOF>(line 1, pos xy)
spark_df.selectExpr("id", "name") # right!
> DataFrame[..]
spark_df.selectExpr(*["id", "name"]) # correct too!
> DataFrame[..]

Related

Oracle SQL export to csv show error: ORA-01722: invalid number

table_a table_b
-------------- ----------
product,amount,pcode rounding,pcode
-------------- ----------
apple,100.00,001 0.02,001
orange,150.02,001 -0.02,001
output
----------
apple,100.02
orange,150.00
select a.product||','||sum(a.amount)+b.rounding from table_a a,table_b b
where a.pcode=b.pcode
group by a.product,b.rounding
Hi , I trying to spooling this query to csv format, but showing error:
ORA-01722 invalid number.
when I remove +b.rounding then no issued at all, may I know how to use sum(a.amount)+b.rounding in my query ?
your kinds assist is much appreciated.
The error has nothing to do with "export to csv" or with aggregation. You can reproduce it very simply, like this:
select 'a' || 1 + 3 as result from dual;
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The reason is very simple: concatenation (the || operator) has the same precedence as addition (+). Since in your formula concatenation comes first, it is performed first. In my example, the number 1 is converted (implicitly) to the string '1' and concatenated to 'a', resulting in 'a1'. Then this must be added to 3. To do that, Oracle tries to convert 'a1' (implicitly) to number, and obviously that fails, with the same error you observed.
If you expected the result to be the string 'a4' (in my example), the solution is trivial: use parentheses.
select 'a' || (1 + 3) as result from dual;
RESULT
------
a4
The same applies to your situation.
Note that the following works - showing that || and + have the same precedence; concatenation is not stronger than addition, they are simply performed in order, left to right.
select 1 + 3 || 'a' as result from dual;
RESULT
------
4a
Here we don't need parentheses, because 1 + 3 = 4, and then that is converted to the string '4' and then 'a' is concatenated to it.

Databricks "extraneous input expecting EOF" error

This took me a while to figure out so I thought I'd share to save someone else the pain. This is obviously dummy code to illustrate the issue.
This doesn't work:
%sql
Select 'A' as A -- I won't need this
, '1' as B;
Select 'Magic';
Error message:
Error in SQL statement: ParseException:
extraneous input 'Select' expecting {<EOF>, ';'}(line 4, pos 0)
== SQL ==
Select 'A' as A -- I won't need this
, '1' as B;
Select 'Magic';
^^^
This does work:
%sql
Select 'A' as A -- I wont need this
, '1' as B;
Select 'Magic';
And the difference in the single-quote in the comment on line 3.
When you use single-quote in the comment, you need to pass the comment in double quotes.
Example: -- I won't need this it should be -- "I won't need this"

Unable to write case statement in Spark SQL

I have written below query in Spark SQL using spark-shell and I am getting below error message
spark.sql(""" select case when Treatment == 'Yes' then 1 else 0 end AS 'All-Yes' from person """)
Error message-
org.apache.spark.sql.catalyst.parser.ParseException:
mismatched input ''All-Yes'' expecting <EOF>(line 1, pos 58).
Can someone please help me in this
The alias should be enclosed with backquotes
select case when Treatment == 'Yes' then 1 else 0 end AS `All-Yes` from person
though in general you shouldn't use non-standard, an incompatible names.

Invalid number error in where clause

I am executing a query in Oracle database. The column and everything is correct but I am getting an Invalid Number error for below query:
select COUNT(*) AS "COUNT" from NE.STRUCT B
where B.STRUCT_TYPE in ('IDC')
and NET_ENTITY_ID is not null
and length(NET_ENTITY_ID) = 18
AND regexp_like(SUBSTR(NET_ENTITY_ID,15,1),'[^A-Z]')
and TO_NUMBER(SUBSTR(NET_ENTITY_ID,(length(NET_ENTITY_ID) -3), 4)) < 6000;
NET_ENTITY_ID field has only one data ABCDEFGHXXXXNB0001.
But not necessary that it will always be having one data. This is just for resolving the issue I am considering only this.
Error Message:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The problem is that Oracle -- and any other database -- does not guarantee the order of evaluation of clauses in a WHERE. You can get around this using CASE:
where B.STRUCT_TYPE in ('IDC') and
NET_ENTITY_ID is not null and
length(NET_ENTITY_ID) = 18 AND
regexp_like(SUBSTR(NET_ENTITY_ID, 15, 1), '[^A-Z]') and
(CASE WHEN regexp_like(SUBSTR(NET_ENTITY_ID,(length(NET_ENTITY_ID) -3), 4), '^[0-9]{4}$'
THEN TO_NUMBER(SUBSTR(NET_ENTITY_ID,(length(NET_ENTITY_ID) -3), 4))
END) < 6000;
You need to ensure that the last 4 chars are numeric before using TO_NUMBER on them.
This will do it:
select COUNT(*) AS "COUNT" from
( SELECT * FROM NE.STRUCT B
where B.STRUCT_TYPE in ('IDC')
and NET_ENTITY_ID is not null
and length(NET_ENTITY_ID) = 18
AND regexp_like(SUBSTR(NET_ENTITY_ID,15,1),'[^A-Z]')
AND regexp_like(SUBSTR(NET_ENTITY_ID,-4),'[0-9]')
)
where TO_NUMBER(SUBSTR(NET_ENTITY_ID,-4)) < 6000;
NB I simplified your SUBSTR for obtaining the last 4 characters.

Apply IsNull function on a date column (SQL Server)

I am trying to apply the IsNull function to a column of a datatype date.
What am trying to do is, to replace the word NULL with an empty string (actual null value) when retrieving the data
The code am using is;
Select
ISNULL("UPDATE_DT", '')
from
MyTable
I have also tried
Select
ISNULL("UPDATE_DT", 0)
from
MyTable
The error am getting is
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 4, column: 1: Incorrect syntax near 'from'.
If you want to see the column as an empty string rather than "NULL", you need to convert the output to a string. To control the format, use convert():
select coalesce(convert(varchar(10), update_dt, 121), '')
. . .
Then use coalesce() (or if you must, isnull()) to replace the NULL value with another string.