SQL compilation error: syntax error line 1 at position 8 unexpected '-' - snowflake-cloud-data-platform

I am getting the SQL compilation error: syntax error line 1 at position 8 unexpected '-'.
sql query is :
INSERT INTO table_name(col_names) values();
position 8 is I of "INTO"
I am stuck with this tried searching the character from notepad++, but couldn't find.

You example of "it breaks" need to be reproducible. So you need to paste you not-working code (which is clear you don't want to) or toy sql that has the problem. It should look like:
create table toy_example(col_names text);
-- this works yippie!
insert into toy_example(col_names) values ('this is a value');
-- but this
insert into toy_example(col_names) values ();
/*
Syntax error: unexpected ')'. (line 22)
*/
because as it stands:
I have some SQL it gives me a error
002020 (21S01): SQL compilation error:
Insert value list does not match column list expecting 3 but got 1
my code looks like:
SELECT 1;
cannot really be worked with.

From your toy example your list of columns doesn't match the number of columns in your values clause. You're trying to insert 0 values into some number of columns.
You need something like
INSERT INTO table_name(col_1, col_2, col_3) values(1, 'hello', 'world');
I'm getting a similar, less than helpful error when I try something similar to your code. I think the parser is simply not able to comprehend a values clause with no input, so it's not even managing to figure out where the issue is, and it's just giving a generic "there's something wrong with your insert" error.

Thanks, #David Garrison and #Simeon Pilgrim for the time you put into answering my question.
The cause of my error was: I was using a Postgres data type: INTERVAL in my subquery with hyphen in it.
select CAST((DATE_TRUNC('MONTH', CURRENT_DATE) + INTERVAL '1 MONTH - 1 DAY') AS -
DATE
so , snowflake was not compatible with the hyphen above, but it was not showing the error in the correct line number while compiling the SQL instead it was showing the error in the first line INSERT INTO as explained in my question.(I think its a bug in snowflake)
So to make it compatible in snowflake , I used :
select CAST(LAST_DAY((DATE_TRUNC('MONTH', CURRENT_DATE))) AS DATE);
INTERVAL will work in snowflake but without hyphen, that is : INTERVAL '30 DAYS',
but this is not fair with the month of February, so I used LAST_DAY function.
and the query went fine :D

Related

SQL compilation error: syntax error line 2 at position 6 unexpected ' R'

I am using RIGHT function in where clause but it is throwing error unexpected 'R'. Could you please help on below
select * from table where RIGHT(DATE, 10) ='2020-07-26'
What you posted is valid SQL. Is there more to the query that you have omitted?
with tbl as (select $1 DATE from values ('2021-01-01'))
select * from tbl where RIGHT(DATE, 10) ='2021-01-01';
DATE
2021-01-01
Issue resolved as there is special character in between where and Right
I'm able to produce the same error when I put an invisible character between WHERE and RIGHT keywords. So there must be an invisible character causing this error.
As a workaround, try to re-write the query from scratch, or delete all spaces between keywords and put spaces again.

TO_NUMBER function inside AVG doesn't work

I'm in trouble with TO_NUMBER ORACLE function.
The query
SELECT TO_NUMBER(varchar2_column)
FROM TABLE#ANOTHER_DB;
works, but if I put TO_NUMBER inside AVG, ORACLE returns the following error:
ORA-01722: invalid number ORA-02063: preceding line from ANOTHER_DB Position: 0
The query is the following:
SELECT AVG(TO_NUMBER(varchar2_column))
FROM TABLE#ANOTHER_DB;
Could someone help me? Thanks in advance
You can use to following query to get a working format mask for your data:
SELECT AVG(TO_NUMBER(varchar2_column, 'FM'||REGEXP_REPLACE(varchar2_column,'\d','0')))
FROM TABLE#ANOTHER_DB;
That will produce a formatmask replacing all numerical characters with 0, which should work with all your data.

Error converting data type when using multiple WHERE statements

I have a SQL query that works fine when the WHERE statement contains only one statement, but not when it contains both statements at the same time. When I use both WHERE statements, I get "Error converting data type to varchar to numeric." (The error is occurring in the second SELECT statement, not in the CTE).
This code is what I want:
;WITH cte_1 as(
SELECT EVENT_ID
,CLIENT_ID
,convert(varchar(10), E.STARTDATE, 101) as R106_DATE
FROM SCEVENT
WHERE STARTDATE>='06/01/2016' AND STARTDATE<='06/30/2016'
and SVC_ID=106
)
SELECT R.*
,convert(varchar(10), S.BEG_DATE, 101) as SVC_DATE
,S.UNIT_ID
FROM cte_1 R
JOIN CDCLSVC S
on R.CLIENT_ID=S.CLIENT_ID
WHERE R106_DATE<=BEG_DATE
and UNIT_ID=251
If I include one or the other WHERE statements, it works fine. But if I have both of those statements, I get an error.
(The desired result is a list of people who had a service at unit 251 after they had a service code (SVC_ID) 106.)
Thoughts?
I changed the code so that it's not converting the date until after the match (excellent point, #JonathanShields) but I kept getting the error message.
I ended up changing my date range to a longer period of time, and suddenly it worked. I guess there were no records in the given time frame that met both criteria, but instead of returning a result of 0 records it gave me the error message.
Thanks for everyone's help.

Pentaho error data truncate when insert into SQL Server

I'm developing Pentaho job to get data from BigQuery and insert into SQL Server. The job is quite simple as you can see below but during insert to a SQL Server table process in thrown 'Data truncation' error. Then I checked max length for this column. It is just 64 while in database it is nvarchar(500). Moreover that I want to know how is look like then for error records I log into text file. You can see it below. I've spent for 3 days with this problem but still not get an answer yet. Please do guide me.
What I have done so far
String cut step to sub string
String Operation step to trim
put left function in SELECT statement
put REGEXP_REPLACE(uuid, ' ', '') which remove spaces in SELECT statement.
All I have done getting the same error.
Pentaho job
Error records in text file
I have been solved this problem. It is my stupid mistake. I just recreate table and put more number for length of that column.
My case
post_name nvarchar(50) -> nvarchar(150)

PL/SQL: SQL Statement ignored?

Hi everyone getting this error message when trying to create a trigger and its got me a little stumped.
Here is my trigger code.
CREATE OR REPLACE TRIGGER CUSTOMER_AD
AFTER DELETE ON CUSTOMER
REFERENCING OLD AS OLD
FOR EACH ROW
DECLARE
nPlaced_order_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO nPlaced_order_count
FROM PLACED_ORDERS p
WHERE p.FK1_CUSTOMER_ID = OLD.CUSTOMER_ID;
IF nPlaced_order_count > 0 THEN
INSERT into previous_customer
(customer_id,
first_name,
last_name,
address,
AUDIT_USER,
AUDIT_DATE)
VALUES
(:old.customer_id,
:old.first_name,
:old.last_name,
:old.address,
UPPER(v('APP_USER')),
SYSDATE);
END IF;
END CUSTOMER_AD;
And the error I'm getting 'Error at line 4: PL/SQL: SQL Statement ignored 0.10 seconds'
Anyone any guesses why?
thanks for the help
The error shown is only the highest level. Depending where you're running it you should be able to see the stack trace. The client will determine exactly how to do that; SQL*Plus or SQL Developer would show you more than this anyway, but I don't really know about other clients. If you can't see the details in your client then you can query for them with:
select * from user_errors where name = 'CUSTOMER_AD' and type = 'TRIGGER'
Assuming the tables all exist, it's probably this line:
WHERE p.FK1_CUSTOMER_ID = OLD.CUSTOMER_ID;
which should be:
WHERE p.FK1_CUSTOMER_ID = :OLD.CUSTOMER_ID;
When referencing the old (or new) value from the table, the name as specified in the referencing clause has be preceded by a colon, so :OLD in this case. As you're doing already in the insert ... values() clause.
(From comments, my assumption turned out to be wrong - as well as the missing colon problem, the table name is really placed_order, without an s).
Seems like you copied code from both answers to your previous question without really understanding what they were doing. You might want to look at the trigger design guidelines (particularly the one about not dupicating database functionality) and the syntax for create trigger which introduces the referencing clause.

Resources