Select a specific time in DB2 from the currenttime - sql

I am trying to do a select and set the time to 10AM in DB2. Ive tried
SELECT (trunc(sysdate) + 10/24) FROM sysibm.sysdummy1;
however this sets the time to 0:00:00, im trying to get it set to 10AM, is there a way to do this in DB2?

Please can you try this:
SELECT trunc(sysdate) + 10 hours FROM sysibm.sysdummy1;
Here is the DEMO
Also, in your question example you have one open bracket too many.
it should be: SELECT (trunc(sysdate) + 10/24) FROM sysibm.sysdummy1
Cheers!

Try below query.
SELECT DateAdd(Hour, 10, sysdate) FROM sysibm.sysdummy1;

Related

How to get an hourly output for 24 hours oracle

Hi everyone im very very new to messing with oracle but I have to figure out how to somehow loop a query to get an hourly output on a 24 hour interval. I dont know how to declare that the time should start whenever SYSDATE is and then to add hour hour each to that.
For example something like:
Declare the system time here
Add an hour for the start time until 24 hours is complete. Do i use loop while for that so it automatically generates without having to put in new query everyday?
Not sure what are you expecting, but here is sql that will give you 24 hours (records) starting with the hour of Sysdate. You can use it as a cursor to loop through or something else....
Select
To_Char(SYSDATE, 'hh24') "HOUR_START",
CASE
WHEN To_Number(To_Char(SYSDATE, 'hh24')) + LEVEL-1 > '23' THEN
LPAD(To_Number(To_Char(SYSDATE, 'hh24')) + LEVEL-1 - 24, 2, '0')
ELSE
LPAD(To_Number(To_Char(SYSDATE, 'hh24')) + LEVEL-1, 2, '0')
END "HOURS_24"
From
Dual
CONNECT BY LEVEL <= 24
No need to use a loop or a cursor within a PL/SQL block, but using a clean SELECT statement containing XML techniques such as
SELECT TO_NUMBER(TO_CHAR(sysdate, 'hh24')) AS hour_start,
XMLCast(column_value AS INT) AS hours
FROM XMLTable('1 to 24')
ORDER BY CASE WHEN SIGN(hours-hour_start)>=0 THEN hours ELSE hours+24 END
Demo

How to select last 7 days from data from today's date?

I know are many ways to get what I searching, actually I found the solution here on the forum, but the problem is that I don't really know why is not working, this is my try:
select * From ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas
Where ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas.Fecha_de_modificacion >= dateadd (day, -7, GetDate());
(where Fecha_de_Modificacion is the field with the date of the record)
When I execute this query I get next error:
"Column 'day' does not exist"
Do you know guys, who is this happening? or if there are other method to get what I want?
Thanks by the way, have a nice day,
Use interval:
ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas.Fecha_de_modificacion >= current_date - interval '7' day
Try this:
select * From ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas
Where ModeloBI.CORP.T_LOGI_RN_Planeacion_Entregas.Fecha_de_modificacion >= dateadd(dd,-7,getdate());

Adding 1 year to the sysdate

I am trying to write a stored procedure to print the last day of month exactly next year
I am getting this years last date, but I want 30/8/2014
I already tried the following:
IF LAST_DAY( add_months( SYSDATE, 12 )-1)
But it didn't give me any output.
Why are you using -1? The following would work:
select last_day(add_months(sysdate, 12)) from dual
You might be getting no output if you're running this in SQL*Plus and printing is not enabled. Try:
set serveroutput on
If your requirement is 8/30/2014 use the following
select last_day(add_months(sysdate, 12))-1 from dual
If the requirement is last date of the month you can use Ben's Answer
Try this one
SELECT add_months(LAST_DAY(SYSDATE),12) as NextYearDate from Dual
May this will help you.
SQL Fiddle Demo

Optimizing SQL Query and Dynamically using current date

I am trying to optimize a simple SQL query and was wondering if anyone has any suggestions. I am developing using Oracle SQL Developer (which I don't like) on an Oracle 11g database. The query I am using is:
SELECT count(*)
FROM my_table
WHERE my_date
BETWEEN TO_DATE('2012-5-09T05.00.00','YYYY-MM-DD"T"HH24:MI:SS')
AND TO_DATE('2012-5-10T04.59.59','YYYY-MM-DD"T"HH24:MI:SS')
AND my_code='33'
GROUP BY my_code;
Also, I want to be able to use this query dynamically by changing the part of the date to be whatever the current date is, but I want to be able to specify the hour. So I want to be comparing something like:
getdate() + 'T05.00.00'
I have no idea how to do this and the getdate() function doesn't seem to work in SQL Developer/I don't know how to use it correctly.
So what I'm looking for is optimization suggestions and pointers on how to just dynamically change the day-month-year part of the date I want to constrain my results to. Thanks!
To get current date, you can use SYSDATE. To add x number of hours to it, you can add x/24. So something like this:
Example: Get current date + 5 hours
SELECT SYSDATE + 5/24 FROM dual
So in your example:
SELECT count(*)
FROM my_table
WHERE my_date
BETWEEN sysdate
AND sysdate + 5/24 -- if you want 5 hours ahead, for example
AND my_code='33'
GROUP BY my_code;
If you want to be able to change the number of hours, you could make this code into a function, and pass in the hours and code as variables.
Something like this:
CREATE FUNCTION myfunc
(
p_num_hours INT
, p_my_code VARCHAR
) RETURN INT
AS
l_ret INT;
BEGIN
SELECT count(*)
INTO l_ret
FROM my_table
WHERE my_date
BETWEEN sysdate
AND sysdate + p_num_hours/24
AND my_code=p_my_code
RETURN l_ret;
END;
As an alternative to adding fractional days via expressions such as "5 / 24" you might want to use an INTERVAL constant. For example:
SELECT count(*)
FROM my_table
WHERE my_date BETWEEN (TRUNC(SYSDATE) + INTERVAL '5' HOUR)
AND (TRUNC(SYSDATE) + INTERVAL '1' DAY +
INTERVAL '5' HOUR - INTERVAL '1' SECOND) AND
my_code='33'
GROUP BY my_code
I like to use INTERVAL constants because it's quite clear what these constants represent. With the fractional-day constants I sometimes get confused ('course, I sometimes get confused, regardless... :-)
Share and enjoy.
If I understand correctly, something like
select count(*)
from my_table
where trunc(my_date) = trunc(sysdate)
and my_code = '33'
group by my_code;
or
select count(*)
from my_table
where my_date
between sysdate and sysdate + 5/24
and my_code = '33'
group by my_code;
HTH.
Alessandro

Extract number from the date in pl sql

Does anyone know if it is possible to take the date from a random date in pl sql.
example.
SELECT SYSDATE FROM DUAL
and here the output would be say : 26-10-2010 13:30:34
Now I want to have just the date as a number. In this case that would be 26.
Or is there some sort of function like IsNum that can recognize it for me. So I can just take 26 and leave the rest out.
You can also use EXTRACT(), like so:
SELECT EXTRACT(DAY FROM SYSDATE) AS DAY FROM DUAL;
select to_char(sysdate,'DD') as day from dual
You can use
to_char(SYSDATE, 'DD')
More you can read here: LINK
All format models are described in the official SQL Reference, take a look in case you need something else
select count(*) from
(select sysdate+rownum dates from dual connect by rownum < (sysdate+15)-(sysdate))
where to_char(dates,'D') <> '1' and to_char(dates,'D') <> '7'