Oracle query concatenate date - sql

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

Related

How to check whether some date is in the current calendar month or not in oracle DB

I need to print records which have the current calendar month as the enrolled month.
I used sysdate for this function. But, I'm not sure if that is correct or not.
My code would be,
select * from student where sysdate,trunc('mm') = enrolled_month;
I just need some way to print current month using sysdate function.
Any kind of help will be appreciated.
Thanks in advance.
I believe you need EXTRACT function:
select *
from student
where enrolled_month = extract(month from sysdate);
Here is another option using TO_CHAR:
SELECT *
FROM student
WHERE TO_CHAR(sysdate, 'Month') = enrolled_month;
But I don't think it's a good idea to store the enrollment month name (and perhaps the other components) separate from the enrollment date. Just store the date once, and then use Oracle's date functions to work that data.
You can use
SELECT TO_CHAR(SYSDATE, 'MON') FROM DUAL;
or
SELECT TO_CHAR(SYSDATE, 'MM') FROM DUAL;
or
SELECT EXTRACT(MONTH FROM SYSDATE) FROM DUAL;
However, you may also need to consider the year if your STUDENT table contains data for more than one year.
Besides all the good answers of our colleagues, you may hold enrolled_month columns' values in your local languages(during population of the column enrolled_month), and match in the where clause with the contribution of nls_date_language argument of to_char function as :
select enrolled_month as "Month_Hindi"
from student
where to_char(sysdate, 'Month','nls_date_language=Hindi') = enrolled_month;
Month_Hindi
-----------
अगस्त
or
select enrolled_month as "Month_Tamil"
from student
where to_char(sysdate, 'Month','nls_date_language=Tamil') = enrolled_month;
Month_Tamil
-----------
ஆகஸ்ட்
considering that you're from Sri Lanka.
I would strongly advise you to consider this approach:
select s.*
from student s
where enrolled_month >= trunc(sysdate, 'mon') and
enrolled_month < trunc(sysdate, 'mon') + interval '1' month;
Why is this better than other approaches? The function calls are all on sysdate, not on the table column. This means that Oracle can take advantage of an index on enrolled_month -- and that could be a significant improvement in performance.

Oracle SQL Current_Date

I’m experiencing some issues while using the Current_Date function in a simple query and I haven’t been able to figure out why. I’m working in an Oracle 12c environment using Oracle SQL Developer 3.2.
My original query looks something like this:
select * from Inventory where Placement_End_Dt >= Current_date
The above works fine except it doesn’t pick up records where Placement_End_Dt is today (14th May 18)
I attempted to simplify the query as follows, but this also returns nothing
select * from Inventory where Placement_End_Dt = Current_date
However when I apply date formatting as follows, it works:
select * from Inventory where to_char(Placement_End_Dt, 'DD-MM-YYYY') = to_char(Current_date, 'DD-MM-YYYY')
Then I try and expand on this to revert to my original query to select all records with an end date from today onwards:
select * from Inventory where to_char(Placement_End_Dt, 'DD-MM-YYYY') => to_char(Current_date, 'DD-MM-YYYY')
This fails spectacularly because it selects records with a Placement_End_Dt past, present and future!
The Placement_End_Dt columns is defined as an Oracle DATE data type
Would appreciate some input on how I can get this query to work.
When using to_char you are comparing strings.
to_char(date '2000-01-20', 'DD-MM-YYYY') > to_char(date '2018-05-14', 'DD-MM-YYYY')
because '20-01-2000' is greater than '14-05-2018', because of the first letters in the strings: '2' > '1'.
And CURRENT_DATE is hardly ever used, because it uses your computer's time, rather than the database time, so you can easily be some hours off. Use SYSDATE instead.
I would suggest using this query
select * from Inventory where trunc(Placement_End_Dt) = trunc(sysdate);
Oracle Date columns also store a timestamp by default, so unless the records were the same down to the second, they won't match. When you use trunc() on a date column, it truncates the timestamp and leaves just the date.
Try this, it will return the rows for present days and future days.
select * from Inventory where trunc(Placement_End_Dt) >= trunc(sysdate);

To_char for Oracle SQL not working

i have the following query which does not retrieve data even though there is:
select *
from INTERFACE_RUN
where TO_CHAR(INTERFACE_RUN.START_TIME, 'dd-mon-yy') = '04-MAY-10';
The start_time field is a timestamp field. Hence I converted it using to_char and compared with the user passed value of 04-MAY-10. But it did not fetch any data.
Any help please. Thanks in advance
to_char() pays attention to the case of the pattern. So, you are producing `04-may-10', which is not the same.
So, try this:
where TO_CHAR(INTERFACE_RUN.START_TIME, 'DD-MON-YY') = '04-MAY-10';
That said, I much prefer:
where trunc(INTERFACE_RUN.START_TIME) = date '2010-05-04'
or:
where INTERFACE_RUN.START_TIME >= date '2010-05-04' AND
INTERFACE_RUN.START_TIME < (date '2010-05-04') + 1
Why? For two reasons. First, the column is a date, so I prefer date comparisons. Second, I prefer the ANSI/ISO standard date formats. The second version can also readily take advantage of an index on the START_TIME column.
Oracle will convert the date to lowercase, thus generating '04-may-10'
Try with 'DD-MON-YY' (uppercase)
select TO_CHAR(sysdate, 'dd-mon-yy') from dual
union all
select TO_CHAR(sysdate, 'dd-MON-yy') from dual;
TO_CHAR(S
---------
13-jun-16
13-JUN-16

Convert datetime field to just a date field in SQL (Oracle)

I've seen a few answers to questions similar to mine but I cannot get them to work. I have several date fields in my query that return the date and time like such 7/1/2014 12:00:00 AM. Is there a way I can just have the fields show 7/1/2014?
SELECT DISTINCT
C.RECEIPTDATE,
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
D.SVCFROMDATE,
D.SVCTODATE,
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....
I basically would like to cut down the two date fields to the shorter date format minus the time.
Thanks in advance!
Just use the function TRUNC.
SELECT DISTINCT
TRUNC(C.RECEIPTDATE),
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
TRUNC(D.SVCFROMDATE),
TRUNC(D.SVCTODATE),
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....
Use to_char function:
SELECT DISTINCT
to_char(C.RECEIPTDATE,'DD/MM/YYYY'),
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
D.SVCFROMDATE,
D.SVCTODATE,
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....
DEPENDS on the data type.
If the column is DATE data type, then, as suggested already, TRUNC would do the job to display. But, if your locale-specific NLS date settings are different, then you will still see the time portion as midnight.
Else, you need to use TO_DATE with proper FORMAT and apply TRUNC to it.
update
If you only want to display, use TO_CHAR, else, if you have a filter in your WHERE clause, then remember TO_CHAR doesn't return DATE, it converts it into literal.
Try this:
SQL> select to_char(sysdate, 'YYYY/MM/DD') dateonly, sysdate datetime from dual;
DATEONLY DATETIME
---------- -------------------
2014/09/26 2014-09-26 15:41:03
The Oracle date datatype always includes the time.
TRUNC will truncate the time to midnight, which you will need to do if you want to match the date parts of two datetimes. The time may still display, depending on how your client is configured, so use TO_CHAR with an appropriate format mask to display it whatever way you want.
SELECT DISTINCT
to_date(C.RECEIPTDATE,'DD/MM/YYYY'),
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
D.SVCFROMDATE,
D.SVCTODATE,
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....

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'