Is there any way to compare two strings in SQL Server 2008 stored procedure like below?
int returnval = STRCMP(str1, str2)
Above method I find in the MySQL but not in SQL Server.
In SQL Server, there are many built-in string functions that can be used by developers. We can compare strings by using the IF-ELSE statement.
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
We can compare two or more strings using the STRCMP string function, LIKE operator, and Equal operator.
String strcmp() function in C++ In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same.
There is no direct string compare function in SQL Server
CASE
WHEN str1 = str2 THEN 0
WHEN str1 < str2 THEN -1
WHEN str1 > str2 THEN 1
ELSE NULL --one of the strings is NULL so won't compare (added on edit)
END
Notes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With