Checking a row in a table in SQL Server - CLR or T-SQL function (question updated)

I need to check if the column value (row) in the SQL server table starts with a small letter and can contain only "_", "-", numbers and alphabets. I know that for this I can use the CLR function for SQL Server. However, I am trying to perform this check using scalar UDF and can do very little here ... I can use "DO NOT LIKE", but I'm not sure how to make sure that I check the string regardless of character order or in other words, write a template in SQL for this. Am I better off using the SQL CLR function? Any help would be appreciated.

Thanks in advance

Thanks to everyone for their comments. This morning I selected the CLR function. For the goal I was trying to achieve, I created one CLR function that does an input string check and calls it from SQL UDF, and it works well.

Just to measure the performance of t-SQL UDF using the SQL CLR vs t-SQL UDF function, I created a SQL CLR function that will simply check if the input line contains only small letters, it should return true else false and have which is called from UDF (IsLowerCaseCLR). After that, I also created the regular t-SQL UDF (IsLowerCaseTSQL), which does the same thing using "NOT LIKE". Then I created a table (Person) with columns Name (varchar) and IsValid (bit) and populated them with names for testing.

Data: - 1000 records with "Ashish" as the value for the Name column 1000 records with "ashish" as the value for the Name column

then I ran the following: UPDATE Person Set IsValid = 1 WHERE dbo.IsLowerCaseTSQL (Name) Above the updated 1000 records (with Isvalid = 1) and took less than a second.

I deleted all the data in the table and re-populated it with the same data. Then I updated the same table using the Sql CLR UDF (with Isvalid = 1) and it took 3 seconds!

If updating occurs for 5000 records, regular UDF takes 0 seconds compared to CLR UDF, which takes 16 seconds!

t-SQL, . , , , CLR SQL, . SQL CLR, , , . SQL.

. . , , - .

.

+3
2
WHERE
    ASCII(LEFT(column, 1)) BETWEEN ASCII('a') AND ASCII('z')
    AND
    column COLLATE LATIN1_GENERAL_BIN NOT LIKE '%[^-_a-zA-Z0-9]%'

COLLATE (Γ€ Γ  ΓΆ ..)

+4

CLR , UDF - CLR, . PATINDEX regex, :

WHERE PATINDEX('%[regex]%', t.column) > 0

... , , PATINDEX , . , .

+2

Source: https://habr.com/ru/post/1736766/


All Articles