By Tom Deutsch
By Nancy Kopp
By Paula Wiles Sigmon
By Joe Borges
By Stuart Litel
By Lester Knutsen
By James Kobielus
By Cristian Molaro
By Leon Katsnelson
By Susan Visser
By Bernie Spang
By the DB2 Guys
By Fred Ho
By Louis T. Cherian
By Shweta Shandilya
By Lawrence Weber
By Serge Rielau
By Dwaine Snow

Part 1 of this article demonstrated how the security features in DB2 V10.1 could meet several key business requirements for organizations that are consolidating data from multiple countries into a single set of tables:
This article builds on Part 1 by demonstrating how global variables and database roles can be used to reduce code complexity and improve usability, as well as dynamically calculate locale-sensitive date/time periods.
Global variables can be used to make SQL simpler to understand. Instead of specifying complex formulas as part of a SQL statement, you can create a global variable for that formula and specify the variable instead. Global variables provide a shared common routine that can be used in any SQL—eliminating the need to write the same code again and again. Furthermore, you can use variables to reduce errors in your SQL by creating just one place where the formula is specified.
Global variables support the use of a descriptive name, which helps the user or reader to determine which calculation is being performed. For example, when specifying the first day of the week, one could include the calculation as part of the query:
SELECT … FROM REAL_ESTATE_SALES
WHERE PROPERTY_TYPE = ‘CONDO’
AND EFFECTIVE_DATE =
CURRENT_DATE – (DAYOFWEEK_ISO (CURRENT_DATE)-1) DAYS)
Or we could create a global variable to improve readability. This allows anyone who views the SQL statement to infer which date calculation the query is performing:
CREATE VARIABLE FIRST_DAY_OF_WEEK DATE
DEFAULT
(CURRENT_DATE – (DAYOFWEEK_ISO (CURRENT_DATE)-1) DAYS)
The query can now be written as:
SELECT … FROM REAL_ESTATE_SALES
WHERE PROPERTY_TYPE = ‘CONDO’
AND EFFECTIVE DATE = FIRST_DAY_OF_WEEK
Global variables allow us to specify a “constant” where the name of the global variable represents the operation the business query is performing. These are a few common date/time periods:
Using global variables, we can represent these time periods in a user-friendly, easy-to-read format. Here are the definitions for the time periods mentioned above.
Today:
CREATE VARIABLE TODAY DATE DEFAULT CURRENT DATE
Yesterday:
CREATE VARIABLE YESTERDAY DATE DEFAULT CURRENT DATE – 1 DAY
Tomorrow:
CREATE VARIABLE TOMORROW DATE DEFAULT CURRENT DATE + 1 DAY
First day of the month:
CREATE VARIABLE FIRST_DAY_OF_CURRENT_MONTH DATE
DEFAULT
(CURRENT_DATE – (DAY (CURRENT_DATE)-1) DAYS)
Last day of the month:
CREATE VARIABLE LAST_DAY_OF_CURRENT_MONTH DATE
DEFAULT
(LAST_DAY (CURRENT DATE))
First day of the week:
CREATE VARIABLE FIRST_DAY_OF_WEEK DATE
DEFAULT
(CURRENT_DATE – (DAYOFWEEK_ISO (CURRENT_DATE)-1) DAYS)
First day of the current quarter (notice that global variable FIRST_DAY_OF_THE_CURRENT_MONTH is also used as part of the definition):
CREATE VARIABLE FIRST_DAY_OF_QUARTER DATE
DEFAULT
(ROUND (FIRST_DAY_OF_CURRENT_MONTH,’Q'))
So far, we have been calculating date/time values that are not tied to a country’s business rules. To illustrate how to “program” global variables to “behave” based on a particular country, let’s look at a business time duration that all countries use: fiscal year.
The beginning and end of a fiscal year is different for each country. Because of this, any global variable that would calculate these values would need to be country-aware to ensure that the values returned are correct. Table 1 lists the fiscal years for the countries used in Part 1 of this article (in some cases, however, there is more than one fiscal year definition for a country):
| Country | Fiscal Year |
| Argentina | Calendar year |
| Australia | First day of July and ends on June 30th of the following year |
| Brazil | Calendar year |
| China | Calendar year |
| Egypt | First day of July and ends on June 30th of the following year |
| Hong Kong | First day of April and ends on March 31st of the following year |
| India | First day of April and ends on March 31st of the following year |
| Kenya | First day of July and ends on June 30th of the following year |
| Mexico | Calendar year |
| New Zealand | First day of July and ends on June 30th of the following year |
| Singapore | First day of April and ends on March 31st of the following year |
| South Africa | First day of April and ends on March 31st of the following year |
| United Arab Emirates |
Calendar year |
To demonstrate how to calculate a country’s fiscal year, we will use the same roles defined in Part 1:
ARGENTINA_ROLE
AUSTRALIA_ROLE
BRAZIL_ROLE
CHINA_ROLE
EGYPT_ROLE
HONG_KONG_ROLE
INDIA_ROLE
KENYA_ROLE
MEXICO_ROLE
NEW_ZEALAND_ROLE
SINGAPORE_ROLE
SOUTH_AFRICA_ROLE
UNITED_ARAB_EMIRATES_ROLE
Next, we need to define a few more global variables to represent current year, next year, and last year:
CREATE VARIABLE CURRENT_YEAR CHAR (4)
DEFAULT (YEAR (CURRENT DATE))
CREATE VARIABLE NEXT_YEAR CHAR (4)
DEFAULT (YEAR (CURRENT DATE) +1)
CREATE VARIABLE LAST_YEAR CHAR (4)
DEFAULT (YEAR (CURRENT DATE)-1)
The code below shows the SQL that defines two global variables named START_OF_CURRENT_FISCAL_YEAR and END_OF_CURRENT_FISCAL_YEAR. Notice that the calculation is based on the database role a user has been granted. For the sake of brevity, only Australia and Hong Kong are displayed.
START_OF_CURRENT_FISCAL_YEAR
CREATE VARIABLE START_OF_CURRENT_FISCAL_YEAR TIMESTAMP
DEFAULT
(
CASE
WHEN VERIFY_ROLE_FOR_USER (SESSION_USER,’AUSTRALIA_ROLE’) = 1
THEN CASE
WHEN MONTH (CURRENT DATE) < 7
THEN LAST_YEAR||’-07-01′
ELSE
CURRENT_YEAR||’-07-01′
END
ELSE
CASE
WHEN VERIFY_ROLE_FOR_USER (SESSION_USER,’HONG_KONG_ROLE’) = 1
THEN CASE
WHEN MONTH (CURRENT DATE) < 5
THEN LAST_YEAR||’-04-01′
ELSE
CURRENT_YEAR||’-04-01′
END
END
END
)
END_OF_CURRENT_FISCAL_YEAR
CREATE VARIABLE END_OF_CURRENT_FISCAL_YEAR TIMESTAMP
DEFAULT
(
CASE
WHEN VERIFY_ROLE_FOR_USER (SESSION_USER,’AUSTRALIA_ROLE’) = 1
THEN CASE
WHEN MONTH (CURRENT DATE) > 6
THEN NEXT_YEAR||’-06-30′
ELSE
CURRENT_YEAR||’-06-30′
END
ELSE
CASE
WHEN VERIFY_ROLE_FOR_USER (SESSION_USER,’HONG_KONG_ROLE’) = 1
THEN CASE
WHEN MONTH (CURRENT DATE) > 4
THEN NEXT_YEAR||’-03-31′
ELSE
CURRENT_YEAR||’-03-31′
END
END
END
)
If a user was granted the role AUSTRALIA_ROLE, then the query:
SELECT …. FROM REAL_ESTATE_SALES
WHERE
EFFECTIVE_DATE BETWEEN
START_OF_CURRENT_FISCAL_YEAR AND END_OF_CURRENT_FISCAL_YEAR
Would resolve to:
SELECT …. FROM REAL_ESTATE_SALES
WHERE
EFFECTIVE_DATE BETWEEN ‘2011-07-01’ AND ‘2012-06-30’
As you can see, the values for the current fiscal year reflect the dates for Australia based on the user’s role.
Another example of an industry or country-specific date/time period is close of business day. The definition of this term differs by country, industry, and/or legal requirements. A common example of how close of business day plays a role in our daily lives is one’s bank balance: Often close of business day is used as part of a formula to calculate available balance (funds immediately available) and current balance (all transactions that the bank considers either not yet settled, completed, or in progress).
How would you calculate the close of business day for Argentina, Brazil, and Mexico? If we assume that close of business day is the time that the stock market closes in each of those countries, this example shows how to calculate the close of business day for these countries:
CREATE VARIABLE CLOSE_OF_BUSINESS_DAY TIMESTAMP
DEFAULT
(
CASE
WHEN VERIFY_ROLE_FOR_USER (SESSION_USER,’ARGENTINA_ROLE’) = 1
THEN
CHAR (CURRENT DATE, ISO) ||’-17.00.00′
WHEN VERIFY_ROLE_FOR_USER (SESSION_USER,’BRAZIL_ROLE’) = 1
THEN
CHAR (CURRENT DATE, ISO) ||’-17.00.00′
WHEN VERIFY_ROLE_FOR_USER (SESSION_USER,’MEXICO_ROLE’) = 1
THEN
CHAR (CURRENT DATE, ISO) ||’-15.00.00′
END
)
By combining global variables with the row column access features of DB2 10.1, you can develop a secure global data warehouse while also addressing locale-specific requirements. Global variables also help to improve usability and can decrease errors by enabling DBAs to define a formula once rather than requiring users to code the correct calculation in their SQL statements in multiple places.
DB2 TechTalk: Deep Dive on BLU Acceleration in DB2 10.5, Super Analytics Super Easy
Thursday, May 30: 12:30 – 2:00 PM ET
Informix Chat with the Lab: Primary Storage Manager (PSM) a Parallel Backup Alternative to Ontape
Thursday, May 30: 11:30 – 1 PM ET
Big Data Seminar 2013, Featuring Krish Krishnan
June 14 in New York City
marcus evans Pharma Data Analytics Conference
July 10-11 in Philadelphia
IBM Smarter Content Summit 2013
Register now!
Big Data at the Speed of Business
Broadcast event replay now available
Information on Demand 2013: Early Bird Registration Now Open
November 3-7 in Las Vegas