ADDTIME and SEC_TO_TIME to SQL Server 2008 - mysql

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.

Related

Find output of MySQL inbuilt function in CLI using custom parameter

I want to see the working of MySQL functions FROM_UNIXTIME() and UNIX_TIMESTAMP() by providing the parameters to them myself in CLI, something like this:
SELECT UNIX_TIMESTAMP(1459460268);
without having to insert these custom values in a table first and then selecting them to see the output.
Thank you for your time :)
UNIX_TIMESTAMP function accepts a date and you are giving it UNIX TIMESTAMP. You can simply run this in MySQL CLI and get the results:
SELECT UNIX_TIMESTAMP("2016-04-01 03:07:48");
SELECT FROM_UNIXTIME(1459460268);
SELECT UNIX_TIMESTAMP();
The last one will return you the current UNIX TIMESTAMP.
Happy coding!

Converting Unix timestamp to actual Date and time

Is there any sql command which I can insert into the stated query so I can convert the timestamp. Although it could be done separately which I have seen so far but I am trying to find something which I can add to the stated query as that would be helpful because I am using other queries to retrieve the data as well. If you any other questions please do mention. Addition: rating_timestamp contains both time and date.
SELECT rating_id,
rating_postid,
rating_posttitle,
rating_rating,
rating_timestamp,
rating_username,
rating_userid
FROM wp_ratings;
In cases of date arithmetic, it is especially important to specify the DBMS you are using - Oracle's math is different from Postgres' math is different from SQL Server's math is different from MySQL's math is...
This assumes that you are using SQL Server. Since there is no built in command to do this conversion, you need to create your own function to do that. The function below takes a UNIX / Linux timestamp and converts it to an SQL Server datetime.
CREATE FUNCTION dbo.fn_ConvertToLocalDateTime (#unixdate BIGINT)
RETURNS DATETIME
AS
BEGIN
DECLARE #UTCTimeOffset BIGINT
,#LocalDatetime DATETIME;
SET #UTCTimeOffset = DATEDIFF(second, GETUTCDATE(), GETDATE())
SET #LocalDatetime = DATEADD(second, #unixdate + #UTCTimeOffset, CAST('1970-01-01 00:00:00' AS datetime))
RETURN #LocalDatetime
END;
GO
I wast sure about about Sql version before. This worked perfectly for me.
FROM_UNIXTIME(rating_timestamp,'%h:%i:%s %D %M %Y')

converting java time to sqldate in query

java datetime (date.getTime()) is stored as string in mysql field.
How can we convert this to sql date using sql query. I am using mysql database.
Is there any sql function available?
For example - This is stored (1416231812348) for today's date in db.
Thanks for suggestions.
Java is returning the date as a long, to convert it you can use:
SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE
If you get an error, try the following (after testing, I can see that your data is stored in milliseconds so you need to use this method):
SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE
(Change event_time to be the field name in your table and MY_TABLE to be the table name.)
Here is a SQLFiddle example that shows it working.
Here is an answer that gives you formatting options as well:
http://notsoyellowstickies.blogspot.co.uk/2011/11/converting-long-into-datetime-mysql.html
There is a java.sql package, that has time included. You can send it straight into your database without needing to convert it.
This may be a more pre-emptive solution than converting a date string from Java, into time in MySQL.
A similar question was answered and may be able to help you out here:
A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
most probably you have recorded from:
System.currentTimeMillis()
so:
select DATE_FORMAT ( from_unixtime( your_table_field / 1000 ) , '%e %b %Y');
you can change the date format as you like.

Time calculation for time value entered in SQL

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)

Efficient way to implement this ActiveRecord Query

I am using MySQL database and I have a datetime field inside it. In my rails application I have to fire a Query similar to the following,
MyTable.all(:conditions=>{my_date.to_date=>"2010-07-14"})
The my_date field is of datatype datetime. I should omit the time and should directly compare it with a date (but I am encountering an error near my_date.to_date due to obvious reasons). How to write an ActiveRecord Query for this scenario?
Thanks.
MyTable.all(:conditions=>["DATE_FORMAT(my_date, '%Y-%d-%m')=?", "2010-07-14"])
EDITED i think it should be
MyTable.all(:conditions=>["DATE_FORMAT(my_date, '%Y-%m-%d')=?", "2010-07-14"])
Here is an efficient solution if you index the date column:
d = "2010-07-14".to_date
MyTable.all(:conditions=>["my_date BETWEEN ? AND ?",
d.beginning_of_day, d.end_of_day)