473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Default Date format using CAST in Stored Proc

We commonly use triggers to log changes to our main tables to
historical log tables.

In the trigger, we create a concatenated string of the old values by
casting them as follows:

CAST(O.MYDATE AS CHAR(30))

When directly updating date fields in the main table, the logged value
gets saved in the format YYYY-MM-DD as expected.

But, when we perform the update through a stored procedure, the date
values are saved as MM/DD/YYYY.

This causes our log parser to think the values are different, when they
are actually the same values represented in a different format.

We discovered that the CHAR( ) function has an optional parameter and
ISO can be used to explicitly designate the way dates are converted.

Question 1:

Does anyone know why the trigger would cast differently when it is
activated from a direct table update vs. a stored procedure?

Question 2:

Is there a database setting that can be used to set the default
conversion method to ISO when casting DATE to CHAR. This would save us
the trouble of recoding all of our triggers to explicitly specify the
ISO option.

Thanks,

Bob
Example:

DROP TABLE DATETEST;
CREATE TABLE DATETEST(ID BIGINT NOT NULL GENERATED BY DEFAULT AS
IDENTITY,
MYDATE DATE);

DROP TABLE DATETESTLOG;
CREATE TABLE DATETESTLOG(ID BIGINT NOT NULL GENERATED BY DEFAULT AS
IDENTITY,
MYDATA VARCHAR(1000),
MYDATAISO VARCHAR(1000),
TS TIMESTAMP);

CREATE TRIGGER DATETEST_ULog AFTER UPDATE
ON DATETEST
REFERENCING OLD
AS O
FOR EACH ROW
MODE DB2SQL INSERT INTO DATETESTLOG(
MYDATA, MYDATAISO, TS)
VALUES(CAST(O.MYDATE AS CHAR(30)), CHAR(O.MYDATE, ISO), CURRENT
TIMESTAMP)
;

DROP PROCEDURE RG.DATETESTCHG;

CREATE PROCEDURE RG.DATETESTCHG(IN PID BIGINT, CHGAMT INT)
SPECIFIC DATETESTCHG
RESULT SETS 0
MODIFIES SQL DATA
NOT DETERMINISTIC
NULL CALL
LANGUAGE SQL

BEGIN

UPDATE DATETEST SET MYDATE = MYDATE + CHGAMT DAY
WHERE ID = PID;

END
;

INSERT INTO DATETEST(MYDATE) VALUES('1967-11-05');

INSERT INTO DATETEST(MYDATE) VALUES(CURRENT DATE);
UPDATE DATETEST SET MYDATE = MYDATE + 1 DAY
WHERE ID = 1;

CALL RG.DATETESTCHG(2, 1);

SELECT * FROM DATETEST;

SELECT * FROM DATETESTLOG;

Aug 28 '06 #1
2 11476
Bob,

The first thing you should note is that the date values are not "saved as
MM/DD/YYYY" (or any other character representation !!! Instead they are
stored in an internal date format. It is the client settings which
control how a date is displayed in output and, more importantly, how
strings which represent dates are handled. This applies inside
application objects such as stored procedures and triggers as well as in
SQL submitted from the command line.

Take the following SP for example -

CREATE PROCEDURE SP001DATE
(OUT p_later CHAR(1))
SPECIFIC SP001DATE
BEGIN
SET p_later = 'N';
IF (CURRENT DATE '11/06/2006') THEN
SET p_later = 'Y';
END IF;
END#

If I create this SP, and run it from my UK client I get the result 'Y',
since it is later than 11th June 2006. However if I connect to the same
database from a US client and run the same procedure then I get the result
'N', since 6th November 2006 is still in the future at this point.

Here I chose a date that was in the first 12 days of the month, so that the
procedure would actually execute correctly. If I had set the date to
'13/06/2006' then from my UK client it would have run correctly, but from a
US client I'd have got -

SQL0181N The string representation of a datetime value is out of range.
SQLSTATE=22007

Note that this is a runtime error. I can equally well build from my UK
client a procedure with the string set to '06/13/2006' but it fails when I
run it.

The moral of this story is that you should never be using character
representations of dates directly in routine code without taking steps to
ensure that they are validated and interpreted uniformly no matter what the
client locale settings are.

Using the ISO setting of CHAR is one way of achieving this, at least for
data being sent out. At least then you are guaranteed a standard format
no matter where you run it from.

And in the SP above you'd want to construct a value of type DATE from the
string in a standard way, rather than using the literal. I've got some
samples of this which I could dig out if you need.

More difficult is when a client wants to send a literal string (as a CHAR())
which they actually want treated as a date within the SP. I think a good
approach is to insist that they actually use a DATE as the parameter
wherever possible : then validation is done automatically. One area where
you have issues with this is in handling XML documents as input : and why
you really should be validating XML against XSDs.

HTH

Phil
sy*****@gmail.com wrote:
We commonly use triggers to log changes to our main tables to
historical log tables.

In the trigger, we create a concatenated string of the old values by
casting them as follows:

CAST(O.MYDATE AS CHAR(30))

When directly updating date fields in the main table, the logged value
gets saved in the format YYYY-MM-DD as expected.

But, when we perform the update through a stored procedure, the date
values are saved as MM/DD/YYYY.

This causes our log parser to think the values are different, when they
are actually the same values represented in a different format.

We discovered that the CHAR( ) function has an optional parameter and
ISO can be used to explicitly designate the way dates are converted.

Question 1:

Does anyone know why the trigger would cast differently when it is
activated from a direct table update vs. a stored procedure?

Question 2:

Is there a database setting that can be used to set the default
conversion method to ISO when casting DATE to CHAR. This would save us
the trouble of recoding all of our triggers to explicitly specify the
ISO option.

Thanks,

Bob
Example:

DROP TABLE DATETEST;
CREATE TABLE DATETEST(ID BIGINT NOT NULL GENERATED BY DEFAULT AS
IDENTITY,
MYDATE DATE);

DROP TABLE DATETESTLOG;
CREATE TABLE DATETESTLOG(ID BIGINT NOT NULL GENERATED BY DEFAULT AS
IDENTITY,
MYDATA VARCHAR(1000),
MYDATAISO VARCHAR(1000),
TS TIMESTAMP);

CREATE TRIGGER DATETEST_ULog AFTER UPDATE
ON DATETEST
REFERENCING OLD
AS O
FOR EACH ROW
MODE DB2SQL INSERT INTO DATETESTLOG(
MYDATA, MYDATAISO, TS)
VALUES(CAST(O.MYDATE AS CHAR(30)), CHAR(O.MYDATE, ISO), CURRENT
TIMESTAMP)
;

DROP PROCEDURE RG.DATETESTCHG;

CREATE PROCEDURE RG.DATETESTCHG(IN PID BIGINT, CHGAMT INT)
SPECIFIC DATETESTCHG
RESULT SETS 0
MODIFIES SQL DATA
NOT DETERMINISTIC
NULL CALL
LANGUAGE SQL

BEGIN

UPDATE DATETEST SET MYDATE = MYDATE + CHGAMT DAY
WHERE ID = PID;

END
;

INSERT INTO DATETEST(MYDATE) VALUES('1967-11-05');

INSERT INTO DATETEST(MYDATE) VALUES(CURRENT DATE);
UPDATE DATETEST SET MYDATE = MYDATE + 1 DAY
WHERE ID = 1;

CALL RG.DATETESTCHG(2, 1);

SELECT * FROM DATETEST;

SELECT * FROM DATETESTLOG;
Aug 29 '06 #2
I'm far from an expert on this, but I would take a look at PREP/BIND
options to force the ISO settings. Maybe what you observe is a
difference between application locale and server locale.
Strange as it sounds it is conceivable that the trigger inherits the
application local from the SQL statement that fires it. This would be
due to the inlined characteristics of DB2 for LUW triggers.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Aug 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
5
by: S.Patten | last post by:
Hi, I have a problem with updating a datetime column, When I try to change the Column from VB I get "Incorrect syntax near '942'" returned from '942' is the unique key column value ...
2
by: dbztax | last post by:
Hi all, In Oracle and SQL server I have a date/time of 01/01/2005 13.24.54. If I do a select using the date field with a criteria of 01/01/2005 it will find it just fine. DB2 requires the...
1
by: Curtis | last post by:
We're loading one datagrid with various data results from stored procedures in MSSQL. When there is a date column only mm/dd/yy is displayed. Some datasets have dates and others don't so it would...
1
by: Paul Gagnon | last post by:
Hi everybody I have a field with a custom date format tied to it mm/dd/yy. If I enter an invalid value of 13/10/05, access automatically resets the value to 10/05/13 and accepts the input as...
6
by: nataria | last post by:
Hi, I have a problem in transforming date format. Originally I transform an access database data into XML then transform the XML doc into another XML with different strucutre. The original...
1
by: WebNewbie | last post by:
Hi, I am new to using mysql and there isn't any tutorials online on that shows how to create mysql stored procedure for paging purposes. Thus, I read tutorials on creating stored proc that were...
5
mgpsivan
by: mgpsivan | last post by:
Hi, i have form in Asp.net(2003) in that i've to change the format of the date by the following statement Format(Date.Today, "dd/mm/yyyy") but when i give this code the month part displays as...
3
by: Deane | last post by:
I have a stored proc that returns a resultset. I would like to deposit that resultset into a table. Kind of like a "SELECT INTO", but using a stored proc. If I could do this -- SELECT INTO...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.