Skip to content

Instantly share code, notes, and snippets.

@BirgittaHauser
Created October 24, 2019 10:43
Show Gist options
  • Save BirgittaHauser/afad27c2869e1543cc16c238c79cd242 to your computer and use it in GitHub Desktop.
Save BirgittaHauser/afad27c2869e1543cc16c238c79cd242 to your computer and use it in GitHub Desktop.
SQL Function to convert a numeric date into a real date
-- 1. CvtNumYYYYMMDD2Date User Defined Function
-- Convert a numeric date in the format YYYYMMDD into a real date
Create or Replace Function YourSchema.CvtNumYYYYMMDD2Date (
ParDateNum Dec(8, 0) )
Returns DATE
Language SQL
Specific YourSchema.CvtNumYYYYMMDD2Date
Not Deterministic
Reads SQL Data
Called on NULL Input
Disallow Parallel
Set Option COMMIT = *NONE ,
DBGVIEW = *SOURCE
BEGIN
Declare Continue Handler For SQLEXCEPTION
Return Date('8888-12-31') ;
If ParDateNum = 0 Then Return Date('0001-01-01');
End If;
Return Date(Digits(ParDateNum) Concat '000000');
END ;
-- 2. CvtCYYMMDD2Date User Defined Function
-- Convert a numeric date in the format CYYMMDD into a real date
Create or Replace Function YourSchema.CvtNumCYYMMDD2Date (
ParDateNum Dec(7, 0) )
Returns DATE
Language SQL
Specific YourSchema.CvtNumCYYMMDD2Date
Not Deterministic
Reads SQL Data
Called on NULL Input
Disallow Parallel
Set Option COMMIT = *NONE ,
DBGVIEW = *SOURCE
BEGIN
Declare Continue Handler For SQLEXCEPTION
Return Date('8888-12-31') ;
If ParDateNum = 0 Then Return Date('0001-01-01');
End If;
Return Date(Right(Digits(ParDateNum + 19000000) Concat '000000', 14));
END ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment