MySQL phpMyAdmin error in a simple date query - mysql

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)

Related

converting java time to sqldate in query

java datetime (date.getTime()) is stored as string in mysql field.
How can we convert this to sql date using sql query. I am using mysql database.
Is there any sql function available?
For example - This is stored (1416231812348) for today's date in db.
Thanks for suggestions.
Java is returning the date as a long, to convert it you can use:
SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE
If you get an error, try the following (after testing, I can see that your data is stored in milliseconds so you need to use this method):
SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE
(Change event_time to be the field name in your table and MY_TABLE to be the table name.)
Here is a SQLFiddle example that shows it working.
Here is an answer that gives you formatting options as well:
http://notsoyellowstickies.blogspot.co.uk/2011/11/converting-long-into-datetime-mysql.html
There is a java.sql package, that has time included. You can send it straight into your database without needing to convert it.
This may be a more pre-emptive solution than converting a date string from Java, into time in MySQL.
A similar question was answered and may be able to help you out here:
A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
most probably you have recorded from:
System.currentTimeMillis()
so:
select DATE_FORMAT ( from_unixtime( your_table_field / 1000 ) , '%e %b %Y');
you can change the date format as you like.

MySQL using table columns in function to create alias to be used in sorting

It sounds more complicated than it actually is. Here is what I'm trying to do within the SELECT part:
SELECT TIMESTAMPADD(
UCASE(
SUBSTRING(offset_unit,1,CHAR_LENGTH(offset_unit)-1)
),1,'2003-01-02') as offset_date
offset_unit is a VARCHAR column in the database. It contains one of the following: "Hours","Minutes".
offset is an INT.
I am trying to convert the offset_unit to uppercase, after I have removed the last character ('s') so I can have a proper interval (MINUTE, HOUR...) so I can get a date that I can use in sorting afterwards, but MySQL keeps throwing an error. I have tested each step by adding one function at a time, and it only fails after I add TIMESTAMPADD. If I enter MINUTE manually then it works.
Any way to get this working?
Additional info: I am running this in CakePHP 1.3, in a find, within the 'fields' array, but that shouldn't be important.
this can be easily achived by using CASE WHEN clause as:
SELECT (CASE
WHEN offset_unit = 'HOURS'
THEN TIMESTAMPADD(HOUR,`offset`,'2003-01-02')
WHEN offset_unit = 'MINUTES'
THEN TIMESTAMPADD(MINUTE,`offset`,'2003-01-02')
END) AS offset_date
FROM my_table;
SEE SQLFIDDLE DEMO HERE
It doesn't work because TIMESTAMPADD does not take a string as the first argument, but a unit keyword, for example MINUTE. My guess is that you need to do this in two steps, first get the unit and then construct a query with the correct keyword.

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)

How can I resolve this issue using an sql query

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in Layman's expression ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
Remove the quotes around the field names
SELECT
DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) AS cap_maturity FROM capital
I think this should work.
I know its is an old question, but just trying to help others

Mysql Datevalue()=Date()

I'm trying to split a table in two views depending on whether the field "Date" is today or not.
I have tried using
WHERE DATEVALUE(`table`.`Date`)=DATE()
but I get an error at saving saying that the last ) has wrong syntax. I tried adding a group by, but apparently everything after the ) gives me the same message about wrong syntax.
Am I typing something wrong? Can I fix this? Is there maybe another way to do this?
The condition you're looking for is:
table.`Date` = CURDATE()
if you column is of DATE type or
DATE(table.`Date`) = CURDATE()
if it's of DATETIME type
You should try WHERE table.date = DATE( -your date- ). For instance:
WHERE table.date = DATE('1977-10-20') ;
your function usage is wrong:
WHERE DATE(table.Date)=CURRENT_DATE