Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

SQL CLR Function for Email validation and string Split

By Bharat Panthee,

Create two separate cs file for two above functions.

create DLL including 2 functions, lets say that is Splitter_email_validator.dll

Enable CLR in your database.

 

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO

Then Create assembly

CREATE ASSEMBLY String_splitter_and_Email_Validator
FROM C:\Email_validator\Splitter_email_validator.dll
WITH PERMISSION_SET = SAFE;

Note: Here C:\Email_validator\ is location of DLL.

 

Now create function in SQL.

First create string splitter:

CREATE FUNCTION [dbo].[string_splitter](@strtobeSplit [nvarchar](4000), @strSeparator [nvarchar](4000))
RETURNS TABLE (
[SeparatedString] [nvarchar](1000) NULL
) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [String_splitter_and_Email_Validator ].[Emailvaliator_and_word_splitter].[string_splitter]

 


Now, create email validation function:


CREATE FUNCTION [dbo].[emailValidator](@emailAddress [nvarchar](4000))
RETURNS [nvarchar](4000) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [String_splitter_and_Email_Validator ].[Emailvaliator_and_word_splitter].[emailValidator]

 

How to call this?

You can call these functions like other sql functions.

for example:

select * from [dbo].[string_splitter]('test,test1,test2' , ',')

When you run that statement you will get 3 rows as table:

test

test1

test2

Another function can be called like this:

select [dbo].[emailValidator]('bharatpanthee@gmail.com')

It will return 'valid'

if supplied email address is not vaid then it will return:

'Invalid'

 

 

 

 

Total article views: 1464 | Views in the last 30 days: 2
 
Related Articles
FORUM

String splitter to table weird result

String Splitter

FORUM

create function

create function

ARTICLE

Tally OH! An Improved SQL 8K “CSV Splitter” Function

The Tally Table has proven to be a simple and elegant method for avoiding many varieties of RBAR. Un...

FORUM

String splitter function

Hi All, I need help with a function which accepts a string and returns in the column format for SQ...

FORUM

uses of string splitters

Hi, I'm reading sql stuff and came across creating functions to split strings into columns/tables. S...

Tags
sqlclr    
string manipulation    
t-sql    
 
Contribute