Mysql queries giving error after upgrading - mysql

why this simple query not working. I know it was OK before I upgraded my mysql version. i know there are some syntax changes in new version that I done. but this is simple query no join, but not working.
SELECT * FROM ship WHERE sensitive='Y' and entry_date between $startdate and $enddate
Please help me.
EDIT:
Upgraded from mysql4 to mysql5 and error is 'syntax error'

I think you upgraded from mysql 4 to 5 . sensitive is new reserved keyword in mysql5. You need to enclose it in backticks(`) while using it in query.
Mysql 5 allows reserved words to be used is query but with backticks.
Try:
SELECT * FROM ship WHERE `sensitive`='Y' and entry_date between $startdate and $enddate

You have "space" character in the '$start date'. That might be one reason. So, if your $startdate and $enddate are timestamps:
SELECT * FROM ship WHERE `sensitive`='Y' and entry_date between $startdate and $enddate
if they are not timestamps, but date in iso format, but them in single quotes.

Related

MySQL: Comparing the result of two convert functions

Im currently working on a database that have been initialize with dates in varchar instead of using datetime format.
I'm supposed to compare the dates of the DB with an input date (31/05/2020 actually).
Here is the part of my code that makes troubles :
AND Convert(datetime,t1.fin_contrat, 103) > Convert(datetime, '31/05/2020', 103 )
But I've an error from PhpMyAdmin which is : #1064 - Syntax error near 't1.fin_contrat, 103) >= Convert(datetime, '31/05/2020', 103 ) GROUP BY siren OR' line 1
According to this link : https://learn.microsoft.com/fr-fr/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15#implicit-conversions varchar should be able to being converted directly to datetime value.
I tried to use WHERE instead of AND it doesn't work. So I'm out of option, and i'm seeking for ideas.
Thanks for your help
In MySQL, you want something like this:
where date(t1.fin_contrat) = str_to_date('31/05/2020', '%d/%m/%Y')
This corresponds to the code in your question, but adapted for MySQL.
The fonction CONVERT is not the same betweeen Microsoft SQL. and Mysql
https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_convert
To convert a string. to a date. you must use STR_TO_DATE
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
ps:
Every sql server software ( Oracle , Redshift , Posgtres , Mysql , MariaDB , MS SQL server , Sqllite , ...) has his own SQL dialect . Usually simple queries are portable but more complex or using functions queries must be rewriten .

rails difference between two dates inside .where

Here is my logic
I want to get the closest 4 more expensive mobiles to a specific mobile #mobile but under one condition the difference between the release dates of the two mobiles is not more than a year and half
Here is the query
high = Mobile.where("price >= #{#mobile.price} AND id != #{#mobile.id} AND visible = true").where("ABS(release_date - #{#mobile.release_date}) > ?", 18.months).order(price: :ASC).first(4)
The first .where() works perfectly but the second is not working and I get this error
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00 UTC) > 46656000) ORDER BY `mobiles`.`price` ASC LIMIT 4' at line 1: SELECT `mobiles`.* FROM `mobiles` WHERE (price >= 123123.0 AND id != 11 AND visible = true) AND (ABS(release_date - 2016-04-10 00:00:00 UTC) > 46656000) ORDER BY `mobiles`.`price` ASC LIMIT 4
I think now you can get my logic. What is the right syntax to achieve it?
A couple of tips here:
It is a dangerous practice to concatenate variables into your queries using the "#{}" operator. Doing so bypasses query parameterization and could leave your app open to SQL injection. Instead, use "?" in your where clause.
The reason MySQL is giving you an error is because you are concatenating a string into your query without encapsulating it in quotes.
With these two things in mind, I would start by refactoring your query like so:
high = Mobile.where("price >= ?", #mobile.price)
.where.not(id: #mobile.id)
.where(visible: true)
.where("ABS(release_date - ?) > 46656000", #mobile.release_date)
.order(price: :ASC).first(4)
You will note that I replaced 18.months with 46656000. This saves a few clock cycles in the Rails app. Depending on your database schema, the last where clause may not work. The modification below may end up working better.
As a further refinement, you could refactor your last where clause to look for a release date that is between 18 months before #mobile.release_date and 18 months after. The saves your MySql database from having to do the math on each record and may lead to better performance:
.where(release_date: (#mobile.release_date - 18.months)..(#mobile.release_date + 18.months) )
I do not know your database schema, so you may run into date conversion problems with the code above. I recommend you play with it in the Rails console.
Use a Range to query between dates/times:
Mobile.where("price >= ?", #mobile.price)
.where.not(id: #mobile.id)
.where(release_date: 18.months.ago..Time.now)
.order(price: :ASC)
.first(4)

JasperReports with MySQL - Unable to use DATE_ADD in query editor

I am unable to use date_add in the query editor. I am connected to MySQL database
select DATE_ADD(Registrationdate, INTERVAL $P{addday} DAY) from tab
I get the following errors :
a. No viable alternative at input 'INTERVAL'
b. Mismatched input $P{addday} expecting FROM
Any idea on this would be of great help
Thanks
Rathi

MySQL phpMyAdmin error in a simple date query

In MySQL using phpMyAdmin I am trying out this simple query to fetch rows that satisfy a certain date criteria:
select *
from student_invoice
where date_generated < '2012-01-01'
The date_generated is of date type. I get an error in phpMyAdmin that says:
ERROR: Unclosed quote # 64 STR: '
I have closed all quotes so its not making sense. The phpMyAdmin version is 2.11.9.6
Adding a new answer, as it's unrelated to my other one.
According to this bugzilla post here, your version suffers from this bug!
Upgrading to 2.11.11 or higher should fix this issue.
This may sound silly, but have you tried wrapping the date in double quotes?
SELECT *
FROM sometable
WHERE somedatecolumn < "2012-01-01"
Make sure that those are actually single quotes surrounding the date, not backticks.
Not familiar with the specific error. But you could try casting your static date to a date format, just to make sure it jives with the datecolumn format. Or even casting both? I.e:
where cast(somedatecolumn as DATE) < cast('2012-01-01' as DATE)
I have a feeling that won't work though. So maybe this?:
where somedatecolumn < cast('2012-01-01' as DATE)

How to use DAY function in PostgreSQL database?

I have this query for MySQL database:
Article.where('DAY( created_at ) = DAY( ? )', day)
And I try to use the query above also in the PostgreSQL database, specifically I am trying something like this:
Article.where("DATE_PART('day', created_at) = DATE_PART('day', ?)", today)
But I am getting the error PGError: ERROR: function date_part(unknown, unknown) is not unique
Why is there that error? I thought I have the syntax by documentation...
It seems today is a string of the pattern YYYY-MM-DD. You could just extract the rightmost two characters, instead of casting to date and then extracting a number. Would be faster and simpler:
Article.where("date_part('day', created_at::date)::int
= right(?, 2)::int", today)
right() requires PostgreSQL 9.1 or later. In older version you can subsitute:
... = substring(?, 9)
Because you want characters 9 and 10.
This should work, too:
Article.where("date_part('day', created_at::date)::int
= date_part('day', ?::date)", today)
Note, that you must cast '2012-01-16' to date, not to interval. '2012-01-16'::interval would be nonsense.
According to the documentation there are two functions:
date_part(text, timestamp)
date_part(text, interval)
so database cannot choose which one you want. Cast the second parameter to timestamp, e.g. like this:
DATE_PART('day', created_at::interval) = DATE_PART('day', ?::interval)