Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Checking the day of the week

I need to determine if a particular day of the week exists between two given dates. I can't figure out an elegant way to do this, only comparing every DOW between the two dates to DOW in question. Is there an Oracle DATE function that does this? I need this check to be as quick as possible, as it is built into a form.
There is an Oracle function, NEXT_DAY, that you can use in a fairly elegant solution.

``` CREATE OR REPLACE FUNCTION dow_between ( in_dow_text VARCHAR2, -- e.g. 'Mon' or 'Monday' in_date_1 DATE, -- starting date in_date_2 DATE -- ending date ) RETURN DATE -- ***************************** -- ** d o w _ b e t w e e n ** -- ***************************** -- dow_between returns the a DATE between in_date_1 and -- in_date_2 whose day-of-the week matches in_dow_text. -- dow_between returns NULL if there is no such date. -- in_date_1 must be earlier than in_date_2. -- Example: dow_between ('SAT', SYSDATE, due_dt) -- checks if there is a Saturday between today and due_dt, -- inclusive. If there is, it returns the DATE of the first -- one. If not, it returns NULL. -- in_dow_text can be any unique abbreviation (case insensitive) -- of the day-of-the-week in NLS_LANGUAGE. IS return_date DATE := NEXT_DAY ( in_date_1 - 1, in_dow_text ); BEGIN IF TRUNC (return_date) > in_date_2 THEN return_date := NULL; END IF; RETURN return_date; END dow_between; /
```

The NEXT_DAY (d, c) function has a couple of quirks that dow_between handles or inherits:

• It returns a DATE after d, never the same as d. That's why return_date is initialized by NEXT_DAY (in_date_1 - 1, ...)
• It returns a DATE with the same time of day as the argument d. That's why the TRUNC command in the IF statement is necessary.
• In some languages, the argument c may be abbreviated. When NLS_LANGUAGE = English, for example, the first two letters of the weekday names (as returned by TO_CHAR (d, 'Day')) are unique, and NEXT_DAY accepts a two-character abbreviation. (In some other languages, abbreviations give less desirable results.)
• Anything after the required abbreviation is ignored, so the following are all valid and equivalent (in English):
• Sunday
• Su
• Sunny skies
• Sun 02-Nov-2003

• You don't have to worry about the trailing blanks that may come with TO_CHAR (d, 'Day')
• As far as being quick, I wouldn't worry. The biggest bottlenecks in most applications are disk I/O and network traffic. This function uses little (if any) of either.

Footnote:

In languages where the name of one weekday (a) is itself an abbreviation of another (b) NEXT_DAY seems to regard any string LIKE 'a%' as a synonym for a, unless it is LIKE 'b%'. For example, when NLS_LANGUAGE = Turkish, 'Pazar' is Sunday and 'Pazartesi' is Monday. NEXT_DAY (d, x) returns the following Sunday when x is any of the following:

• Pazartes
• Pazarte
• Pazart

It makes no difference if NLS_TERRITORY = America (where Sunday is the first day of the week) or Turkey (where Monday is the first day).

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Amazon cloud database and data analytics expand

AWS expands its Redshift data warehouse capabilities including managed storage and query acceleration at the re:Invent 2019 ...

• ### Vendors move away from open source database software licensing

While open source is a key to success for many database vendors, it can also potentially lead to a competitive threat from a ...

• ### Oracle looks to grow multi-model database features

Decade after decade, Oracle continues to be relevant in the database market as it pivots to include an expanding list of ...

• ### Sisense analytics platform update features AI, data prep tool

Augmented intelligence features and an in-warehouse data prep tool highlight Sisense's latest update, its second since acquiring ...

• ### Operation Fistula uses data to treat childbirth injuries

Aided by an analytics stack developed by Exasol, nonprofit Operation Fistula is working to end obstetric fistula in targeted ...

• ### Automated tools broaden the future scope of data science

Automated data science tools may make their way into the data science process, but are unlikely to ever be able to do everything ...

## SearchSAP

• ### Partners need to target SAP SMB market for cloud transition

In this Q&A, SAP executive Claus Gruenewald discusses how SAP partners are instrumental in servicing the SMB market and how they ...

• ### S/4HANA Cloud integrates Qualtrics for continuous improvement

The latest version of SAP S/4HANA Cloud embeds Qualtrics XM capabilities that provide a way for users to give direct feedback ...

• ### SAP Data Warehouse Cloud may democratize data analytics

SAP's new SAP Data Warehouse Cloud promises to make data analytics more accessible to the business. It's a needed addition to ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Public API strategy considerations for enterprise adoption

As organizations look for more cost-effective ways to manage data, an evolving landscape with APIs has made the technology more ...

• ### Ideas on how to hold a successful code hackathon

Want to host a hackathon? Here are some ideas on what a company can do to host an event that solves problems and reenergizes the ...

• ### A Jenkins tutorial for beginners with examples

If you're ready to try Jenkins, this quick start tutorial lays out the specific steps you'll need to take to download and install...

## SearchDataCenter

• ### Top 4 open source automation tools for admins

Open source offerings are an easy way to bring automation into your organization. When selecting software, evaluate the user ...

• ### U.S. facility may have best data center PUE

The data center energy efficiency metric, power usage effectiveness, or PUE, is not improving. But one supercomputer data center ...

• ### Increase efficiency with data center temperature monitoring

Data centers must track multiple temperature components. Organizations can use ASHRAE standards and on-premises hardware for ...

## SearchContentManagement

• ### New capabilities added to Alfresco Governance Services

The updates are intended to secure electronically stored information residing across different locations in light of recent ...

• ### Top 5 Office 365 skills for SharePoint administrators

Microsoft SharePoint administrators need to branch out in their Office 365 skill set to support products tightly integrating with...

• ### Box cloud content management helps simplify digital work

Box currently has three main areas of focus: integration with enterprise applications, content security and collaboration. The ...

## SearchHRSoftware

• ### IT's technology experience problem gets HR tool

Technology deployments have been blamed for loss of productivity and even employee resignations. A new tool from Qualtrics could ...

• ### 3 keys to employee engagement measurement

Far too many organizations and their HR departments conduct employee engagement measurement in a way that does more harm than ...

• ### Cornell researchers call for AI transparency in automated hiring

Researchers at Cornell University want vendors to disclose how automated hiring systems are making hiring recommendations. They ...

Close