Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL Generate Random/Sample Data from RegEx

Tags:

t-sql

I have to generate some random data, but from Regular Expression given.

For example I have these rules:

 1) IsRegEx('^(((([0-1]?[0-9]|2[0-3])(:[0-5][0-9])?)?:)?[0-5])?[0-9]$', #COMMENT#)
 2) IsRegEx('^[0-9]+:([0-5][0-9]):([0-5][0-9])$',#COMMENT#)
 3) IsRegEx( '^[0-9]+$' , #COMMENT#)

and many others... I have to generate text #Comment# which will pass the rule. It is not necessary to there are many variations, one is enough.

Have anyone do something like this?

Thanks in advance.

like image 439
gotqn Avatar asked Jun 17 '26 23:06

gotqn


2 Answers

The proper way to do this in Sql Server - thru CLR stored procedure or CLR user defined function, if your Sql Server version allows this. Else - there is no any acceptable way.

like image 182
Oleg Dok Avatar answered Jun 24 '26 04:06

Oleg Dok


You can't do that with T-SQL. T-SQL doesn't even support regular expressions. If you have Visual Studio Premium you can create a database project and use a regular expression data generator to create the test data you want. Check "Generating Test Data for Databases using Data Generators".

If you don't have VS Premium you can create your own generator as a CLR stored procedure

like image 39
Panagiotis Kanavos Avatar answered Jun 24 '26 04:06

Panagiotis Kanavos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!