mysql where statement date issue - mysql

hope someone can help,
What i`m trying to do is pull all results from the database for any given month,
Is the possible using a mysql query only (no php).
Im using a template app with the only access i have is through i mysql where statement, So i need to work out from "2013-04-01" what the month is and grab it if its the one i want.
The code below works but obviously ties me too the 2013 year.
`SELECT * FROM table_name WHERE Display_Date => 2013-04-01 AND Display_Date <= 2013-04-30 LIMIT 10`

SELECT
*
FROM
table_name
WHERE
MONTH(Display_Date) = MONTH(CURDATE())
AND YEAR(Display_Date) = YEAR(CURDATE())
reference
MONTH()
YEAR()

Related

Access Group By Month Names Using Format Function

I am Unable to Sort the Order By by Month names in MS Access Database
I am Using the Format Function to retrieve Month Name (If I use MonthName(Month(date)) Function The Query is Executing at Access DB(but when I am trying to Use the Same Query from My Application I am getting Errors
My Query is
This is My Query(Code )Used all the Functions of Format But I am Unable to Order By MonthNames
SELECT Format(date ,"mmmm") as MonthName,
Sum(Rojnamchatable.asal1) AS SumOfasal1,
Sum(Rojnamchatable.chaat1) AS SumOfchaat1,
SumOfasal1+SumOfchaat1 AS TotalBiri1,
Sum(Rojnamchatable.asal2) AS SumOfasal2,
Sum(Rojnamchatable.chaat2) AS SumOfchaat2,
SumOfasal2+SumOfchaat2 AS TotalBiri2,
GROUP BY Format(date ,"mmmm")
ORDER BY Format(date ,"mmmm")
I am Getting Data Perfectly But I am unable to get Order By Month Name
(i.e Jan ,Feb ,March)
I Even Used Format(date,"mm") Function If I Use that I am getting Erors
So Please try to Solve My Issue
I Spent 2 days for this
How about adding the month (number) column but not displaying it?
SELECT
Format([date] ,"mmmm") as MonthName,
Sum(Rojnamchatable.asal1) AS SumOfasal1,
Sum(Rojnamchatable.chaat1) AS SumOfchaat1,
SumOfasal1+SumOfchaat1 AS TotalBiri1,
Sum(Rojnamchatable.asal2) AS SumOfasal2,
Sum(Rojnamchatable.chaat2) AS SumOfchaat2,
SumOfasal2 + SumOfchaat2 AS TotalBiri2,
FROM
yourTableName
GROUP BY
Format([date] ,"mmmm"), Month(date)
ORDER BY
Month([date])
Please note I have enclosed date in [], this is because of the fact Date is a reserved word and should not be used as a column/field name.

Mysql Select with Dates and maybe Case when

im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.

How can I group data by month, day and hour in mysql?

I have a mysql database that looks like this:
id | userid | timestamp | activity
Timestamp is a datetime data type, I need to get the data grouped by month, day and hour. I am using mysql and php for my scripts.
I am able to do it by month and day with the following query:
$query = "SELECT COUNT(id) as totals FROM security_transactions WHERE YEAR(timestamp) = 2012 GROUP BY MONTH(timestamp), DAY(timestamp)";
I need to do it by month day and hours.
Please help.
Thanks.
You can add , HOUR(TIME(timestamp)) to your group by query providing your column is of DATETIME format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_hour
Also, from the error messages put in the comments below, it looks like #Aprentice is not using mysql, but I've improved this answer for others looking for mysql.
I have never used mssql, so I can't test this but the following might work to group by nearest hour:
GROUP BY dateadd(hour, datediff(hour, 0, timestamp, 0)
Just take it one step further and use HOUR() as well. You will first need to extract the time portion of the timestamp. But guess what, there is a function for that as well ;)

Working with Dates in SQL

I have an events table and need to pull the 4 closest dates to today's date and they can be in the past, present or future.
What would the SQL (using MySQL) be for this if it is possible?
Thanks
Brett
I don't know which DB you are using, but this works with mysql:
select *
from event
order by abs(datediff(event_date, now()))
limit 4
Try using the TIMEDIFF function like this:
select *
from events
order by abs(timediff(now(), yourdatecolumn))
limit 4;

Query with sql datepart

I have a table with two column as :
serial_number
1
2
3
dateOfAppoinement
2011-06-30 00:39:04.130
2011-06-30 00:40:01.130
2011-06-30 00:49:04.130
I want to get the highest serial_number of a day. I have to avoid the time part. I'm just using the date part.
Can anyone tell me how can I do this?
I updated my answer to use Convert like snkmchnb suggested. However in SQL Server 2008 there is a DATE datatype that is just the date portion of the year so you don't have to specify the 120 code. I tested this and it works perfectly and the SQL is pretty straight forward.
SELECT
MAX(serial_number),
CONVERT(DATE, dateOfAppointment) as [Day]
FROM
#TempSerialsByDate
GROUP BY
CONVERT(DATE, dateOfAppointment)
I think this select solves your problem, can't figure if it is the best way:
select dateadd(dd,0, datediff(dd,0, dateOfAppoinement ))
from your_table where serial_number = (select max(serial_number) form your_table)
How truncate date in sql server can be found here:
How can I truncate a datetime in SQL Server?