Extract number from the date in pl sql - 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'

Related

Replacing day part in Date with another number in PLSQL

i am trying to replace a day part in a date value. say for example
select TRUNC(SYSDATE) from dual;
Result: 28/11/2014
I wanted to replace only the 28 with another number value(X). So i can arrive the result like X/11/2014.
Can you please help me?
Thanks in Advance,
Murugan.
trunc(sysdate,'MM') + (x-1)
would do it. trunc(sysdate,'MM') returns the first of the month. Then you add however many days you want to get the date that you want.
SELECT TO_DATE('01' || SUBSTR(TO_CHAR(SYSDATE, 'DD/MM/YYYY'),3))
FROM DUAL
WHERE TO_CHAR(SYSDATE, 'DD') = '25';
If it is a character then you can use below query:
SELECT 'X'||SUBSTR(TRUNC(SYSDATE),3) FROM DUAL;
If you want to replace with some other number then:
SELECT TO_DATE(21||SUBSTR(TRUNC(SYSDATE),3),'DD-MON-YY') FROM DUAL;

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

Oracle query concatenate date

I have the following query:
select *
from mytable
where to_char(mydate,'mm/dd/yyyy') between ('05/23/2013')
and ('06/22/2013')
I need to change it to make dynamically so that I won't modify it every month from 05/23/2013 to 06/23/2013 for example:
('05/23/' + (select to_char(sysdate, 'yyyy') from dual))
but this is giving an error. Any suggestions?
What I need to do: Every month I need to run this query to get the records between 23rd of this month and 23rd of the last month.
Oracle uses || as the concatenation operator:
('05/23/' || (select to_char(sysdate, 'yyyy') from dual))
BTW, David is right. If you really want to compare string representations of dates (but why?), use a date format that is ordered the same way as dates:
to_char(mydate,'yyyy/mm/dd')
You're performing a comparison on strings, not on dates, so your code doesn't work the way you think it does.
Based on the string logic, "05/23/2000" is between "05/22/2013" and "06/24/2000".
Keep the data types as date and Oracle will get the comparison right.
Possibly what you want is:
select *
from my_table
where mydate >= add_months(trunc(sysdate,'MM'),-1)+22 and
mydate < trunc(sysdate,'MM')+22
but it's difficult to tell without a description of what the requirement actually is.
How about this one?
SELECT '(''05/23/'''||to_char(sysdate, 'yyyy')||'''' FROM DUAL
Have not testet because I have no Oracle database right now, needs checking for quote escapes...
Do you need exact days from the month? You can also substract days from sysdate:
SELECT (sysdate - 30) FROM DUAL
You may also use concat function
concat('05/23/', (select to_char(sysdate, 'yyyy') from dual))

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 time part from TimeStamp column in ORACLE

Currently I'm using MyTimeStampField-TRUNC(MyTimeStampField) to extract the time part from a timestamp column in Oracle.
SELECT CURRENT_TIMESTAMP-TRUNC(CURRENT_TIMESTAMP) FROM DUAL
This returns
+00 13:12:07.100729
This works OK for me, to extract the time part from a timestamp field, but I'm wondering if there is a better way (may be using a built-in function of ORACLE) to do this?
What about EXTRACT() function?
You could always do something like:
select TO_DATE(TO_CHAR(SYSDATE,'hh24:mi:ss'),'hh24:mi:ss') from dual
I believe this will work with timestamps as well.
You want just date then use
to_char(cast(SYSDATE as date),'DD-MM-YYYY')
and if you want just time then use
to_char(cast(SYSDATE as date),'hh24:mi:ss')
the parameters are making all the changed
'DD-MM-YYYY'
and
'hh24:mi:ss'
This may help:
Select EXTRACT(HOUR FROM (SYSDATE - trunc(sysdate)) DAY TO SECOND ) From dual;
select TO_DATE(TO_CHAR(SYSDATE,'hh24:mi:ss'),'hh24:mi:ss') from dual
This gives the timestamp for 1hour less than the actual.
select hour(CURRENT_TIMESTAMP)