MySQL search query is not executing in Postgres DB - mysql

In MySQL the below query is executing properly.
SELECT * FROM <Table-name> WHERE (Table.ID LIKE '1%')
But when I try to execute the above query in Postgres, I get the following exception
"org.postgresql.util.PSQLException: ERROR: operator does not exist:
integer ~~ unknown Hint: No operator matches the given name and
argument type(s). You might need to add explicit type casts".
If I convert the same query
SELECT *
FROM <Table-name>
WHERE CAST(Table.ID as TEXT) LIKE '1%'
This gets executed directly in Postgres DB. But I need some query which implicitly type cast in DB, which allows me to execute the MySQL query without any exception. Because I remember there is a way for integer to boolean implicit type cast.
Thanks in advance.

If you search this site with [postgresql] explicit type casts you will get enough information to solve your problem.

Related

How can I fix the error with json_object in postgresql-9.6?

There is Zabbix with PostgreSQL database monitoring. There is one trigger in Zabbix that constantly spam error:
ERROR: function json_object(text[], text[]) does not exist
LINE 11: SELECT json_object(array_agg(name), array_agg(setting)) F...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 190
Found a template in which this trigger is registered. It is standard, I tried to request a line separately in Postgres, as a result the same error.
The request itself:
select json_build_object('extensions',(select array_agg(extname) from (select extname from pg_extension order by extname) as e),'settings', (select json_object(array_agg(name),array_agg(setting)) from (select name,setting from pg_settings where name != 'application_name' order by name) as s));
I ask you to tell what is wrong.
The function json_object(text[], text[]) exists in PostgreSQL (documentation) since version 9.4.
Are you sure you use the version 9.4 or above ? You can check with:
SELECT version();

Cannot find MySQL syntax error

MySQL gives me syntax error for a simple query but I don't see any error. If you guys find any please help.
insert into cast(sid,celeb_id,type,name,prior)
values(30,1,1,'James Keziah Delaney',2)
It gives sql syntax error near cast.
The main cause of this error is that there is a function Cast in mysql.
It look like to call the cast() function.
You can choose one of the solutions to solve it.
add ` to contain cast table name
look like this.
insert into `cast`(sid,celeb_id,type,name,prior) values(30,1,1,'James Keziah Delaney',2)
sqlfiddle
add a space between cast and ( let mysql know you did't want to execute Cast method. thank for #Barmar remind.
Note:
I would suggest you don't give the table name from keyword or function name.
You can't insert into cast(). It requires a list of column names.

MySQL SUM(BIGINT) query returns bytearray instead of BIGINT, but only when called as a prepared statement

I have the following MySQL query:
SELECT COUNT(billable_size),
SUM(billable_size),
MIN(billable_size),
MAX(billable_size)
FROM inv_files
WHERE billable_size >= %s
AND billable_size < %s
The billable_size column is a BIGINT, and the SUM() of its values can get anywhere up to about 1e+14.
When I run this query using simple string interpolation --
cursor = cnx.cursor()
cursor.execute(query_sql % (from_size, to_size))
-- MySQL Connector/Python correctly returns the sum value as a Python Decimal.
But when I try to run it as a prepared statement --
cursor = cnx.cursor(prepared=True)
cursor.execute(query_sql, (from_size, to_size))
-- the sum instead gets returned as a bytearray, causing any number of headaches. (Apparently there are some known issues with MySQL returning byte arrays when it should return something else? Maybe I should count myself lucky it works with plain string interpolation.)
Is there a SQL "type hint" or some other way I can get MySQL Connector/Python to correctly return the right type?

How do I write an SQL query that fails?

I need this query for testing exception handling, so I would prefer that the query is not schema dependent. I am looking for something like SELECT 1; but of course that doesn't fail.
I am using Java and MySQL but I hope to find answers that doesn't depend on programming languages and/or RDBMSs.
What about "SELECT 1/0" for starters?
You could put an invalid token into the query
select doesnotexist.* from something_else
Or of course, what you should do is mock out the method and have it throw the exception during your test.
there are tons of ways to make a query fail, like mispelling a field, or selecting from non existing tables. for example:
SELECT some_fake_field FROM table_that_doesnt_exists
One way to trigger a failure is to call a stored procedure with the wrong number of parameters. Another similar idea is to write an update/insert statement with the wrong number of arguments...
More ideas here:
How to raise an error within a MySQL function
Any old syntax error will do... like an unterminated string
select 'bob
To get 1/0 to raise an error in MySQL, you need to set sql_mode to ERROR_FOR_DIVISION_BY_ZERO.
Try this:
SET sql_mode = 'ERROR_FOR_DIVISION_BY_ZERO';
SELECT 1/0;
If this sql_mode isn't set, MySQL will return a NULL instead of an error.
You can check what your current settings are with the following:
SELECT ##GLOBAL.sql_mode;
SELECT ##SESSION.sql_mode;

error in navicat lite mysql gui : Unknown column in 'field list'

I am using navicat lite with mysql database.
my database is test
and i have a table named transactions with schema:
date, sharename,buyORsell,quantity,finalrate
I am trying to create a new procedure which takes some input and outputs some results, and I would want that when I execute this procedure, this procedure asks for the input parameter from the user and outputs results accordingly.
My requirement: given a input (transaction type i.e. 'buy' or 'sell'), it should output all the transactions with that type
I create a function and click on procedure
BEGIN
#Routine body goes here...
select * from test.transactions where buyORsell = intype;
END
In the space below, I see:
IN `intype` char
But when I run this prcedure and type sell in "enter parameter" pop-up, I get the error:
Unknown column 'sell' in 'field list'
Please help..
I had the same problem. I fixed it by defining char with a length (i.e. char(254)) and then wrapping the input string in quotes when prompted.
You have an error in your stored procedure definition and forgot to include the intype as a parameter
Looks like it might be a Navicat problem. I now use HeidiSQL for functions and that seems to work fine.
With Navicat, the same problem occurs when I have an input parameter - whether or not I use that paramater. Like Martin I can clear out all the code from BEGIN...END but still get the error if I use any char or varchar input parameter.
In fact, using varchar as an input parameter ALWAYS gives a save/compile error with ANY function using Navicat. With Navicat, Varchar in functions seems completely broken.