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
Related
I have a table that includes dates, I'm trying to check if a date I have falls between the dates in the table. My query is working, but it doesn't return anything. This seems like it should be very simple, but I can't wrap my head around it.
SQL query looks like this:
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
The dates are generated dynamically in my script so I can ascertain if what I'm passing into it falls between the beginning and end dates in my table. I don't need any specific data from the table, just a boolean telling me whether the date is between the beginning and end dates or not.
You are looking for EXISTS:
SELECT
EXISTS(
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
) AS hasValue
Hope this helps,
Check your MySQL server's date format and your generated date format.
MySQL date format is like that: '2019-01-30 18:19:52'
Also you can try change
(beginning_date_from_table) and (end_date_from_table)
to
(end_date_from_table) and (beginning_date_from_table)
Check: How do I query between two dates using MySQL?
I phrased the question slightly wrong, in that I was trying to return the result of a conditional. Once I realised how to ask google the right thing, I quickly came up with this solution:
SELECT (CASE WHEN this_date BETWEEN beginning_date AND end_date THEN 1 ELSE 0 END) AS date_result
Thanks to the other people who answered, your input put my thinking on the right track.
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)
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.
This is a quick one.
In Linq to sql, I need to order by a datetime field descending then order by the time part of the same datetime filed ascending, make sense? Here's an example of the result needed:
[Serial] [Date (mm-dd-yyyy:hh-MM-ss)]
1 3-13-2008:10-10-02
2 3-13-2008:10-12-60
3 3-12-2008:12-05-55
I tried :
return query.OrderByDescending(c=> c.Time).ThenBy(c=> c.Time.TimeOfDay);
And also tried:
query.OrderByDescending(c => c.Time).ThenBy(c => c.Time.Hour).ThenBy(c=> c.Time.Minute).ThenBy(c=> c.Time.Second);
but never seemed to work.
Any suggestions?
Use the Date and TimeOfDay properties for the sort.
return query
.OrderByDescending(c=> c.Time.Date)
.ThenBy(c=> c.Time.TimeOfDay);
The Problem is that "Time" contains Date and Time information, thus, already sorting completely. You would have to take the Date Part to sort with first.
return query.OrderByDescending(c=> c.Time.Date).ThenBy(c=> c.Time.TimeOfDay);
Not tested :)
I think first orderByDescending sorts everything by datetime(date+time), so the following orderbys will become effective only if you have any records with the same timestamp.
Try this
query.OrderByDescending(c=> c.Time.Date).ThenBy(c=> c.Time.TimeOfDay)
Turn it to ticks will work as well:
return query.OrderByDescending(c=> c.Time.Ticks)
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.