Skip to content

Instantly share code, notes, and snippets.

@BirgittaHauser
Created September 1, 2022 07:07
Show Gist options
  • Save BirgittaHauser/4108b879cdfa5bc848b44d8fe05db97e to your computer and use it in GitHub Desktop.
Save BirgittaHauser/4108b879cdfa5bc848b44d8fe05db97e to your computer and use it in GitHub Desktop.
Check eMail Address with SQL
-- UDF - Check eMail Address:
-- Parameters: ParEmail => EMail Address
-- Returns: 0 = Invalid eMail Address / 1 = valid eMail Address
-- In Release 7.5 the new Boolean Datatype could be used for the Return Value
-- Attention: There might be better regular expression patterns for checking eMail addresses
Create or Replace Function YourSchema.CheckEmail (ParEMail Varchar(256))
Returns Integer
Language SQL
Modifies SQL Data
Deterministic
Called On Null Input
Set Option Dbgview = *Source
Begin
Declare RegexPattern VarChar(256) CCSID 1208
Constant '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$';
Declare Continue Handler For SQLEXCEPTION Return 0;
Return Case When RegexP_Like(Trim(ParEmail), RegexPattern)
Then 1
Else 0
End;
End;
Label On Routine YourSchema.CheckEmail(Varchar()) Is 'UDF - Check eMail Address';
Comment On Parameter Routine YourSchema.Checkemail (Varchar())
(Paremail Is 'EMailAddress');
-- Test
Values(CheckEMail('Manfred.Mustermann@Company.com'));
Values(CheckEMail('Manfred!Mustermann@Company.com'));
@BirgittaHauser
Copy link
Author

BirgittaHauser commented Sep 2, 2022

There were some people having problems calling my UDF using my example.
If the UDF schema/library is in the library list (when working under System Naming Conventions)) or in the SQL Path (when working with the SQL Naming Convetions), the UDF does not have to be qualified.
If you want to call the function qualified, the test statements must look as follows:

Values(YourSchema.CheckEMail('Manfred.Mustermann@Company.com'); (System AND SQL Naming conventions)
Values(YourSchema/CheckEMail('Manfred.Mustermann@Company.com'); (System Naming Conventions only)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment