Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. String Comparison in MS SQL

String Comparison in MS SQL

Scheduled Pinned Locked Moved Database
databasetutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TPN
    wrote on last edited by
    #1

    As you know, the =, like operators for string comparison in MS SQL are just for not-case sensitive string comparison. So my concern is how to compare two strings in case sensitive mode? Thank in advance!

    C M 2 Replies Last reply
    0
    • T TPN

      As you know, the =, like operators for string comparison in MS SQL are just for not-case sensitive string comparison. So my concern is how to compare two strings in case sensitive mode? Thank in advance!

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      It is to do with the collation sequence. If the collation sequence is set to a Case-sensitive one then you will get case sensitive comparisons. SQL Server by default uses, IIRC, Latin1_General_AS_CI which is Accent Sensitive & Case Insensitive


      Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

      1 Reply Last reply
      0
      • T TPN

        As you know, the =, like operators for string comparison in MS SQL are just for not-case sensitive string comparison. So my concern is how to compare two strings in case sensitive mode? Thank in advance!

        M Offline
        M Offline
        Michael Potter
        wrote on last edited by
        #3

        Cast the string to varbinarys and then compare - here is a small script you can run in Query Analyzer to test it if you would like. DECLARE @Str1 VARCHAR(20) DECLARE @Str2 VARCHAR(20) DECLARE @Str3 VARCHAR(20) SET @Str1 = 'abcdefg' SET @str2 = 'ABCDEFG' SET @Str3 = 'abcdefg' IF @Str1 = @str2 Print 'Case Insentative: Equal' ELSE Print 'Case Insentative: Not Equal' IF CAST(@Str1 AS VARBINARY(50)) = CAST(@Str2 AS VARBINARY(50)) Print 'Case Sensitive 1: Equal' ELSE Print 'Case Sensitive 1: Not Equal' IF CAST(@Str1 AS VARBINARY(50)) = CAST(@Str3 AS VARBINARY(50)) Print 'Case Sensitive 2: Equal' ELSE Print 'Case Sensitive 2: Not Equal'

        T 1 Reply Last reply
        0
        • M Michael Potter

          Cast the string to varbinarys and then compare - here is a small script you can run in Query Analyzer to test it if you would like. DECLARE @Str1 VARCHAR(20) DECLARE @Str2 VARCHAR(20) DECLARE @Str3 VARCHAR(20) SET @Str1 = 'abcdefg' SET @str2 = 'ABCDEFG' SET @Str3 = 'abcdefg' IF @Str1 = @str2 Print 'Case Insentative: Equal' ELSE Print 'Case Insentative: Not Equal' IF CAST(@Str1 AS VARBINARY(50)) = CAST(@Str2 AS VARBINARY(50)) Print 'Case Sensitive 1: Equal' ELSE Print 'Case Sensitive 1: Not Equal' IF CAST(@Str1 AS VARBINARY(50)) = CAST(@Str3 AS VARBINARY(50)) Print 'Case Sensitive 2: Equal' ELSE Print 'Case Sensitive 2: Not Equal'

          T Offline
          T Offline
          TPN
          wrote on last edited by
          #4

          That's great! Thanks all of you!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups