all groups > sql server programming > february 2007 >
You're in the

sql server programming

group:

Force column UPPERCASE


Force column UPPERCASE jlewis
2/4/2007 2:59:00 PM
sql server programming:
Below is a query I'm working on. I want to force the Gender column to be
UPPERCASE but can't figure out how to do it. Any help is GREATLY
appreciated!!
---------------------------------------------------------

CREATE TABLE Person
(PersonID INT CONSTRAINT ROWpk_person PRIMARY KEY,
LastName NVARCHAR(50) CONSTRAINT ROWnn_person_lname NOT NULL,
FirstName NVARCHAR(50) CONSTRAINT ROWnn_person_fname NOT NULL,
Gender NVARCHAR(50) DEFAULT 'F' CONSTRAINT ROWck_person_gender
CHECK ((Gender='F') or (Gender='M')),
Weight INT CONSTRAINT ROWck_person_weight
CHECK ((Weight>84) AND (Weight<251)),
DateOfBirth DATETIME,
Re: Force column UPPERCASE --CELKO--
2/4/2007 5:06:08 PM
[quoted text, click to view]

The ISO standard uses a numeric value and not a language dependent
abbreviation. 0 = unknown, 1 = male, 2 = female and 9 = "lawful
person" like a corporation.

If you spend some time doing your research and thinking about the data
types instead of taking the cowboy coder "Git'er done fast" approach,
your data quality, portability and ease of maintaining the DB will
improve quite a bit. Why did you have a NVARCHAR(50) for the gender
code?? Can you give me an example of such a thing in Chinese? Trust
me, some user will do just that because you did not prevent it like
you are supposed to.
Re: Force column UPPERCASE Erland Sommarskog
2/5/2007 12:03:40 AM
jlewis (jlewis@discussions.microsoft.com) writes:
[quoted text, click to view]

First, Gender should be char(1), not nvarchar(50), since you are only
permitting two values, and none of them are Unicode values.

Here is a way to write the constraint:

CHECK (Gender COLLATE Finnosh_Swedish_BIN IN ('F', 'M'))

by forcing a binary collation in the check, you make the test
case-sensitive. You can pick any collation you like as long as its
binary or case-sensitive.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Force column UPPERCASE jlewis
2/5/2007 7:33:00 AM
This was the best solution for me. I never would have thought of this. I
went online and looked up the ASCII code to see how you got the numbers 70 &
77. Thank you for your help!

[quoted text, click to view]
Re: Force column UPPERCASE Tony Rogerson
2/5/2007 7:59:43 AM
Assuming you use CHAR(1) and hold F or M instead...

check( ascii( gender ) in ( 70, 77 ) )

[quoted text, click to view]
AddThis Social Bookmark Button