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

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.

Related

MYSQL How to get list of records with time period included in specified year and month?

I have a table that links an employee to a project, the columns are:
BIGINT ID;
BIGINT FK_PROJECT;
BIGINT FK_EMPLOYEE;
DATE FROM;
DATE TO;
from and to are the first and last day during which the employee will be assigned to the project,
now what I would like to do is getting records by using the id of the employee, the year and month, currently I have a whole date to give to the query but I can extract month and year if necessary. I need a query that by using these infos it fetches the records in which the year and month provided are included in the time period from-to, like if I have employee 3 and march 2022 I get all the projects said employee was assigned to in march 2022.
A project can last more than a year.
An employee could be assigned from 23/05/2022 to 29/08/2022.
I already tried select where fk_employee = 3 and 2022-03-01 between from and to but it clearly wouldn't work if from and to were in random days of the month like 13 and 23
Can you guys help me? I'm not good at all in sql
You can use EXTRACT function
Try:
SELECT ID,
FK_PROJECT,
FK_Employee
FROM tablename
WHERE EXTRACT(YEAR_MONTH FROM 'your_date') BETWEEN EXTRACT(YEAR_MONTH FROM `from`) and EXTRACT(YEAR_MONTH FROM `to`)
Fiddle
SELECT ID,
FK_PROJECT,
FK_Employee
FROM tablename
WHERE DATE > '2020-01-01'
AND DATE < '2022-01-01';
Not that if this does not give you the results you are expecting, the most likely reason is that the DATE field is not defined as a DATE OR DATETIME column in MySql. If this is the case, you can change the column type, or if that is not an option, you can use str_to_date to treat that column as a date column for this query.

Checking Values Stored In A Column In A Date of Birth Format in Database

I am currently trying to get all records in a database that have a date of birth set to a future date. But I am not sure how to check the existing values stored and make sure they are greater than/less than today's date.
Currently all I have is,
select * from [table_name]
where [column_name] = .........;
the formats of the saved dates are DD-FEB-YY and they are stored in a column within a person table.
Use the CURDATE() function to get today's date. This will return all records later than today:
SELECT * FROM table WHERE dateColumn > CURDATE()

SQL extra only the day instead of entire date

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

mysql get current date and row date

Is it possible to do sql query on what day is today and get the row of today for date column?
So let say today is july 25th, i have database table sales, column name date, date = sales transaction date in timestamp.
i need all row that the sales date is same with current date and also is it possible set value as gmt+5?
This will get you all the rows for today's date:
SELECT * FROM sales
WHERE DATE(NOW()) = DATE(DATE_ADD(sales_transaction, INTERVAL 5 HOUR))
ORDER BY sales_transaction
As for GMT +5, Do you mean all rows with a sales date +5 hours or today +5 hours?
EDIT: Updated to add 5 hours to sales date. For a column called date, I would use the back-ticks to indicate it's a column name. e.g. SELECT `date`` FROM sales
I can't figure out how to work the back-ticks on the date field. But you should get the idea. Wrap your column names with `
Give some time and Check out Date Time Function of mySQL
SELECT * FROM tablename WHERE salesdate = '1998-1-1';
If your date is stored in GMT timezone
Select *
FROM tablename
WHERE DATE_ADD(NOW(), INTERVAL 5 HOUR) = salesdate
SELECT * FROM `sales` WHERE DAYOFMONTH(FROM_UNIXTIME(date)) = DAYOFMONTH(DATE(now()))
seems working.
Thank you for all of your replies.

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. =)