Skip to content Skip to sidebar Skip to footer

Comparing Two Strings In Sql Server

Is there any way to compare two strings in SQL Server 2008 stored procedure like below? int returnval = STRCMP(str1, str2) returns 0 if the strings are the same returns -1 if the

Solution 1:

There is no direct string compare function in SQL Server

CASEWHEN str1 = str2 THEN0WHEN str1 < str2 THEN-1WHEN str1 > str2 THEN1ELSENULL--one of the strings is NULL so won't compare (added on edit)END

Notes

  • you can wraps this via a UDF using CREATE FUNCTION etc
  • you may need NULL handling (in my code above, any NULL will report 1)
  • str1 and str2 will be column names or @variables

Post a Comment for "Comparing Two Strings In Sql Server"