AS400 - Token "!" not valid - sql

I'm running a SQL query using RUNSQL into a CL program. This query is a basic SELECT statement and uses the exclamation mark to concatenate strings.
For years until yesterday, it worked fine. Now, out of nowhere, I've got a SQL0104 message displaying Token '!' not valid every time I run the program.
If I run the query manually using STRSQL, it works.
Did this occur to someone ?
Best regards.

DB2's operator for string concatenation is actually the double pipe ||.
The documentation says:
Use the concatenation operator (||) to join two values of an expression into a single string. In some non-English, single-byte character sets, the || can display as !! (exclamation marks) or other special characters.
So your issue may be caused by a change in the character set of your client. Just use the standard operator, and your code will work regardless.

Related

Snowflake SQL - Invalid escape sequence when using Regexp_Like

I have been migrating something from a netezza based SQL script into Snowflake, and part of one of my join clauses has the following lines:
AND regexp_like(hist.Description, p.RegexPattern, 'i')
This runs fine on Netezza, however when running within Snowflake I get the following error after about 2 minutes of run time:
100048 (2201B): Invalid regular expression: '^Renewal\b.*\bDraft\b.*\bPending\b\h+\bNon-Renewal\b.*', invalid escape sequence: \h
Has anyone ever encountered this error? A bit new to regexp_like function so it may be something simple, but haven't been able to find anything regarding a fix online. Not sure what the error is really telling me?
\h is a Perl 5 escape sequence to match horizontal whitespace per Perl Regular Expression Classes. Snowflake regular expressions don't appear to support \h. The closest alternative would be to use \s. Note that \s will match a few characters that \h will not, so you may want to verify whether you have any of those characters before making the substitution. See the Perl reference for details.

SQLSyntaxErrorException Using LTRIM to trim character 'x' in query

I using TRIM function to trim some characters in query, I using hibernate following is my query.
from ABean s where s.cId in (select ca.id from CBean ca where LTRIM(ca.refNumber,'0') = LTRIM('$ref$','0') and ca.valid = 0)
$ref$ is replace with actual value in query.
I am seeing a different behaviour when I am running with DB2 and When I am running with Mockito test (Using In memory DB).
With DB2 this query is working fine but with Mockito in memory db I am getting java.sql.SQLSyntaxErrorException, Error is something like this.
Syntax error: Encountered "," at line 1, column {column_number_in_actual_query}.
I am not able to make it working with in memory db, Is there anything wrong I am doing?
Thanks.
in IBM DB2, in the SYSIMB schema, LTRIM takes a second argument of characters being trimmed like you have (see here). However, in the SYSFUN schema (and in most other SQL implementations) it only takes one argument and assumes you are trimming whitespace (see here).
Based on the error it looks like the interpreter wasn't expecting a comma, so it's probably trying to use the more standard version of the function and failing when it sees the second argument.
based on the documentation for function references you should be able to replace LTRIM with SYSIBM.LTRIM

What does the “&” symbol do in SQL?

What does the “&” symbol do?
select *
from emp
where ename like '&A%';
I infer you are using Oracle RDBMS since EMP.ENAME is from one of the example schemas from Oracle. In Oracle the ampersand "&" can be used as a substitution variable in SQL*Plus (client). See http://oracle-base.com/articles/misc/literals-substitution-variables-and-bind-variables.php#substitution_variables.
99% of the time, you are probably not doing substitution and want a literal ampersand. Such as INSERT INTO sometable VALUES ('Black & Decker'); So you would disable substitution first in SQL*Plus with SET DEFINE OFF.
It doesn't do or mean anything special in SQL string literal. It is just stands for itself; i.e. the ampersand character. So like '&A%' means a string that starts with '&A'.
Apparently (Joshua's answer), the ampersand can have special meaning in Oracle SQLPlus. But that isn't SQL. It is a different language that has SQL syntax embedded in it.

Adding a quote character in a Delphi String

I am writing a SQL program in Delphi 7 and came across a problem.
If you add an SQL command you have to use ' to indicate it is a Variable or String, but I want to build up my SQL statement because it is coming from different If statements and thus have to build it up. Therefore, I wanted to know if anyone knows a trick to add a ' into a string.
Don't make the same mistake like many before you and lookup parametrized queries or else you will be open for SQL injection attacks. If you need to include string constants in your query then use 2 single quotes ('') or the QuotedStr() function from the SysUtils unit.
Try two quotes to represent one i.e. ''

Perl: escape sql strings without having a db connection

I know the right way to sanitize sql strings in Perl is to use a prepared statement, however, this particular Perl script is generating statements to be executed later in a different environment, not Perl. It has no database to connect to.
How can I safely escape a string for insertion into a MySQL query. The solution doesn't have to be portable.
Unfortunately the quoting function used by DBD::mysql, and the MySQL client library in general, requires an active database handle. According to the documentation, "this is needed because the escaping depends on the character set in use by the server".
I can think of a few hacky solutions, but none of them are really satisfying, so let's work with this from the docs:
Characters encoded are “\”, “'”, “"”, NUL (ASCII 0), “\n”, “\r”, and Control+Z. Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped.
This suggests that you can probably get away with a quoting function that does either
s/([\\"'])/\\$1/g;
or
s/([\\"'\0\n\r\cZ])/\\$1/g;
although I would still be wary.
You could just check for special chars in the variables you add to your query string that are required to do an SQL-Injection such as ";" or brackets and replace them or throw them out?!?