Date functions Now that IB6 has built­in EXTRACT function, some older problems can be solved more easily.  • • • • •

 Day of Week     First/last day of month      Number of days in month      Week of year    Updated   New code is simpler, and works with all dialects.    Is Leap Year ?   

Day of Week In IB6 there is a new built­in function EXTRACT(). This call  EXTRACT(WEEKDAY FROM D) will return 0=Sunday, 1=Monday, ... 6=Saturday.  On the other hand, International Standard ISO 8601 specifies that week begins on Monday and that it is day 1. To  get Day of Week by ISO standard, use:  EXTRACT(WEEKDAY FROM D-1)+1 which will return 1=Monday, 2=Tuesday, ... 7=Sunday. 

Example:  SELECT D, EXTRACT( WEEKDAY FROM D) EXTRACT( WEEKDAY FROM D - 1) + 1 FROM T; D AMERICAN ISO8601 =========== ======== =========== 29-NOV-2001 4 4 30-NOV-2001 5 5 1-DEC-2001 6 6 2-DEC-2001 0 7 3-DEC-2001 1 1 4-DEC-2001 2 2 5-DEC-2001 3 3

First/last day of month First day of month:  D - EXTRACT(DAY FROM D) + 1;

AS AMERICAN, AS ISO8601

<<<<< Sunday

Last day of month:  LDM = D - EXTRACT(DAY FROM D) + 32; LDM = LDM - EXTRACT(DAY FROM LDM); or as single expression  D - EXTRACT(DAY FROM D) + 32 - EXTRACT(DAY FROM D - EXTRACT(DAY FROM D) + 32)

First day of next month:  FDNM = D - EXTRACT(DAY FROM D) + 32; FDNM = FDNM - EXTRACT(DAY FROM FDNM) + 1; or as single expression  D - EXTRACT(DAY FROM D) + 33 - EXTRACT(DAY FROM D - EXTRACT(DAY FROM D) + 32)

Example: if the day is D='2002­3­22',  first day of month will be '2002-3-1' last day of month will be '2002-3-31' first day of next month will be '2002-4-1'

Number of days in month Number of days in month procedure:  CREATE PROCEDURE MonthLength (D DATE) RETURNS (ML INTEGER) AS DECLARE VARIABLE TMP DATE; BEGIN TMP = D - EXTRACT(DAY FROM D) + 32; ML = EXTRACT(DAY FROM (TMP - EXTRACT(DAY FROM TMP))); END or as single expression  EXTRACT(DAY FROM (D - EXTRACT(DAY FROM D) + 32 - EXTRACT(DAY FROM D - EXTRACT(DAY FROM D) + 32)))

Week of year ISO 8601 standard (for Date and Time Notation) specifies that week begins with a monday, and that the first week  of a year is the one which includes the first thursday. (In other words, week belongs to the year where lies its bigger  part.) This is the basic formula:  (EXTRACT(YEARDAY FROM D) - EXTRACT(WEEKDAY FROM D-1) + 7) / 7 It can return value between 0 and 53. Sometimes it can be sufficient, however if we want to conform ISO standard,  we need to make some adjustments in case the result is zero or 53. 

If the result was zero, it means that in fact the week belongs to previous year. The last week of previous year can  be either 52 or 53; to find out which one, we just repeat the same formula (with the last day of previous year as  parameter).  If the result was 53, we must check whether it is really 53rd week, or whether it is the first week of the next year.  53rd week is valid only if it contains thursday (i.e. 31.12. is either thursday or friday).  These adjustments can easily be avoided if we compute the formula for the thursday of the week we 

are interested in. 

CREATE PROCEDURE YearWeek (D DATE) RETURNS (WEEK_NO VARCHAR(8)) AS DECLARE VARIABLE W INTEGER; /* week number */ DECLARE VARIABLE Y INTEGER; /* year the week belongs to */ BEGIN D = D - EXTRACT(WEEKDAY FROM D-1) + 3; /* move to thursday */ W = (EXTRACT(YEARDAY FROM D) - EXTRACT(WEEKDAY FROM D-1) + 7) / 7e0; Y = EXTRACT(YEAR FROM D); /* This is just formatting; you may prefer to make W and Y return parameters instead. */ IF (W<10) THEN WEEK_NO = '0'; ELSE WEEK_NO = ''; WEEK_NO = Y || '/' || WEEK_NO || W; SUSPEND; END

Original code worked with dialect­1 only. Here it is with fix to work with all dialects:  CREATE PROCEDURE YearWeek_old (D DATE) RETURNS (WEEK_NO VARCHAR(8)) AS DECLARE VARIABLE W INTEGER; /* week number */ DECLARE VARIABLE Y INTEGER; /* year the week belongs to */ BEGIN W = (EXTRACT(YEARDAY FROM D) - EXTRACT(WEEKDAY FROM D-1) + 7) / 7e0; Y = EXTRACT(YEAR FROM D); IF (W=0) THEN BEGIN Y = Y - 1; D = D - EXTRACT(YEARDAY FROM D) - 1; /* last day of previous year; D is used as temporary variable here */ W = (EXTRACT(YEARDAY FROM D) - EXTRACT(WEEKDAY FROM D-1) + 7) / 7e0; END ELSE IF (W=53 AND 4>EXTRACT(WEEKDAY FROM (D - EXTRACT(DAY FROM D) + 31))) THEN BEGIN Y = Y + 1; W = 1; END /* This is just formatting; you may prefer to make W and Y return parameters instead. */ IF (W<10) THEN WEEK_NO = '0'; ELSE WEEK_NO = ''; WEEK_NO = Y || '/' || WEEK_NO || W;

SUSPEND; END

Is Leap Year ? To find out whether given date belongs to leap year, we can look e.g. at 59th day in year; it is either February 29 for leap year, or it is March 1 for normal year.  Here is example of stored procedure:  CREATE BEGIN IF ( LY ELSE LY END

PROCEDURE Is_LeapYear (D DATE) RETURNS (LY INTEGER) AS 2 = EXTRACT(MONTH FROM (D - EXTRACT(YEARDAY FROM D) + 59)) ) THEN = 1; /* leap year */ = 0;

/* normal year */

Another procedure (with Year as input parameter):  CREATE BEGIN IF ( LY ELSE LY END

PROCEDURE Is_LeapYear (Y INTEGER) RETURNS (LY INTEGER) AS 60 = EXTRACT(YEARDAY FROM CAST(Y || '-3-1' AS TIMESTAMP)) ) THEN = 1; /* leap year */ = 0;

/* normal year */

Because IB5 does not support EXTRACT function, we can just test whether February 29th exists:  CREATE PROCEDURE Is_LeapYear (Y INTEGER) RETURNS (LY INTEGER) AS DECLARE VARIABLE D DATE; BEGIN LY = 1; D = CAST('29-FEB-' || Y AS DATE); WHEN ANY DO LY = 0; END

Well known expression for testing leap year is this  Is_LeapYear := ( ((Year MOD 4) = 0) AND ((Year MOD 100) <> 0) ) OR ((Year DIV 400) = 0); but IB/FB does not directly support MOD operator; it can be implemented either with UDF, or by proper  implementing of expression  x MOD y = x ­ (x DIV y) * y (which is not as easy as it seems to be at first sight because of different rules for dividing in Dialect­1 and Dialect­ 3). 

Copyright © 2002,2005 Ivan Prenosil 

Firebird/InterBase - Date functions

Number of days in month ... get Day of Week by ISO standard, use: .... Because IB5 does not support EXTRACT function, we can just test whether February 29th ...

270KB Sizes 24 Downloads 297 Views

Recommend Documents

Functions and Equations in Two Variables Functions ...
z = f(x, y). Example:ааEvaluate the function for f(4,ан3). f(x, y) = x. 2. + 4y or ... necessary to solve an equation for a variable. ... Pg 486аа585 x 5, 100, 101, 103.

Perceptual Reward Functions - GitHub
expected discounted cumulative reward an agent will receive after ... Domains. Task Descriptors. Figure 3: Task Descriptors. From left to right: Breakout TG, ...

Rational Functions Name: Modified Text Date: Per: U. 4 ...
... transformations to the asymptotes x = 0 and y = 0 and original reference points (-1,1) and (1,1). Also state the domain and range of g(x) using inequalities, set.

functions-pack.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. functions-pack.

Recursive Functions - GitHub
Since the successor function can increment by one, we can easily find primitive ... Here we have used the convention of ending predicate names with “?”; in this .... that is, functions that are undefined, or divergent, at some values in the domai

Executive functions in synesthesia
Jan 8, 2013 - Third, we found support for our hypothesis that inhi- bition of a synesthetic color ..... Six color words (in Dutch) were presented on the computer screen (distance to the screen was ...... Nature, 406, 365. Dixon, M. J., Smilek, D., ..

Executive functions in synesthesia
Jan 8, 2013 - not predict performance on a synesthetic Stroop task. .... those synesthetes good at inhibiting synesthetic color should be relatively good at .... www.neurobs.com) on a PC with Windows version XP and CRT monitor, and re-.

Kendall distribution functions
If X and Y are continuous random variables with joint distribution function H, then the Kendall distribution function of (X; Y) is the distribution function of the random variable H(X; Y). Kendall distribution functions arise in the study of stochast

Physics-based basis functions - GitHub
effect of element mutual coupling on both signal and noise response of the .... evlamemo69.pdf ..... {10o, 20o, 30o, 40o, 50o}, which give rise to a rank-five voltage covariance matrix. ... An illustration of two sets of CBFPs for modeling the.

SLAX Functions The slaxproc Command
the two formats. The SLAX syntax is similar to .... $res[2] = Address family, "inet4" or "inet6". $res[3] = Prefix length .... function. output-method [ xml | text | html ] {.

Functions Inputs Output
int main(void). { int x = 2; printf("x is %i\n", x); x = cube(x); printf("x is %i\n", x);. } int cube(int input). { int output = input * input * input; return output;. } Page 6. Page 7. cube()'s parameters cube()'s locals main()'s locals main()'s par

Searching for Activation Functions - arXiv
Oct 27, 2017 - Practically, Swish can be implemented with a single line code change in most deep learning libraries, such as TensorFlow (Abadi et al., 2016) (e.g., x * tf.sigmoid(beta * x) or tf.nn.swish(x) if using a version of TensorFlow released a

Functions of the Cranial Nerves
Glosso-pharyngeal Taste (Pharyngeal). Pharyngeal muscles. X. Vagus. Viscero-sensation. (including taste) + somaticsensory from head. Visceral motor:.

hyperbolic functions w.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. hyperbolic ...

Sketching Valuation Functions
Oct 4, 2011 - ‡Department of Computer Science, Stanford University, 462 ..... with XOS functions, for which 1-supporting prices exist, it remains open whether ...

Functions & Relations Intro.pdf
Names and phone numbers in a directory. • Input and output ... This pairing or mapping is done. based on a ... Page 3 of 9. Functions & Relations Intro.pdf.

Fun with Functions Accounts
Microsoft Excel: Fun with Functions. J. Dee Itri, Excel Maze. ○ Cell referencing. ○ Row numbers and column letters (E7, G4, BZ12). ○ =5+5 vs =A1+A2. ○ Ranges (A:A, 1:4, A5:B14). ○ The great and powerful “$” sign. ○ Relative (B5). ○

Business English- Teleconference Functions - UsingEnglish.com
“There are two Akiras, so please call me Aki and him Akira.” “Richard has just .... I suggest we schedule another conference call for (day and time). Okay, thanks ...

Functions & Relations Intro.pdf
Former Lecturer – John Donaldson Technical Institute - Trinidad. © All rights reserved. No part of this document must be reproduced stored in a retrieval. system ...

Functions, Responses, and Effectiveness.pdf
United States of America: Secularist, Humanist, Atheis ... ed States; Functions, Responses, and Effectiveness.pdf. United States of America: Secularist, Humanist, ...

Network Functions Virtualization
Computing virtualization (e.g., Linux KVM). ▫. All advantages of virtualization (quick provisioning, scalability, mobility, reduced CapEx, reduced OpEx, multitenancy, …) ▫ Standard API (i.e., ETSI framework). ▫ Network Functions Virtualizatio

Evaluating Functions notes.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Evaluating ...

C1-L13 - Trigonometric Functions - Transformations.pdf
C1-L13 - Trigonometric Functions - Transformations.pdf. C1-L13 - Trigonometric Functions - Transformations.pdf. Open. Extract. Open with. Sign In. Main menu.