I have a database with the following structure and I am trying to get all rows from this table based on passing both the month and year using a where on the timestamp column (this will be a unix standard timestamp)
e.g month - 3, year - 2018 // get all rows for March 2018 only.
// db structure
id, timestamp, post_title
If you want rows for a given month using a unix timestamp, I would recommend:
where timestamp >= unix_timestamp('2018-03-01') and
timestamp < unix_timestamp('2018-04-01')
If you are passing in a variable, I would recommend passing in the first day of the month and doing:
where timestamp >= unix_timestamp(?) and
timestamp < unix_timestamp(? + interval 1 month)
Use convert function
SELECT * FROM dbo.YourTable WHERE CONVERT(VARCHAR(5),DATEPART(mm,timestamp))+'-'+CONVERT(VARCHAR(5),DATEPART(yy,timestamp)) ='3-2018'
I guess this can help you.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Related
I'm trying to retrieve some data from a database using MySQL. I would like to SELECT just the records that I uploaded in the last month and not the previous ones. This script has to be dynamic : I mean that on 1st of February it should retrieve data from 1st of January to 1st of February and so on.
My table structure is really simple : the upload date is stored in the column 'reg_date' that is a TIMESTAMP type.
Does anyone know how to do this in a single query? Thanks!
This can all be done in a single SQL query using the CURDATE() and INTERVAL functions so no PHP date calculation is required.
SELECT *
FROM `TABLE`
WHERE `reg_date` BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()
You can filter the data like so:
select *
from your_table
where reg_date between date_sub(curdate(), interval 1 month) and curdate()
just use this query:
select * from your_table_name where reg_date between '2017-03-01' and '2017-03-31';
I need to only SELECT data where the date field is yesterdays date. The only problem I'm having is that the data in the date field looks like the following 20160412 062815.000
I don't really care about the time, I just want to search dynamically for anything with yesterdays date. I've tried a multitude of CURDATE() -1 but I'm unsure how to just search the first 8 digits of the field.
Assuming the date value is stored as a string, and the first 8 characters are always the date in YYYYMMDD format, then you can use a query like this:
select *
from your_table
where your_column like concat(date_format(current_date() - interval 1 day,'%Y%m%d'),'%')
One advantage of this query is that it can leverage an index on your date field, unlike the other answers so far.
Format yesterday's date to a number and convert the date string also to a number.
select * from your_table
where date_format(curdate() - interval 1 day, '%Y%m%d') * 1 = date_col * 1
SQLFiddle demo
*1 is a math operation that forces MySQL to convert strings to a number.
you can use subdate(currentDate, 1)
select * from your_table
where subdate(currentDate, 1) = DATE(your_date)
I wanna get all my articles out of my database where the date of the last update is greater than the publishing date + 1 week.
Do you habe an idea how that should work?
In my table there is the publish_date and the update_date. Both of them contain a datetime in the following format: Y-m-d H:i:s
It should be something like the following (which does not work!)
SELECT * FROM foo WHERE update_date > publish_date+1week
SELECT * FROM foo WHERE update_date > DATE_ADD(publish_date, INTERVAL 1 WEEK)
See MySQL DATE_ADD.
If you want to retain your structure (i.e. not use a DATE_ADD function), I would use:
SELECT * FROM foo WHERE update_date > publish_date + INTERVAL 1 WEEK
You can use DATEADD() function to add weeks to DateTime in Sql Server. DATEADD() functions first parameter value can be week or wk or ww, all will return the same result.
SELECT GETDATE() 'Today', DATEADD(week,1,GETDATE())'Today + 1 Week'
I am trying to select records from a database only if they match today's date. The format for the date in the database is 2012-06-20 9:30:00 I am using the statement SELECT id FROMnewsreportsWHERE DATE(newsdate) = CURDATE() but it doesn't not return any records for today?
Screenshot of column with dates
http://img135.imageshack.us/img135/8399/2347f03df0394cd898c7fc5.png
DATE_FORMAT(NOW(),'%Y-%m-%d') = FORMAT_DATE(NOW(newsdate), '%Y-%m-%d')
Or better:
DATE(newsdate) = DATE(NOW())
The best way is to store the additional column with 2001-09-11 date format and compare this one
Example, thanks to #Conrad Frix
It looks like the curdate function is going to give you something different than the date format you've got in the database.
Take a look at the docs here
MySQL Date and Time Functions
It's supposed to return a date in this format:
2008-11-11
So, you could either search for a date range between curdate() and curdate() + INTERVAL 1 DAY (untested), or store the dates in the curdate() format.
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.