SQL extra only the day instead of entire date - mysql

I have a field in my table called created_date. The date format is 2010-02-28. I just wondering is it possible to do a mysql statement, only return the day instead of the entire date. eg. 28
SELECT
day(created_date)
FROM
table
This above query throw me error, is there a way i can do similar stuff?
cheers

Use MySQL built-in function called DAYOFMONTH
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3
DAYOFMONTH()
From Docs,
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.

MySql EXTRACT function extracts day, month or year from a given date.
select extract(day from created_date) as created_day from table

you can fetch the whole date in your format and display only the required field that is date by using this
date("j", strtotime(date('Y-m-d')) );

Related

MYSQL: How to get rows till specific month's end from very start

I'm working on a project for a Fuel Pump. Here is my table for the records:
I want to display rows from very start (2019-07-03) till the end of the specific month (e.g 2019-09-29) . How could i do achieve this?
A simple WHERE clause will do the trick
SELECT id, date, total_amount, total_paid
FROM table
WHERE date <= LAST_DAY(CURDATE())
CURDATE() will return current date i.e. 2019-09-08
LAST_DAY() will return the last date of current month i.e. 2019-09-30
WHERE clause will return all rows with date <= 2019-09-30
Update
If you want to filter records based on user input which is month and year ( 2019-09 ) then either it can done by appending '-01' at scripting side or using CONCAT at MySQL level,
WHERE date <= LAST_DAY(CONCAT('2019-09', '-01'))
I think this will work. You can change dates accordingly.
Select *
From table
Where date>='2019-07-03' AND date<='2019-09-29'

How to check if a date is before 10th of that month in mysql?

I have a date column :
Report_Received
8/7/2019 11:39
6/18/2019 10:25
I want to check if the date falls before 10th of that month of that year.
If yes, need to say True else False under a new column(Complaince_Reporting).
Is there any way to do this?
You can use the DAY function to get the day of the month, but first, because you are not using a MySQL Datetime column or a valid MySQL date format, you need to convert your date using STR_TO_DATE. Something like this:
SELECT ...,
DAY(STR_TO_DATE(Report_received, '%c/%e/%Y')) < 10 AS Compliance_Reporting
FROM yourtable
Demo on dbfiddle

Make a query that will get a specific month from my sales table

In my sales table have the date column and the date is formatted like this January 9, 2018, 5:06 pm. How can I specifically get all the sales made from the month of January 2018 using MySQL?
assuming you date column is a date data type columns you can use year() and month() function
select * from my_table
where year(my_date_col) = 2018
and month(my_date_col) = 1
Since
the date is formatted like this January 9, 2018, 5:06 pm
and this is not recognizable time format for MySQL I belive the solution is:
SELECT * FROM table WHERE date like 'January%2018%';
I know this is not a "best practice" as well as storing time in a wrong type.
Since the column isn't already in a DATE format, let's assume it has to stay in its current format for the application purposes and add an auxiliary DATE column to select on (because SELECTs will be more optimized this way).
First we add a VIRTUAL column (requires MySQL 5.7+), which is a calculated column that is calculated at runtime but not stored on disk. We don't need to store this column on disk because we're going to index it later. Our VIRTUAL column will convert the application date's format to a MySQL DATE.
Replace str_date with your current date column:
ALTER TABLE `my_table`
ADD `virtual_date` DATETIME AS (STR_TO_DATE(`str_date`, '%M %d, %Y, %l:%i %p')) VIRTUAL AFTER `date`;
Now add the INDEX:
ALTER TABLE my_table ADD INDEX (`virtual_date`);
Now we can perform a properly indexed SELECT:
SELECT *
FROM my_table
WHERE virtual_date >= '2018-01-01'
AND virtual_date <= '2018-01-31'
or
SELECT *
FROM my_table
WHERE virtual_date >= '2018-01-01'
AND virtual_date <= LAST_DAY('2018-01-01')
The MySQL DATE format is YYYY-MM-DD. The latter query is logically simpler; the LAST_DAY() function will give you the last DATE of the month given a DATE or DATETIME value.

How to match current date with the database entries and retrieve it

Suppose I have a MySQL table called birthday with dob as a column. dob has lot of dates in date time format. I have a perl variable $test=current date. My problem here is, how can I get the all the values present in the dob column that matches with the day and month of $test.
Let us assume, $test='2013-05-28 00:00:00' So, now I dont know how to write a query to select all the entries in MySQL table which matches the day and month of the $test i.e 28 and 5 respectively.
You need to use Month and Day function inside mysql query like
select * from birthday where Day(dob) = Day('2013-05-28 00:00:00')
and Month(dob) = Month('2013-05-28 00:00:00');
Hope it works.

datetime searching using sub-sections of the YYYY-MM-DD HH:MM string

[MySQL/PHP] My table has a date column of datetime format. All records are of the YYYY-MM-DD HH:MM:SS variety.
MySQL queries like SELECT record FROM table WHERE date > '1941' AND date < '1945' work nicely.
MySQL queries like SELECT record FROM table WHERE date > '1941-03-01' AND date < '1945-01-30' also work nicely.
But what about if I wanted all records that were filed in March, regardless of year? Or all records filed on the 17th, regardless of month/year?
``SELECT record FROM table WHERE date = '03'` clearly doesn't work.
I know I could snag it with a LIKE '%-03-%' parameter, but that doesn't leave room for me to search for range, like all records from March to May.
Help? :-)
Try WHERE MONTH(DATE(`date`)) BETWEEN '03' AND '05'
The DATE() part is to extract the date from the timestamp to be used with MONTH().
You can use MySQL date functions:
SELECT record FROM table WHERE MONTH(date) = 3
SELECT record FROM table WHERE DAY(date) = 17
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
If you look at http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html, you will find many useful functions for your purpose.
... WHERE MONTH(date) = 3, e.g. =)