Solved

Oracle SQL - Dynamic Dates

Posted on 2011-03-24
4
347 Views
Last Modified: 2012-05-11
Looking for SQL to create dynamic dates, so that it always pulls from December 1 of prior year to the current date.

Example (if run today):
Select * from my_table where my_date between '12/1/2010' and '3/24/2011'

Need this part to be dynamic: '12/1/2010' and '3/24/2011'
0
Comment
Question by:carsRST
  • 2
  • 2
4 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 35208660
elect * from my_table where my_date
between add_months(trunc(sysdate,'yyyy') ,-1) and trunc(sysdate)

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35208669
note the "between" will exclude all data for today unless my_date is for midnight  00:00:00


if you want to include today, try one of these clauses instead

my_date between add_months(trunc(sysdate,'yyyy') ,-1) and trunc(sysdate)+1 - 1/86400


or

my_date >=  add_months(trunc(sysdate,'yyyy') ,-1) and my_date < trunc(sysdate)+1



0
 
LVL 16

Author Closing Comment

by:carsRST
ID: 35208670
You're a good man!

Thanks.
0
 
LVL 16

Author Comment

by:carsRST
ID: 35208679
>>note the "between" will exclude all data for today unless my_date is for midnight  00:00:00

Actually, I really need as of the previous day anyway.  works out perfectly.

Thank you!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now