cancel
Showing results for 
Search instead for 
Did you mean: 

Error when using ODBC function {fn right(,)} and {fn left(,)}

Former Member
8,448

I am using SQL Anywhere Network Server Version 11.0.1.2044 with Interactive SQL Version 11.0.1, build 2044. When I execute the following statement in this ISQL,

select {fn right(StringColumn, 5)} from ATable;

I got the following Error:

Could not execute statement. Syntax error near 'fn right(StringColumn, 5)' on line 1 SQLCODE=-131, ODBC 3 State="42000"

When I execute the same statement in the ISQL Session of the Database Painter of PowerBuilder Version 12.1 Build 6875, I got two different results: 1.When the DelimitIdentifier='Yes' in the SQLCA.DBParm of the database connection string, it executes correctly. 2.When the DelimitIdentifier='No' in the SQLCA.DBParm of the database connection string, it gives me the following error message:

SQLSTATE = 37000 [Sybase][ODBC Driver][SQL Anywhere]Syntx error near 'from' on line 1

Should it work without setting DelimitIdentifier='Yes' for the database connection in PowerBuilder? Is it a bug for PB or SQL Anywhere?

Accepted Solutions (0)

Answers (3)

Answers (3)

jack_schueler
Product and Topic Expert
Product and Topic Expert

Regarding select {{fn POSITION(StringColumn in 'abc')}} from ATable;

The ODBC driver escape syntax parser is looking for uppercase keywords like IN or FROM in scalar function syntax involving functions POSITION and EXTRACT. The string comparison should be done in a case-insensitive manner. This problem will be corrected.

jack_schueler
Product and Topic Expert
Product and Topic Expert

The problem with functions like RIGHT and the Powerbuilder DelimitIdentifier='No' option is that the ODBC driver places quotation marks around functions that are keywords when it rewrites the escape syntax.

So this ...

select {{fn right(DepartmentName,4)}} from Departments;

is rewritten as this ...

select "RIGHT"(DepartmentName,4) from Departments;

The PB DelimitIdentifier='No' option results in the server option "quoted_identifier" being set "off".

Of course, when it is "off", then "RIGHT" is interpreted as the literal value 'RIGHT' and this leads to the syntax error. It's as if you had tried to execute this ...

    select 'RIGHT'(DepartmentName,4) from Departments
VolkerBarth
Contributor
0 Kudos

Just to understand: So this is considered desired behaviour?

I would think that "{fn right(...)}" would clearly indicate that a builtin function is called (instead of an arbitrary identifier which might have to be quoted).

jack_schueler
Product and Topic Expert
Product and Topic Expert
0 Kudos

I think what you are asking is "is it necessary to use the quotation marks?" For example create table foo (col1 char(65) default {{fn char(65)}}) becomes create table foo (col1 char(65) default "CHAR"(65)) but does there exist a case where something like create table foo (col1 char(65) default CHAR(65)) could result in a syntax error because CHAR (or whatever) is interpreted as a keyword rather than a function. I need a SQL syntax expert to answer this.

VolkerBarth
Contributor
0 Kudos

Yes, I guess this is what I'm asking...

As usual, I have thought less carefully: I was just thinking of the "SELECT {fn ..." syntax where one would not expect DDL statements. But given your sample it's more complicated, I agree.

jack_schueler
Product and Topic Expert
Product and Topic Expert
0 Kudos

The problem has been corrected by using square brackets instead of quotation marks.

select [ RIGHT ] (DepartmentName,4) from Departments;

Former Member
0 Kudos

I don't believe iSQL is using ODBC to access the database engine. I'm not certain that ODBC escapes would be handled properly, being as they get "fixed up" by the driver before being sent to the database.

Possibly related google searches: SQL_ATTR_NOSCAN, SQLNativeSql

Not sure I can comment about DelimitIdentifier

VolkerBarth
Contributor
0 Kudos

AFAIK, DBISQL uses JDBC and requires doubled braces around the escape sequences whereas dbisqlc is an ESQL application and does not support ODBC escapes.

Former Member
0 Kudos

Doubled braces does work in DBISQL, though it removed the first non-space character so you have to do like:

*select {{fn right(StringColumn, 5)}} from ATable;

instead of just:

select {{fn right(StringColumn, 5)}} from ATable;

That's a bug, probably in the SQL Anywhere's JDBC driver.

When I test the problem in PowerBuilder with ODBC connection, the following statements cause errors when the DelimitIdentifier is set to 'No' in the SQLCA.DBParm of the database connection string:

select {fn CHAR(40)} from ATable;

select {fn LEFT(StringColumn,5)} from ATable;

select {fn RIGHT(StringColumn,5)} from ATable;

while all other ODBC string functions work just fine, such as:

select {fn RTRIM(StringColumn)} from ATable;

select {fn SUBSTRING(StringColumn,1,1)} from ATable;

select {fn UCASE(StringColumn)} from ATable;

This inconsistency might be a bug in the ODBC driver for SQL Anywhere.

Here is a common problem for both JDBC and ODBC driver of SQL Anywhere:

The following statement works

select {{fn POSITION(StringColumn IN 'abc')}} from ATable;

while this does not work

select {{fn POSITION(StringColumn in 'abc')}} from ATable;

The only difference: "IN" is upper case while "in" is lower case. Should case matter?

VolkerBarth
Contributor
0 Kudos

Given that I would think that those function names that are reserved words in SQL Anywhere are problematic.

Note that I'm in the position to draw any conclusions from that.

jeff_albion
Product and Topic Expert
Product and Topic Expert

No, the case should not matter, but in this specific case it did: http://search.sybase.com/kbx/changerequests?bug_id=684608. This has been fixed in 11.0.1.2678.

jack_schueler
Product and Topic Expert
Product and Topic Expert
0 Kudos

Re: Doubled braces does work in DBISQL, though it removed the first non-space character so you have to do like:

*select {{fn right(StringColumn, 5)}} from ATable;

There was a problem like this back in 2009 but it was corrected. I suspect your software may be out-of-date.