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

# Date arithmetic on a CHAR date in DB2

Can you help with a date calculation? I'm on IBM DB2 if it helps (or hinders). I have a record with a date stored in a character field, formatted YYYYMMDD. How can I calculate when this date is less than 30 days from today? Please note, I can't (not allowed to) update the database using SQL, I'm only a lowly user.

Well, I certainly sympathize with your position. Of course, we all know that dates should not be stored in character fields, but this does happen, for any number of reasons, and then we have to work with it.

The main thing we need to do is convert the character value to a date value so that we can do date arithmetic on it. One way is to CAST it, another is to use the DATE function. Once we have a date value, we can compare it to CURRENT DATE like this:

```select rowpk, chardatefield
from yourtable
where abs( days( date(chardatefield) )
- days( current date )
) < 30
```

Note that the DATE function, which converts an expression to a date value, requires a "valid string representation of a date." Despite a good half hour of searching on the IBM site, I was unable to find out exactly which string representations are considered valid. I saw a few examples (and they were few, and hard to find) that suggested that 'yyyy-mm-dd' was a valid string representation, but none that showed 'yyyymmdd'. (Note: even though I was frustrated in this instance, I hasten to point out that my impression of IBM's online documentation is otherwise very good.) Since I don't have access to DB2 and cannot test this myself, I'm going to go out on a limb and say that 'yyyymmdd' probably is a valid string representation of a date, so we can simply say DATE(chardatefield) and not worry about using substrings to pull out the year, month, and day in order to stuff dashes between them.

The second part of the solution involves converting dates to a day number using the DAYS function, which generates a result representing the number of days since January 1, 0000, or something. Note that the actual base date doesn't matter, nor do Julian and Gregorian calendar differences, because we are not concerned about the actual number of days, just their difference. So all that matters is that the day numbers are both calculated relative to the same base date.

The final part of the solution hinges on the fact that you did not specify whether you wanted to select dates less than 30 days from today in the past, in the future, or both. Since the subtraction of day numbers can result in positive or negative numbers depending on whether the date is in the past or future, the use of the ABS function selects dates which are within 30 days in either direction.

A kind reader provided a link to the DB2 Universal Database for iSeries SQL Reference manual, a 4.5meg PDF file on the IBM site. Fortunately, I was able to find the HTML manual as well. See String Representations of Datetime Values. YYYYMMDD is not among the valid values, so in the solution above, change DATE(chardatefield) to:

```DATE(SUBSTR(chardatefield,1,4)||'-'||
SUBSTR(chardatefield,5,2)||'-'||
SUBSTR(chardatefield,7,2)
)
```

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Ascend boosts enterprise data governance with data lineage

The new capabilities of Ascend Govern bring data lineage and enterprise data governance as well as the ability to try and figure ...

• ### The business benefits of enterprise data governance and MDM

Data leaders from prominent large organizations provide insights into data governance best practices and benefits, at ...

• ### How data governance and data management work together

Data governance is an important part of data management. These recommendations will help build a strong complementary ...

• ### SAS analytics platform benefiting from AI investment

Fueled by SAS' investment in AI and augmented intelligence features, the long-established vendor strengthened its BI platform ...

• ### Trends and top use cases for streaming data analytics

As more enterprises adopt real-time analytics, new infrastructure and best practices are appearing. Here are some trending ...

• ### COVID-19 effect on analytics software development an unknown

With COVID-19 forcing software engineers to work remotely rather than together in teams, they'll need to find new ways of ...

## SearchSAP

• ### How to pick a SuccessFactors implementation partner

Selecting the right SuccessFactors implementation partner is a critical step in the journey to deploying the system. Check out ...

• ### 6 critical steps of a successful SAP S/4HANA migration

A successful S/4HANA project starts with knowing why your organization should even make the move, then nailing down whether S/4 ...

• ### SAP Ariba Discovery now open to all buyers and suppliers

Buyers and suppliers can now access SAP Ariba Discovery for free, and SAP Ariba, Fieldglass and S/4HANA are further integrated ...

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

• ### How to refactor the God object antipattern

Loose coupling can inadvertently create a God object problem in an object-oriented software system. Here's how to get rid of the ...

• ### Oracle ships Java 14 with new preview, productivity features

With Java 14, Oracle delivers features to the Java language and platform that enhance the productivity of developers building new...

• ### 6 top webpage performance metrics to monitor

Webpage performance is paramount for digital success. To monitor webpage performance, use these top six metrics.

## SearchDataCenter

• ### Tech giants aim supercomputers, clouds at finding a COVID-19 cure

The new COVID-19 HPC consortium comprised of tech giants, national labs and academia are using supercomputers and clouds to speed...

• ### Top vendor SDDC certification programs for admins

If you decide to grow your knowledge of software-defined data center technologies, VMware, Nuage Networks and Microsoft offer ...

• ### Build up your knowledge of leaf-spine network technology

As organizations deal with challenges of single tree path networks, many admins are considering leaf-spine architectures. Learn ...

## SearchContentManagement

• ### Vendors offer free remote work technology for telecommuting

It's more than a marketing ploy: Cloud technology vendors, seeing their own issues enabling remote work, extend paid products to ...

• ### How businesses should deal with enterprise search issues

Enterprise search issues frequently complicate user experience with ECM systems. While users may face many problems, they also ...

• ### Box, Dropbox shore up clouds; Box-Microsoft integrations go live

Online document collaboration platforms Box and Dropbox shore up cloud bandwidth, security and customer support as remote work ...

## SearchHRSoftware

• ### Farmers Insurance piloting human-like VR training

VR training has become a best practice at Farmers Insurance Exchange. Its claims adjusters are being exposed to more scenarios ...

• ### Virtual hiring, avoiding layoffs part of HR's pandemic response

In the face of a pandemic, many firms are trying to avoid layoffs and even hire. They are using virtual tools, including testing ...

• ### Transformational HR in 2020: 7 strategies for agility and tech mastery

CHROs and their teams must grow their people, business and technology skills as they focus on curating the employee experience. ...

Close