get data from mysql table as per IST zone - mysql

I am using GoDaddy webserver of shared hosting.I have a mysql database in which there are 2 columns which contains datatype date.. I am using following sql queries. From query1 I am getting Expired records & from query2 getting ongoing records. Problem is because webserver uses it's local time so as per indian time zone I am not getting accurate records. Is there any way to fix problem in query.
Query1
SELECT * FROM offers WHERE startDate <= CURRENT_DATE AND endDate <= CURRENT_DATE
Query2
SELECT * FROM offers WHERE startDate <= CURRENT_DATE AND endDate >= CURRENT_DATE

If your values in database are stored in IST then below should work.
Query 1
SELECT * FROM offers WHERE DATE_ADD(startDate,INTERVAL -330 MINUTE) <=
CURRENT_DATE AND DATE_ADD(endDate,INTERVAL -330 MINUTE) <=
CURRENT_DATE
Query2
SELECT * FROM offers WHERE DATE_ADD(startDate,INTERVAL -330 MINUTE) <= CURRENT_DATE AND DATE_ADD(endDate,INTERVAL -330 MINUTE) >=
CURRENT_DATE

You need to convert the time zones. I assume MySql is using UTC time by default.
SELECT * FROM offers WHERE CONVERT_TZ(startDate,'UTC','Asia/Kolkata') <= CURRENT_DATE AND CONVERT_TZ(endDate,'UTC','Asia/Kolkata') <= CURRENT_DATE;
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz
You need to check time zone tables.
SELECT * FROM mysql.time_zone;
SELECT * FROM mysql.time_zone_name;
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
Mysql uses these tables to convert th time zones. If above tables are empty. You have to fill it.
shell> mysql_tzinfo_to_sql /usr/share/zoneinfo
more info here http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html

Related

sql telling how many days have passed(datediff)

I'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;

SELECT all records that are 30 days old

I need to SELECT all records that are 30 days old. I have the code below but it's not working. In updatestatus I have dates like 12/26/2011. I create a 30 day old date like
$onemonthago="01/01/2012";
$sth = $dbh->prepare(qq(
SELECT *
FROM people
WHERE STR_TO_DATE (updatestatus,'%m/%d/%y')
<= STR_TO_DATE ( "$onemonthago",'%m/%d/%Y')
) );
If the datatype of updatestatus is date:
SELECT *
FROM people
WHERE updatestatus <= '2012-01-01'
or:
SELECT *
FROM people
WHERE updatestatus <= CURRENT_DATE() - INTERVAL 1 MONTH
If the datatype is datetime or timestamp and you want to check the time part, too:
SELECT *
FROM people
WHERE updatestatus <= NOW() - INTERVAL 1 MONTH
You can put an exact datetime instead of the NOW() - INTERVAL 1 MONTH. The correct way depends on how you are storing the datetimes or timestamps (does the Perl code or MySQL creates them in the first place?).
You could also put - INTERVAL 30 DAY which yield slightly different results.
This is what I used. Very simple
$sth = $dbh->prepare(qq(SELECT * FROM people WHERE updatestatus + INTERVAL 30 DAY <= NOW() )) or die $DBI::errstr;
If the time column is in timestamp then use below query.(use from_unixtime function)
SELECT wd.* FROM `watchdog` as wd
WHERE from_unixtime(wd.timestamp) <= NOW() - INTERVAL 1 MONTH
You can try this way. In SQL, there is dateadd function and I think there should be similar function in MySQL.
select *
from Table
where str_to_date between dateadd(day,-30,getdate()) and getdate()
It retrieve records between current date and past 30 days. You need to adjust for time. If you don't count time, you need to remove timestamp.

MySQL select a date between two dates using the time now

I am trying to select a row from a table assuming that the kick off time is within an hour's range of the current time.
The table is just an id and a datetime field.
SELECT * FROM kick_offs WHERE NOW() BETWEEN (DATE_SUB(`time`, INTERVAL 30 MINUTES)) AND (DATE_ADD(`time`, INTERVAL 30 MINUTES))
SELECT * FROM kick_offs WHERE `time` BETWEEN (DATE_SUB(NOW(), INTERVAL 30 MINUTES)) AND (DATE_ADD(NOW(), INTERVAL 30 MINUTES))
These two queries both fail. I'm not really sure why. The server is running MySQL 5.0. What am I doing wrong?
Not sure where you got your SQL from, but this should do it I think.
SELECT * FROM kick_offs WHERE `time` < DATE_ADD(NOW(), INTERVAL 1 HOUR) AND `time`>= NOW()
See:
Display rows from MySQL where a datetime is within the next hour

How to define a date interval in a compatible way?

I am trying to construct the SQL query, that should be executed as is
both on HSQL (v2.2.4) and MySQL (v5.1.36) server (if it can run also on DB2 v9, that would be wonderful bonus!)
The query is:
select count(*) from document where current_date - cast(indexing_date as date) <= ?
(here current_date is a standard HSQL/MySQL function and indexing_date is a column with type datetime, parameter ? is substituted by integer 20 which is the number of days).
The problem is that MySQL returns the difference between dates as between numbers while HSQL returns the difference in days (which is logical when you subtract date from date).
Also HSQL supports this syntax (but MySQL does not):
select count(*) from document where cast(indexing_date as date) between current_date - 20 day and current_date
while MySQL does not. I am aware about DATEDIFF() in MySQL, but as I said the solution should be inter-operable.
HSQLDB also supports this:
select count(*) from document where current_date - cast(indexing_date as date) <= cast(? as interval day)
and
select count(*) from document where cast(indexing_date as date) between current_date - '20' day and current_date
or
select count(*) from document where indexing_date >= current_date - interval '20' day
Also, from version 2.2.6, HSQLDB supports DATEDIFF(datevaluea, datevalueb), which returns the number of days between the two dates, as well as DAYS(datevalue), which returns the day number since the epoch.
db2:
SELECT COUNT(*)
FROM document
WHERE DATE(indexing_date) BETWEEN DATE(DAYS(CURRENT DATE) - 20) AND CURRENT DATE

MYSQL query / dates older than 1 week ago (all datetimes in UTC)

How do I query a mysql db to return all records with a datetime older than 1 week ago. Note that the datetime table stores everything in UTC, and I should be comparing it in that itself.
Just to be clear - I'm looking for a pure mysql query.
SELECT * FROM tbl WHERE datetime < NOW() - INTERVAL 1 WEEK
If your table stores datetimes in different timezone than what NOW() returns, you can use UTC_TIMESTAMP() instead to get the timestamp in UTC.
SELECT * FROM table WHERE DATEDIFF(NOW(),colname) > 7;
SELECT SUBDATE('2008-01-02', 7);
OR
SELECT SUBDATE(now(), INTERVAL 1 week);
Result:
2007-12-26