Using Hour and Minutes in Comparison - mysql

I have a table that has date and hours and i am looking to get in my output > > all with a particular date and hour and minutes of that day.
A particular person on the table can have a date_added of 10/12/2016 9:38:04 PM
I want to pull all who have a date added of 10/12/2016 after 9:25:00PM. I have tried different things but nothing worked, see below
s.date_added >= '2016-12-10 9:25:04 PM'
s.date_added >= '12/10/2016 09:00:00 AM'
convert(s.date_added) >= '10/12/2016 9:25:04 PM'.
I am using sql. I just want a code that will give me all those on a table that have a specific date added on the date added column. I am not trying to format, just get an output

set date to variable datetime before
declare #dateParam datetime
set #dateParam ='2016-12-10 9:25:04 PM'
s.date_added >= #dateParam
s.date_added >= #dateParam
example comparison with just hour
if datepart(hh, #dateParam) >= datepart(hh, s.date_added)
print 'True'
else
print 'False'

Related

MySQL searching timestamp columns by date only

I am building out a query to search a table by a timestamp column value. An example of the format I am passing to the api is 2018-10-10. The user has the ability to select a date range. Often times the date range start date is 2018-10-10 and end date is the same day, 2018-10-10. The below doesn't seem to do the trick. What is the simplest way to accomplish this without having to specify the time? Obviously, I'd like to query for the entire day of 2018-10-10 from start to end of day.
SELECT
count(*)
FROM
contact
WHERE
created_at >= '2018-10-10'
AND created_at <= '2018-10-10';
The problem here is that Timestamp datatype will have HH:MM:SS (time) values also. While comparing a datetime with date, MySQL would automatically assume 00:00:00 as HH:MM:SS for the date value.
So, 2018-10-10 12:23:22 will not match the following condition: created_at <= '2018-10-10'; since it would be treated as: 2018-10-10 12:23:22 <= '2018-10-10 00:00:00, which is false
To handle this, you can add one day to the date (date_to in the filter), and use < operator for range checking.
SELECT
count(*)
FROM
contact
WHERE
created_at >= '2018-10-10'
AND created_at < ('2018-10-10' + INTERVAL 1 DAY);

UPDATE sql to change time only in datetime

I want to UPDATE time only from datetime using sql but it seem cant work. the update will get from user and only change the time.
example 2018-10-06 08:00:00 update to 2018-10-06 12:00:00 (time that user enter)
$sql3="UPDATE course
SET date_start = '$date_start'
WHERE date_start = SUBSTRING(date_start,11,17)
AND CourseNo = '$id1' ";
$hasil3=mysql_query($sql3);
if($hasil3==false)
echo "SQL error:".mysql_error();
else
{
?> <center>
<script language="javascript">
alert("Successfully update!");window.location="studentTimetable.php";
</script>
You can use an expression such as:
select date('2018-10-06 08:00:00') + interval 12 hour
You can also use addtime() if you want to add hours/minutes/seconds.
This should be simple enough to put into an update statement if that is what you want to do.
If I understand you right, you get an input as string in the format <hours> ":" <minutes> ":" <seconds>, that represents a time of the day. You want to replace the time of the day portion of a datetime column with that given time of the day.
To do so you can first downcast the column to a date and then upcast it again to a datetime. That way the time of the day portion becomes 00:00:00. Now use date_add to add the time of the day the user has given to you. Since the time of the day was zeroed before that will result in a datetime with the time of the day as the user put in.
UPDATE elbat
SET nmuloc = date_add(cast(cast(nmuloc AS date) AS datetime), INTERVAL '12:00:00' HOUR_SECOND);
I had a similar problem to this..
I have a number of dates that are not quite right by a few seconds. They should all end on an Hour, i.e 00:00
Work out how much time I need add (I know i want to add some seconds)
SELECT 60 - DATEPART(SECOND, EndDate), e.*
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
Now I can write an update to correct all the dates that are slightly off.
UPDATE Events
SET EndDate = DATEADD(SECOND, i.Seconds, i.EndDate)
FROM (
SELECT Id, 60 - DATEPART(SECOND, EndDate) AS Seconds, EndDate
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
) i
WHERE
i.ID = Events.ID

T-SQL DateDiff between the times but 1 datecolumn is 1900

I have a column that pulls in two dates, 1 of the dates has the correct date and time so I can easily compare the number of minutes between this date and GetDate() but some values have the date 01/01/1900 and then the time i need to use.
How can I do datediff but ignore the date and only use the times?
So
My date | 01/01/1900 14:25:00
GetDate() | 06/02/2014 14:26:00
Would give me 1 minute
Convert your datetime to the time data type and then everything works as expected.
Ex:
Declare #d1 DateTime,
#d2 DateTime
Select #d1 = '01/01/1900 14:25:00',
#d2 = '06/02/2014 14:26:00'
Select DATEDIFF(minute, Convert(Time, #d1), Convert(Time, #d2))
The time data type was added in SQL2008 (which you have tagged in your question).

What time does a SQL query like this one start and end?

I am using a SQL query like:
SELECT * FROM game_list
WHERE start_date <= DATE(NOW()) AND end_date >= DATE(NOW())
ORDER BY game_id DESC;
Now, what time will this actually start and end? I mean I know on what date but what will the time be, is it midnight, 12 am, pm or what?
If i understand correctly you want to show a game only if current time is between start time and end time,in that case what you need is actually:
SELECT * FROM game_list
WHERE DATE(NOW()) >= start_date AND DATE(NOW()) <= end_date
ORDER BY game_id DESC;
This way it should work properly
The only problem i could see is if you don't format your start_date and end_date correctly.
If your value is full time stamp,you should simply use NOW() or CURRENT_TIMESTAMP directly as it contains both date AND time
If your start_date is year month day eg: 2012-05-12 you should use CURDATE()
If your value is simply a day number,like 1...2...3...4..etc you should use DAY()
I would doublecheck what start_date returns and decide accordingly,for reference i would take a look here date and time in mysql
If you are asking how the DATE function works: it simply extracts the date part of a datetime.
see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31
This represents simply that day. Considered as datetime again it would become midnight(2003-12-31 00:00:00)
I think you want something like following
SELECT * FROM game_list
WHERE start_date <= DATE(NOW()) AND end_date >= DATE_SUB(NOW(), INTERVAL 1 DAY)
ORDER BY game_id DESC;
Above query fetch the records which starts before today and having end date greater than tomorrow's date

Select mysql query between date?

How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >= and <= :
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
You can use now() like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'