473,756 Members | 1,861 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 11514
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
43014
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
5137
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
8522
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
1082
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
2476
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
9858
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
10637
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
4219
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
3590
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
9431
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8688
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...
0
6514
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
5119
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...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
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

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.