Retrieve number of the day sql query - sql

I wan't to retrieve the number of the day using an SQL request but I found that I retrieve the number of day since the 01-01-4712 using this query:
SELECT TO_CHAR(TRUNC(SYSDATE),'J') FROM DUAL;
Is there any other query I may use?

Use to_char() to convert your date. Format mask 'DDD' returns the day of the year.
select to_char(sysdate, 'DDD') from dual;
select to_char(to_date('1/1/2014', 'MM/DD/YYYY'), 'DDD') from dual;
Some more info on various formats: http://www.techonthenet.com/oracle/functions/to_char.php

After investigations I found that we can use this query to obtain the number of day in the current year:
select to_date(sysdate) - trunc(sysdate,'YYYY') from dual

Related

How to display month in different formats

When i perform this query
SELECT CURRENT_DATE FROM DUAL;
I get current date with month in shortcut format (NOV). How can i get the same result but getting month in full format(NOVEMBER) and in numerical value
I know there are built in formatting options in MSSQL, and assume there are in Oracle. A quick google gave me this website that shows how:
https://www.oracletutorial.com/oracle-basics/oracle-date/
SELECT
TO_CHAR( SYSDATE, 'YYYY-MM-DD' )
FROM
dual;

I am using Oracle SQL Developer and want to extract the days of the week from timestamps. Am I on the right track with the syntax?

SELECT DAYOFWEEK(TIMESTAMP('07-JUN-21 12.00.00.0000000 AM'))
--or
SELECT
EXTRACT (DAYOFWEEK
FROM
{'07-JUN-21 12.00.00.0000000}) as dayofweek,
FROM table_name
I think you are better off using TO_CHAR:
SELECT TO_CHAR(CURRENT_TIMESTAMP, 'DAY')
FROM DUAL;
If you don't want the full day name, you can use DY or another format model. All of which are listed in the Oracle Documentation Link

Oracle Julian day of year

how can I select Julian day of year in Oracle database?
I tried:
select to_char(sysdate, 'J') from dual;
Which gives me the number of days since January 1, 4712 BC. But I would need the number of days since 1.1. of current year.
If you check the TO_CHAR (datetime) documentation you get a link to "Format Models" with a comprehensive list of available formats. I guess you want this:
DDD Day of year (1-366)
SELECT TO_CHAR(SYSDATE, 'DDD') from DUAL;
One way would be to use:
select sysdate - trunc(sysdate,'yyyy') from dual
'Trunc' cuts everything except the year and returns 01/01/2014, subtracted by the sysdate returns numbers of days since 1st of january.
Use sql select trunc(sysdate)+1 - trunc(sysdate,'yyyy') from dual. you will get an even number

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))

In oracle SQL, how would you obtain the timestamp representing the start of the week?

I'm using an Oracle 9i database and want to obtain, within a function, the timestamp representing the start of the week, i.e. The most recent monday, at 00:00:00.
I am aware that the timestamp representing the start of the current day is TO_TIMESTAMP(SYSDATE).
You can use the function next_day to get that:
SQL> select next_day(sysdate-7, 'MONDAY') FROM DUAL;
NEXT_DAY
---------
29-APR-13
Getting the start of the week should work with trunc (see docs).
So,
select to_timestamp(trunc(sysdate, 'D')) from dual
should work.
However, depending on your NLS settings, the first day of the week for oracle may well be Sunday.
this appears to return Monday before the day of week in question at midnight. to prove out just play around with sysdate and subtract days...
select case when to_Char(sysdate,'d') = 1 then
trunc(sysdate-6)
else
trunc(sysdate - (to_Char(sysdate,'d')-2))
END
from dual;
You can truncate a date to Monday with:
select trunc(sysdate, 'IW') FROM DUAL;