what is the difference between SUBSTRING and SUBSTR functions (SQL)? - sql

before I used :
entityManagerFactory.createQuery("select p FROM Pays p where SUBSTRING(p.libeleClient, 0,1)
but when I use this query :
entityManagerFactory.createQuery("select p FROM Pays p where SUBSTR(p.libeleClient, 0,1)
I get an exception :(
who to remplace SUBSTRING by SUBSTR ?

SUBSTR is the function from Oracle
SUBSTRING is the function from MySql
depends on DB which u r using
EDIT:
try to edit your java code like below
String query = "select p FROM Pays p where SUBSTRING(p.libeleClient, 0,1)";
// from Connection Object (connection)
DatabaseMetaData meta = connection.getMetaData();
//If the DB is Oracle
if(meta.getDatabaseProductName()).contains("Oracle")) {
entityManagerFactory.createQuery(query.replace("SUBSTRING", "SUBSTR"));
}// If the DB not Oracle , any Other like MySql
else {
entityManagerFactory.createQuery(query);
}

substring is the sql operation defined in the sql standard ISE:IEC 9075:1992.
substr is an old syntax used by oracle. This wrong syntax is completely inconsistent with sql usage of real english words, never abbreviations.
Oracle still does not support the standard syntax.
Did anyone wrote a hack in oracle to support the standard syntax ?

You don't say what exception you get, but I 'm guessing it's a syntax error. The correct syntax for Oracle's SUBSTR() is ...
where SUBSTR(p.libeleClient, 0,1) = 'X'
...(or whatever). That is the first occurence of a single character must equal; some specified value. SUBSTR() is not a boolean function.
Whereas SUBSTRING() is not an oracle function at all. Either you've borrowed the syntax from some other database, or you're using a bespoke function without realising it.
"I tried your suggestion but it does not work"
Do you get an error? Or do you mean it doesn't return any records? Because I have given a perfectly valid usage, as defined in the documentation. But you haven't given any examples of your data, so it's almost impossible for me to provide a solution which will return rows from your database.

Related

Does TDengine support functions in where clause?

I tried to query data in TDengine with the following sql clause:
SELECT * from TABLE where COLUMN < FUNCTION();
and I got illegal condition expression error.
Does anyone know if using function in where condition is illegal usage in SQL convention or I need to resort to other syntax?
Using function in where condition is illegal. Maybe it will be supported later.

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

Functions not working in Table Adapter Query bulider

I'm using Visual Studio 2013, I try to use some function (like "cast" and Year ) in Table Adapter Query Builder (in DataSet.XSD ). I get erroe messgae every time. I run the sql statement on other sql programs and it's work fine. did anyone face this problem?
Is your dataSource SQL Server or SQLite. If you are using SqLite then functions such as Year(),Cast() are not allowed.
If you are using SQLite then please find below link for date time function reference,
https://www.sqlite.org/lang_datefunc.html
As you have asked for Cast function, there is SO post describing the cast function, which is similar to that of SQL Server
SQLite supports CAST and:
Casting an INTEGER or REAL value into TEXT renders the value as if via sqlite3_snprintf() except that the resulting TEXT uses the
encoding of the database connection.
So you can do things like this:
select cast(some_integer_column as text) from some_table;
Or, depending on what you're trying to do, you could just treat the
numbers as strings and let SQLite coerce the types as it sees fit:
select some_int || ' pancakes' from some_table; select some_int || ''
from some_table;
SQLite does not have this function YEAR(...). Try strftime('%Y', degrees.ExamDate) = '2017' instead.

Informatica Coding to SQL

I am attempting to translate the following Informatica code to the equivalent SQL scripts. I am a little stuck as I am not familiar with Informatica and would appreciate any assistance.
The original informatica code reads as follows:
LTRIM(RTRIM(SUBSTR(COV_REINS_CONCAT_BK,11, INSTR(COV_REINS_CONCAT_BK, '|',1,3)-INSTR(COV_REINS_CONCAT_BK, '|',1,2)-1 ))) || 'C'
select LTRIM(RTRIM(SUBSTR(COV_REINS_CONCAT_BK,11,INSTR(COV_REINS_CONCAT_BK,'|',1‌​,3)
-INSTR(COV_REINS_CONCAT_BK,'|',1,2)-1 )))||'C' from TABLE;
Above script will work fine in Oracle. Please replace table with your table name.
It becomes increasingly difficult to port such expressions that make extensive use of INSTR function using the occurrence parameter.
Maybe it's easier to create in your MS-SQL database an INSTR equivalent function that supports the occurrence parameter. See how to create the dbo.INSTR function here.
On this same site you can also see a table with more function equivalences from Oracle to MS-SQL.
Then the expression from the comment becomes much easier to translate:
LTRIM(RTRIM(SUBSTRING(COV_REINS_CONCAT_BK,11,dbo.INSTR(COV_REINS_CONCAT_BK,'|',1‌​,3)
-dbo.INSTR(COV_REINS_CONCAT_BK,'|',1,2)-1 )))+'C'
Here SUBSTR became SUBSTRING, and INSTR became dbo.INSTR and concatenation || was changed to +.
The SUBSTR() and instr() functions are from the Informatica Transformation Language. IMHO it has its roots in function names from Oracle and MSSQL / Sybase function names. This is why it doesn't translate directly to either but is similar. The functions are very well documented in the online help. You'll need to review the switches in the INSTR() function for case sensitivity and the like to ensure their parallel can be written correctly in another tool. The numbers may translate to different things and in some of the Informatica functions the end arguments can be omitted such as, in the SUBSTR() function, meaning that the SUBSTR() will take effect from the numbered position to the end of the string regardless of length. The typing of the port in Informatica can affect the result too, although in this case, the combined function is performing a trim at the end.
SUBSTR() and INSTR() functions are not MS SQL Server functions. I'm guessing the code snippet is Oracle's PL/SQL. Try resources such as http://www.dba-oracle.com/oracle_news/2005_12_16_sql_syntax_differences.htm

SQL query giving an incorrect syntax error

Select COUNT(*) as 'Number'
From image
WHERE (image.current_phase = 'aggregation' AND (image.raw_filename REGEXP '%gordonpho%back%$'))
The above SQL query is giving me an incorrect syntax error. I want to get the number of rows from the table image where the column image.current_phase has aggregation as text. Also the column image.raw_filename ends with '%gordonpho%back%'.
Can anybody see the syntax error in my statement?
REGEXP isn't valid in T-SQL, but you don't need it anyway since your "regular expression" would be the same using LIKE anyway:
Select COUNT(*) as 'Number'
From image
WHERE (image.current_phase = 'aggregation'
AND (image.raw_filename LIKE '%gordonpho%back%')
SQL Server doesn't support regular expressions natively. Try using LIKE:
Select COUNT(*) as 'Number'
From image
WHERE (image.current_phase = 'aggregation'
AND (image.raw_filename LIKE '%gordonpho%back%'))
For times where you really need/want to use regular expressions, you'd have to do it via SQLCLR support (.NET code).
It's possible in your SQL implementation that image is a reserved word. Try quoting it with backticks (MySQL), square brackets [] (MSSQL) or double quotes " (most others).
Also, the %s in '%gordonpho%back%$' aren't treated as wildcards in regular expressions. Try replacing the literal with:
'.*gordonpho.*back.*$'