Time calculation for time value entered in SQL - sql-server-2008

I need help on calculating my time difference. I search on the forum but not what I need. Here is the code I am using:-
(convert(varchar(10),([RT_Phase_Time])-(convert(time,'00:30:00'))))
So I [RT_Phase_Time] is in this format 'hh:mm:ss'. I am trying to get the difference between ([RT_Phase_Time] - '00:30:00'). Please help!

In MySQL you can easily do that with TIMEDIFF().
TIMEDIFF('22:00:00','00:30:00');
-- Returns '21:30:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff

Try something like this, check the BOL for datediff to convert to other values:
declare #rt_phase_time time
set #rt_phase_time = '12:30:30'
select datediff(ss,cast('00:30:00' as time),#rt_phase_time)

Related

Sequelize how to format sequelize.literal('CURRENT_TIMESTAMP') in a query?

I've been trying to compare a date (yyyy-mm-dd) to sequelize.literal('CURRENT_TIMESTAMP') as shown below:
dataagendado: {
[Op.gte]: sequelize.literal('CURRENT_TIMESTAMP')
}
This works for greater than cases but not when the value of dataagendado is equal to today's date. I believe I need to format the CURRENT_TIMESTAMP data. I've been trying to find an answer but all I get are formatting for when a column is created, which doesn't seem to be applicable inside a SELECT query. I've also tried sequelize.fn('NOW') and formatting it but got no luck.
Am I even on the right track? Any help would be greatly appreciated.
Disclaimer: I'm assuming you are using Postgres as the underlying database. If it's not the case, please post your database engine.
You are using sequelize.literal('CURRENT_TIMESTAMP') which resolves in Postgres to NOW(). The NOW() function returns the timestamp with the current time. If you are comparing your date without the timestamp against a date with a timestamp that shouldn't work.
You could use something like this:
dataagendado: {
[Op.gte]: sequelize.literal('now()::Date')
}
In SQL the following happens:
SELECT NOW() => 2021-03-31T07:39:24.518Z
SELECT NOW()::Date => 2021-03-31T00:00:00.000Z
Thanks to staaar's answer I got enough insight to realize that I should be looking for a function in the database's documentation and not in Sequelize's documentation or threads. I'm using MySql as the database so the current date function I should be using looks like this:
dataagendado: {
[Op.lte]: sequelize.literal('CURDATE()')
}
Here's the official documentation, maybe look for something similar in your database's documentation. Best of luck!
Thanks again to user staaar!

Unable to get Results of a query

I am newbie to SQL.
I have this query
I am not sure where i am doing it wrong.
Can you guys help me out?
thanks.
Three mistakes:
You need single quotes
MySql expects two-digit months
The 2015-09-30 end date includes an implicit midnight for the time component, which excludes most of the day
Put it all together you get this:
SELECT * FROM results WHERE played_on BETWEEN '2015-09-01' AND '2015-10-01';
While I'm here, I prefer to avoid BETWEEN in favor of explicit bounds. There's always that chance someone codes a game for exactly midnight October 1:
SELECT * FROM results WHERE played_on >= '2015-09-01' AND played_on < '2015-10-01';
And you would have had this answer faster if I could have copy/pasted the query text from your post instead of having to re-type. Posting images of sample data or code instead of the text is considered very rude here.
i think you can use MySql Date Function like MONTH() and YEAR() for this query :
SELECT * FROM results WHERE MONTH(played_on) = 9 AND YEAR(played_on) = 2015
I hope this answer can help you.

Created time greater than or equal to

I am trying to write a query which will show all records greater than or equal to a 2013-08-01.
I have tried the following:
SELECT *
FROM v_itdept_allcases
WHERE `Created Time` >= '2013-08-01';
The query returns dates between 30-06-2006 and 23-09-2013.
Not quite sure what else to try as I have tried several variations with no luck.
Gary
As has been mentioned, you're probably hitting a format or string comparison issue. I would suggest converting to an easily comparable monotonic format, such as UNIX_TIMESTAMP(date), when then can be arithmetically modified. You are correct that you shouldn't need to do that, but it's a reasonably bulletproof solution.

Convert MySQL date and time to datetime

I have a table which has 4 columns. Date login, date logout, time login, time logout.
How can I get the difference. I tried something like this;
SUM(TIMESTAMP(date_logout, time_logout) - TIMESTAMP(date_log, time_log))
However I'm getting a really really high number and I am unable to convert it to proper time. Any hints or clues? I didn't design the table btw :P
Thanks in advance!
if you want the diff in secounds:
SUM(UNIX_TIMESTAMP(TIMESTAMP(date_logout, time_logout)) - UNIX_TIMESTAMP(TIMESTAMP(date_log, time_log)))
If you want the diff in days:
SUM(DATEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))
If you want the diff as HH:mm:ss
SUM(TIMEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))
Subtract the two value by using TIMEDIFF FUNCTION.
TIMEDIFF(TIMESTAMP(date_logout, time_logout) ,TIMESTAMP(date_log, time_log) )
TIMEDIFF(TIMESTAMP(date_logout, time_logout) , TIMESTAMP(date_log, time_log))

ADDTIME and SEC_TO_TIME to SQL Server 2008

I try to change my code from MYSQL to SQL Server.
I have some problems with specific functions (ADDTIME and SEC_TO_TIME) in my query.
Here is the end of my query which i have to change :
order by j.idjour,j.heure,ADDTIME(j.heure,SEC_TO_TIME(pl.duree)) asc
I tried to use convert and DateAdd but i am a bit disapointed about how to change it without any error.
Thanks for your help.
This should be what you are looking for,
dateadd(second, pl.duree, j.heure)
Assuming that pl.duree is an integer value representing seconds and that j.heure is a time or datetime value.