473,574 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.M YDATE 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 11495
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.c om 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.M YDATE 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
42992
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 mediate between webapps and arbitrary database backends using JDBC. I am very unwilling indeed to write special-case code for particular databases. Our...
5
5125
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 However if I update any other column the syntax is fine
2
8498
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 selection criteria to be in the format 2005-01-01-13.24.54.000000 or it will not find it. Is there a configuration parameter that can be set in the DB2...
1
1076
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 be nice not to have to specifically set the style of a particular column. Is there some way to tell the datagrid how to display dates when it has...
1
2470
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 valid. This appears to be a bug with Access. Has anyone ever come across this issue before? Cheers Paul Gagnon
6
9854
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 date./time formating is : e.g. 2007-10-25 12:45:00 and I would like to transform in using xslt to format : 25-10-2007 12:45:00 does anyone know how to...
1
10623
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 written for use with SQL Server. I just need to be shown how to convert the syntax to one that is compatible with MySQL. The following is the stored proc...
5
4196
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 00 i.e i get the output as the folowing for eg: "18/00/2007" can any one help me in changing the format... Thanks in...
3
3584
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 MyTable FROM MyStoredProc -- I'd be happy.
0
7831
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7746
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8262
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6497
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5331
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2258
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1092
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.