Can't use MySQL extract() function in the WHERE clause - mysql

I've run the following query:
UPDATE main_table, reference_table
SET main_table.calc_column =
(CASE WHEN main_table.incr = "6AM"
THEN reference_table.col1+reference_table.col2+...
WHEN main_table.incr = "12AM"
THEN reference_table.col7+reference_table.col8+...
WHEN main_table.incr = "6PM"
THEN reference_table.col13+reference_table.col14+...
ELSE reference_table.col19+reference_table.col20+...)
WHERE main_table.month = extract(month from reference_table.thedate)
AND main_table.day = extract(day from reference_table.thedate)
I've used extract() function since my reference_table doesn't have month and day columns but has the date column named thedate. I've used the extract() function on the reference_table many times before successfully, so, I know that there's nothing wrong with my extract function syntax. However, in this instance, MySQL complains. It probably has to do with the fact that I've used in the WHERE clause.
I know that this issue could get fixed if I added the month and day columns to the reference_table to avoid using the extract() function. However, I'm very reluctant to do that and would like to avoid it. How can I make it work?`

As discussed in the original question, the reason you are getting this error is that the CASE expression is missing its END.

Related

Filtering SQL results by date

Here is my query for a maintenance dates list.
SELECT `checkdates`.`checkdateplanneddate`, `checkdates`.`checkdatevehicle`, `checktypes`.`checktype`, `checktypes`.`emailto`, `checktypes`.`daysnotice`
FROM `checkdates`
, `checktypes`
WHERE `checktypes`.`checktype` = `checkdates`.`checkdatechecktype`;
The idea is..
Everyday the server will email customers to let them know which checkdates are coming, based on the days notice that is set for that type of check. (see image)
Currently it is showing all checkdates.
All i need to do is filter the list so it only shows the dates that are
"Todays date plus checktypes.daysnotice"
I have tried many different queries, but cannot seem to get the right combo.
Thank you in advance
I have attached an image to show that the data is available
If I understand your question correctly, and assuming that you are running MySQL (as the use of backticks for quoting and the phpmyadmin screen copy indicate), you can use date arithmetics as follows:
SELECT cd.checkdateplanneddate, cd.checkdatevehicle, ct.checktype, ct.emailto, ct.daysnotice
FROM checkdates cd
INNER JOIN checktypes ct ON ct.checktype = cd.checkdatechecktype
WHERE cd.checkdateplanneddate = current_date + interval ct.daysnotice day
The where condition implements the desired logic.
Side notes:
Use standard, explicit joins! Implicit joins (with commas in the from clause) is a very old syntax, that should not be used in new code
Table aliases make the query easier to write and read

Statement in trigger is not "picking up" the condition in its Where clause

so I'm currently working on a MySQL trigger. I'm trying to assign values to two variables when a new record is inserted. Below are the queries:
SET mssgDocNo = (SELECT Document_ID FROM CORE_MSSG WHERE Message_ID = new.MSSG_ID);
SET mssgRegime = (SELECT CONCAT (Regime_Type, Regime_Code) FROM T_DOC WHERE CD_Message_ID = new.MSSG_ID);;
For some reason, the second SQL query is not picking up the 'new.MSSG_ID' condition while the first query in same trigger recognizes it. I really can't figure out what seems to be the problem.
When I replace the 'new.MSSG_ID' with a hard-coded value from the database in the second query it seems to work. I doubt the 'new.MSSG_ID' is the problem because it works perfectly fine in the first query.
I've tried pretty much anything I could think of. Would appreciate the help.
I would write these more simply as:
SELECT mssgDocNo := Document_ID
FROM CORE_MSSG
WHERE Message_ID = new.MSSG_ID;
SELECT mssgRegime := CONCAT(Regime_Type, Regime_Code)
FROM T_DOC
WHERE CD_Message_ID = new.MSSG_ID;
The SET is not necessary.
I did make one other change that might make it work. I removed the space after CONCAT. Some versions of MySQL have trouble parsing spaces after function calls.

Need help in datetime SQL Syntax

In TableAdapter Configuration Wizard, I am having an issue with my SQL statement.
I wanted to filter CreatedDate for that particular Year based on the current date. So I use the Year() function, but when executed, it prompts an error message.
Below is my SQL statement.
SELECT cff.CFNo, cff.RegionalOffice, cffitemstatus.Reason
FROM cff, cffitemstatus
WHERE cff.RecordNo = cffitemstatus.RecordNo
AND (cff.CreatedDate = `YEAR`(CURDATE()))
ORDER BY cff.RegionalOffice, cffitemstatus.Reason
Need guide and advise on this issue.
You should learn to use explicit JOIN syntax. Simple rule: Never use commas in the FROM clause.
Then, one method to do what you want is to use YEAR() on both dates:
SELECT cff.CFNo, cff.RegionalOffice, cffitemstatus.Reason
FROM cff JOIN
cffitemstatus
ON cff.RecordNo = cffitemstatus.RecordNo
WHERE YEAR(cff.CreatedDate) = YEAR(CURDATE())
ORDER BY cff.RegionalOffice, cffitemstatus.Reason;

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.

MySQL "in" command

I want to know how we can do this using the IN comparison syntax.
The current SQL query is :
select * from employee
where (employeeName = 'AJAY' and month(empMonth) = month(curdate()))
or (employeeName ='VINAY' and month(empMonth) = month(curdate()))
I tried it using IN comparison function, but am unable to properly set the pieces. Can any one help me?
select * from employee
where employeeName in ('AJAY','VINAY')
and month(empMonth) = month(curdate()); // ERROR.
I tried it in MySQL Query.
Thank You,
Sindhu
Your solution is fine for most DBMS (data-base management systems). As far as I know it is no problem in MySQL. But some years ago I had similar problems in DB2 and also in another more exotic DBMS named "Focus".
Maybe this can help:
Put the complete where-block into a pair of brackets.
Inside this block put each comparison in a pair of brackets again.
Move the IN-Comparison to the end of the where-block.
This would transform your example into this code:
SELECT *
FROM employee
WHERE (
(month(empMonth) = month(curdate())
AND
(employeeName IN ('AJAY','VINAY'))
);