mysql get current date and row date - mysql

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.

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.

Get price from mysql between two dates

In mysql database I have table like this:
StartDate EndDate Price
01.01.2017 01.06.2017 100
02.06.2017 01.12.2017 150
What is the best solution to get price between some days. For example if I send dates 25.05.2017 as start date and 05.06.2017 as end date I need to get sum of price between these dates.
Try this
select sum(Price) as total_price from table_name
where StartDate >= 'given_startdate' and EndDate <= 'given_enddate'
To find overlapping periods use following logic:
where start_1 <= end_2 and end_1 >= start_2
in your case:
where startdate <= date '2017-06-05' and enddate >= date '2017-05-25'
Edit:
Seems like you want to expand the date range to one row per day. Join to a calendar table (if you don't have one, yer, create it, you will used it in many places):
select sum(price)
from mytable join calendar
on calendardate between startdate and enddate

How to compare a date when the date is stored as a varchar

The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'

Select Range of Items by Year and Month

I'm attempting to create a select statement which gets items by year and month.
This is what I have so far:
SELECT * FROM sales WHERE YEAR(Date) = 2013 AND MONTH(?) = 'June'
I can't merely select date ranges because different months have different number days. It would be ideal to select months by their number (ie, January being 1) or a similar approach.
How is this worked out in a mysql statement?
The fields are datetime fields such as 2012-12-01 00:00:00
Have a look at the performance and write your condition as
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-07-01 00:00:00'
for the example month: June of 2013
so MySQL can use an index on the column date. You will get exactly all rows with a date in the June of 2013, even those in the first second, but not those in the first second of the July of 2013.
You see, that you don't need to know the number of days of the particular month, because you will ever use the first of both months.
You could use a bit of date calculation too:
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-06-01 00:00:00' + INTERVAL 1 MONTH
so you need only to know the start and the length of the interval.
Note
The most important part of my answer is to use the column Date without using a function on this column, so MySQL can use an index.

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.