I want to include a datediff into my SQL Command in SSIS but I am receiving the error below:
I have done quite a bit of research but cannot seem to get the command to work, tried changing the second to ss, tried include (DT_Date), tried quotations around second and other things but not sure why it#s not working.
Does anyone know what is the correct syntax to pass the query?
SELECT
http.xxx.xxx_at AS xxx_at,
http_xxxxx.xxxxx_at AS xxxxx_at,
DATEDIFF(second, xxx.xxx_at,http_xxxxx.xxxxx_at) As duration
FROM public.xxxxx
FETCH FIRST 100 ROWS ONLY
As suggested by Martin, there is no DATEDIFF command for postgres. The workaround is using DATE_PART:
((DATE_PART('day', created_at::timestamp - completed_at::timestamp) * 24 +
DATE_PART('hour', created_at::timestamp -completed_at::timestamp)) * 60 +
DATE_PART('minute', created_at::timestamp - completed_at::timestamp)) * 60 +
DATE_PART('second', created_at::timestamp -completed_at::timestamp) As Duration
Subtracting one timestamp from another will yield and interval from which you can then extract the seconds:
extract(epoch from created_at::timestamp - completed_at::timestamp) as duration
Related
I'm working in a project and in order to know how much time happened between two dates i'm using the following code:
select (DATE_COMPLETED - DATE_STARTED) as avg_days
The problem is that when I run the code it shows the new column but the format answer is for example:
{"months": 0, "days": 0, "microseconds": 2803077000000}
As you may infer I want that information in days, not in 'microseconds'
By the way, the format of the Date data I'm using is '2021-02-24T02:33:00.000+0000'.
Thanks!
If you get microseconds and you want days, use arithmetic:
select (DATE_COMPLETED - DATE_STARTED) / (24 * 60 * 60 * 1000000) as avg_days
I'm trying a query select with jdbc and spring boot,
I want select informations where my creation date value is LESS than now minus 30.
I tried like this :
SELECT *
FROM informations
WHERE lastTreatment < DATEADD(MINUTE, -30, GETDATE())
I have an error like this :
Caused by: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar
Anyone has idea ? Thanks
You claim Oracle but looks like you write MS SQL Server or MySQL. There is no such function DATEADD in Oracle and neither is there a GETDATE function. To add x days in to a date in just do date+x. In this case you just need a fractional part of a day, so just add an x which yields the need fraction. For 30 minuets that would be 30/(60*24) or (30 minuets/ (60 minuets per hour * 24 hours per day). As for GETDATE just use SYSDATE.
So your query becomes:
SELECT *
FROM informations
WHERE lastTreatment < sysdate-(30/(60*24));
maybe missing ; at the end?
SELECT *
FROM informations
WHERE lastTreatment < DATEADD(MINUTE, -30, GETDATE());
I have a columns called time_spent and it is like: hh:mm:ss. For example, 00:23:11, this means it is 23 minutes and 11 seconds. I wonder how I can add up a multiple of these. Say like 00:23:11 + 00:10:20 = 00:33:31. I am not sure how to directly do that in commands. I have tried convert and CAST, but they ended up failed. Any help would be appreciated.
You can turn your times to seconds using time_to_sec(), sum them, and then turn the result to a time using sec_to_time().
As an example, the following query would compute the total time_spent over the whole table:
select sec_to_time(sum(time_to_sec(time_spent))) total_time_spent
from mytable
This will work even it your times are stored as string, since their format complies to MySQL format for the time datatype.
Note that MySQL time datatype can handle values between '-838:59:59' to '838:59:59' only. If your total time is greater than that, then you will not be able to convert it back to a time.
Once you store dates in DATE, TIME and DATETIME formats there are a multitude of available date and time functions you can use.
You can do it with the function time():
select time('00:23:11', '+00:10:20')
or just:
select time('00:23:11', '00:10:20')
Result:
00:33:31
If the sum may exceed 24 hours, for example when you want to add '23:59:59' to '00:23:11' then use this statement:
select
case
when strftime('%d', datetime('00:23:11', '23:59:59')) = '01' then time('00:23:11', '23:59:59')
else (24 + time('00:23:11', '23:59:59')) || strftime(':%M:%S', time('00:23:11', '23:59:59'))
end
Or:
select
(24 * (strftime('%d', datetime('00:23:11', '23:59:59')) - 1) + time('00:23:11', '23:59:59')) ||
strftime(':%M:%S', time('00:23:11', '23:59:59'))
Result:
24:23:10
I am trying to write report in SQL for a ticket database and i need to filter on the date and time.
Unfortunately the values in the date time fields dont make sense to me.
How can I convert 1473140017153 to a datetime value?
Here are the strings and the dates and time that correspond to them.
1473140017153 06/09/2016 07:33 AM
1473140228660 06/09/2016 07:37 AM
We have no clue what RDBMS you are using, forcing us to basically give you answers for all possibilities:
SQL Server:
DATEADD(ms, col, '19700101')
or
DATEADD(s, col / 1000, '19700101')
Oracle:
TO_DATE('1970-01-01', 'YYYY-MM-DD') + NUMTODSINTERVAL(col / 1000, 'SECOND')
MySQL:
FROM_UNIXTIME(col / 1000)
Postgres:
TO_TIMESTAMP(col / 1000)
If you're using SQLite, you're out of luck :-)
I have managed to get the correct date and time using the following.
DATEADD(hh,2,DATEADD(s, wo.COMPLETEDTIME / 1000, '19700101')) as 'date'
I m using this query to get a result of the difference between the start time and end time of an activity. Where the end time is null i wanted to put the minimum value as 500. Please advice and HELP!!
select * from table
where (end_time - start_time) * 24 * 60 > 1,
IF end_time IS NULL THEN '500';
So this is your query:
select * from table where (end_time - start_time) * 24 * 60 > 1;
But you want to treat a null end_time as 500. So use NVL or COALESCE to replace the null with 500:
select * from table where (nvl(end_time,500) - start_time) * 24 * 60 > 1;
IF end_time IS NULL THEN '500';
Just to make it more clear, '500' is not a number rather a string since it is enclosed within single quotation marks.
Now, end_time is. DATE data type or a timestamp, ideally. So, 500 makes no sense. You must convert it to appropriate type, whether 500 is days, hours, minutes, seconds, fraction of a second.
As in other answer it is suggested to use NVL(end_time, 500), it makes no sense. What does 500 - a date mean? Applying NVL is the need, however, you must convert it to the required value, else those are two different data types and Oracle won't allow it.
UPDATE
In my opinion,
Difference between two dates gives the number of days to the precision of seconds converted back to days. But, difference between an arbitrary number and a date makes no sense.
I assumed that start_time and end_time columns have number as datatype, for this calculation you need to select these specific columns and not all (*). Comparison is in where clause, this works in oracle11.
select ((NVL(END_TIME, 500)-START_TIME) * 24 * 60) from TABLE_NAME where ((NVL(END_TIME, 500)-START_TIME) * 24 * 60) > 1;