Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« Sybase IQ Is A Columnar Database, Why Should I Care?How to rename a column in a SQL Server table without using the designer »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

How do you select all the rows that contain uppercase characters only? There sre three ways to do this
1 Compare with BINARY_CHECKSUM
2 Use COLLATE
3 Cast to varbinary

Let's first create the table and also some test data

CREATE TABLE #tmp ( x VARCHAR(10) NOT NULL )

  1. INSERT INTO #tmp
  2. SELECT 'Word' UNION ALL
  3. SELECT 'WORD' UNION ALL
  4. SELECT 'ABC' UNION ALL
  5. SELECT 'AbC' UNION ALL
  6. SELECT 'ZxZ' UNION ALL
  7. SELECT 'ZZZ' UNION ALL
  8. SELECT 'word'

if we want only the uppercase columns then this is supposed to be our output

WORD
ABC
ZZZ

Let's get started, first up is BINARY_CHECKSUM

  1. SELECT x
  2. FROM #TMP
  3. WHERE BINARY_CHECKSUM(x) = BINARY_CHECKSUM(UPPER(x))

Second is COLLATE

  1. SELECT x
  2. FROM #TMP
  3. WHERE x = UPPER(x) COLLATE SQL_Latin1_General_CP1_CS_AS

Third is Cast to varbinary

  1. SELECT x
  2. FROM #TMP
  3. WHERE CAST(x AS VARBINARY(10)) = CAST(UPPER(x) AS VARBINARY(10))

Of course if you database is already case sensitive you can just do the following

  1. SELECT x
  2. FROM #TMP
  3. WHERE UPPER(x) = x

That will work, how do you find out what collation was used when your database was created? You can use DATABASEPROPERTYEX for that. I use the model DB here because when you create a new DB by default it inherits all the properties from the model DB.
When I run this

  1. SELECT DATABASEPROPERTYEX( 'model' , 'collation' )

I get this as output: SQL_Latin1_General_CP1_CI_AS

What does all that junk mean? Well let's run the following function (yes those are 2 colons :: )

  1. SELECT *
  2. FROM ::fn_helpcollations ()
  3. WHERE NAME ='SQL_Latin1_General_CP1_CI_AS'

The description column contains this info

Latin1-General, case-insensitive, accent-sensitive,
kanatype-insensitive, width-insensitive for Unicode Data,
SQL Server Sort Order 52 on Code Page 1252 for non-Unicode Data

You can read some more info about Selecting a SQL Collation here: http://msdn2.microsoft.com/en-us/library/aa176552(SQL.80).aspx

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
412 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)