Select data based on timestamp and interval from mysql - mysql

In joomla, it has jos_session table in which time is stored as unixtimestamp in 'time' column.
I want to select the records which are older then a week or two days, basically any interval.
Not much hands on DB, but i did tried with date_sub, but it seems taking date as an argument. So I also tried using FROM_UNIXTIME to convert, but nothing seems to be working.
The last query I tried was
SELECT username FROM jos_session WHERE DATE_SUB(FROM_UNIXTIME(time,'Y-m-d'), INTERVAL 2 DAY );
But it seems to giving empty set and many warnings!
Can anyone please help!
Thanks in advance,
Tanmay

Try this. It should work:
SELECT
username
FROM
jos_session
WHERE
TO_DAYS(FROM_UNIXTIME(CAST(timeAS UNSIGNED))) < TO_DAYS(FROM_UNIXTIME(UNIX_TIMESTAMP()))-2

Related

Selecting from date range mysql/phpmyadmin problem

For my current project I'm using XAMPP. I have a problem on pulling data from a date range, sorry for the messy title I don't know how to word it out. The problem is I'm pulling dates from 06/01/2020 to this day 06/16/2020 however it doesn't display the data for today, but when my end date is 06/17/2020 (which is tomorrow at least in my country) it appears. Thank you in advance!
Did you use only greater or less >,< operators or also with equal values <=,>=?
You can also use BETWEEN function in your query.
WHERE date BETWEEN CAST('2020-06-01' AS DATE) AND CAST('2020-06-16' AS DATE);
Which is equivalent to:
WHERE date >= CAST('2020-06-01' AS DATE)
AND date <= CAST('2020-06-16' AS DATE);
I've got it, I used SELECT * FROM data WHERE date(date) BETWEEN '2020-06-01' AND '2020-06-16'; if someone's look for this too, please try this out :)

MySQL: update table values with DATE FORMAT %d-%m-%y TO %y-%m-%d

During a database import, I kinda messed things up. So now I have a date column that is of DATE type, but the year and day are in the wrong places: the day ended up appearing in the year column and MySQL has auto formatted it as year further worsening things.
I have values in a table like below:
dates(dddd-mm-yy)
2030-05-12
2021-06-13
2012-12-13
I want them like so:
dates(yyyy-mm-dd)
2012-05-30
2013-06-21
2013-12-12
Please note that I actually want to UPDATE the column, I have seen and tried many examples on SELECT.
Thanks in advance.
You can use substring() and concat() to concat whatever you want. Below is the statement for your reference(table is tb_test, the column name is col_date).
update tb_test set col_date = concat('20',substring(col_date,7,2),'-',substring(col_date,4,2),'-',substring(col_date,1,2));
I dont know if this can be classified a "solution", maybe more of a work around, but it worked flawless for me.
After reading this, i realised that my problem started when i tried to import my dates in the format dd-mm-yy, Hence Mysql assumed my day to be year and presented my dates as yydd-mm-yy
My dates were formatted as yydd-mm-yy and i have severally tried the query
UPDATE table_name
SET DATES = DATE_FORMAT(DATES, '%Y-%m-%d');
This query ran but made no changes to the Table because in the eyes of Mysql it has helped me format my dates as yyyy-mm-dd right from when i imported my DB, but still my dates are in yydd-mm-dd
So i thought, what if i try a query like;
UPDATE table_name
SET DATES = DATE_FORMAT(DATES, '%d-%m-%y');
This way Mysql switched the days and years then provided my dates as yyyy-mm-dd, quite a confusing thing the way Mysql handles dates but the end result of a date like 2030-05-12 is 2012-05-30.

MySQL query to search in between two time range between two dates using timestamp data

I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)

Mysql between query not working

I am using mysql. I want to display records between two dates.
I searched through web and found that mysql's date format is yyyy/mm/dd.
so I am writing query as follows,
select
*
from tbl_reservation
where
current_date between '2014-03-28' and '2014-03-26';
But, I don't know why it is not working. "col_date" has DATE datatype. I am not storing time in it.
Let me give you some idea what i have done at back end,
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date curr_date=new Date();
String compareDate=sdf.format(curr_date);
And I am storing this value in "col_date". Is it happening because of this processing?
Thanks for your valuable time in advance.
With BETWEEN, use least date first, latest date next
It should be:
select * from tbl_reservation
where col_date between '2014-03-26' -- least date
and '2014-03-28'; -- latest date
Write lower date value first.
SELECT * FROM tbl_reservation WHERE col_date BETWEEN '2014-03-26' AND '2014-03-28'
I have set column name as current_date. Actually it is a function in mysql. So i can not use current_date as column name. Thanks I learned something new today. Thank you Masters.

how to get the data using from date and to date in sql server?

i need to retreive data from database with the condition from date to to date using between query,
my query is,
select * from Master where Date between '01-08-2013' and '30-08-2013'
but it retreive all data from the table...
i need only data with in that date..
i tried another one like,
select * from PatientMaster where EntryDate >= '01-08-2013' and EntryDate<= '30-08-2013'
how its posible..
whats wrong with my query...
sorry im very bad in english...
thank you in advance...
A date string has the syntax YYYY-MM-DD and not DD-MM-YYYY
select * from Master
where `Date` between '2013-08-01' and '2013-08-30'
for that you can use
select * from Master where Date >='01-08-2013' and dateadd(dd,1,'30-08-2013')
You have to convert your strings to dates. This page shows you how to do it in mysql, which is what you have tagged. For sql server, which is in your subject line, use this page.
Then you do a slight modification of your 2nd attempt. Instead of
and EntryDate <= the end date
you want
and EntryDate < the day after the end date
That takes care of any time components. It might not matter in your case, but it's a good habit to get into.
You'll be looking for an query that works with your format? (dd-mm-yy)
CAST to the desired format!
http://www.w3schools.com/sql/func_convert.asp
105 = dd-mm-yy
SELECT * FROM Master
WHERE CONVERT(date, Date, 105) BETWEEN '01-08-13' and '30-08-13'
Be conscious with regards of the choice of data type for date Columns,
with or without time, day or year first etc. and please do not use varchar
for dates...
know that it CAN be confusing to call a date column for only Date...
be consistent with high/lower case.