473,700 Members | 2,446 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 11510
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
43008
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 code has worked satisfactorily with many databases, including many instances MS SQLServer 2000...
5
5130
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
8515
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 instance or if there is a pre-SQL command (like Oracles Alter Session Set NLS_DATE_FORMAT=....)?...
1
1081
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 one? Other suggestions?
1
2473
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
9857
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 do this ??? thx
1
10634
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 written for SQL Server, please show me how to convert to a MySQL compatible syntax. Thank you in...
5
4209
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 Advance..... Sivan.G.....
3
3587
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
8648
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8975
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7816
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4408
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3095
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
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.