Need help in datetime SQL Syntax - mysql

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;

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

Access query amazing

When I do that on access, SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN (1,2,5))
GROUP BY RMonturesImp.N°Fac;
but when I do this
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN Requête2)
GROUP BY RMonturesImp.N°Fac;
it doesn't work (it shows 1,2,5 indeed) although the result of Requête2 (which is a query) is also (1,2,5). I can't understand this!
Thanks in advance
It's quite easy. The IN (1,2,5)) must be explicit as SQL will not evaluate an expression not to say a function to obtain the values for IN.
So build your SQL in code creating the string, or pull the values from a (temp) table.
Try this:
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE RMonturesImp.N°Fac NOT IN (Select N°Fac From Requête2)
GROUP BY RMonturesImp.N°Fac;

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'))
);

Change order of MySQL query keywords?

I'm using this 3rd party report generating software. It has the following steps:
1) insert your SQL statement into a webpage.
2) invoke an API to send the a set of primary keys to the Query
3) A Report is generated.
Unfortunately, the software is dumb, and simply appends the WHERE clause after the SQL statement. However with MySQL the WHERE statement is supposed to be before the GROUP BY. So when the API appends a WHERE it fails because its invalid SQL. Is there some way to tell MySQL to expect the WHERE statement at the end?
select incident.incidentID,
GROUP_CONCAT(moccode2.Description) as MOC2Description
from incident
join incidentmoc on incident.IncidentID = incidentmoc.IncidentID
inner join moccode2 on moccode2.id = incidentmoc.moccodeid
/* WHERE should go here */
group by incident.incidentID
/* I want the WHERE to go here */
Derek Kromm is basically correct in what I asked for, unfortunately I have additional constraints. It's still going to append the WHERE.
So I tried this:
select incident.incidentID,
GROUP_CONCAT(moccode2.Description) as MOC2Description
from incident
join incidentmoc on incident.IncidentID = incidentmoc.IncidentID
inner join moccode2 on moccode2.id = incidentmoc.moccodeid
group by incident.incidentID
HAVING incident.IncidentID > 1
////////////////////////////////////////
now software appends WHERE invalid SQL
Use the HAVING keyword
This link has some details around using it: http://www.mysqltutorial.org/mysql-having.aspx

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

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.