comparing two strings in SQL Server

前端 未结 1 542
耶瑟儿~
耶瑟儿~ 2021-02-13 00:57

Is there any way to compare two strings in SQL Server 2008 stored procedure like below?

int returnval = STRCMP(str1, str2)
  • returns 0 if t
1条回答
  •  情书的邮戳
    2021-02-13 01:25

    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

    • 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

    0 讨论(0)
提交回复
热议问题