February 22, 2009 at 6:29 am
I want to have a column in an Employee table called Sex in SQL Server 2005 and I want to be able to choose either M or F. How do I set it to only allow M or F and what data type will it need to be?
February 22, 2009 at 6:38 am
You can do it char(1) with check constraint that will limit the values to M or F. BOL has some examples on how to define check constraint (look for it in the article about create table). If there could be a situation that it isnโt clear if it is a male or female (for example you get a letter from someone, and it isnโt clear), then you should make it nullable or consider adding another status that unclear.
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
February 22, 2009 at 6:39 am
use a char1, and simply put a check constraint--
CREATE TABLE blah(
GENDER CHAR(1) NULL DEFAULT 'U' CHECK(GENDER IN('M','F','U') --U for undisclosed? some politically correct places require that nowadays
Lowell
February 22, 2009 at 6:41 am
omg Adi great minds think alike. paraphrased but verbatim answers
Lowell
February 22, 2009 at 7:34 am
I also enjoyed the fact that there was another answer that was the same as mine and came almost at the same time that my answer came. It feels as if it gives more validity to the answer:-)
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
February 22, 2009 at 7:56 am
Thanks guys, the check constrant worked. Is is possible though to have M and F come in as a drop down when doing data entry so that nothing else could be entered or selected or is that not possible?
February 22, 2009 at 8:06 am
Yes, put a dropdown down in whatever application you are building to enter that data.
If it's ssms or QA, then forget about it as it's not possible.
February 22, 2009 at 10:52 am
ISO5218 has the following definitions for Gender
0 = Not Known
1 = Male
2 = Female
9 = Not applicable
If you have the freedom to choose the encoding then start with the ISO standard values.
February 22, 2009 at 11:36 am
David.Poole (2/22/2009)
9 = Not applicable
?????
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 22, 2009 at 11:40 am
... and what's the value for a transexual or people with both sexes?
I'm really asking, just wondering if they covered all bases.
February 22, 2009 at 8:42 pm
Ninja's_RGR'us (2/22/2009)
... and what's the value for a transexual or people with both sexes?I'm really asking, just wondering if they covered all bases.
Heh... too easy... use the BIT datatype... 0 for female, 1 for male, NULL for everyone else. ๐
--Jeff Moden
Change is inevitable... Change for the better is not.
February 22, 2009 at 9:01 pm
I don't like bits for things like this and I'd go with ISO. Who knows what crazy categories someone might assign, so leave room for growth or changes.
February 22, 2009 at 9:48 pm
Steve Jones - Editor (2/22/2009)
I don't like bits for things like this and I'd go with ISO. Who knows what crazy categories someone might assign, so leave room for growth or changes.
http://www.merriam-webster.com/dictionary/joke ๐
--Jeff Moden
Change is inevitable... Change for the better is not.
February 23, 2009 at 8:00 am
Ninja's_RGR'us (2/22/2009)
... and what's the value for a transexual or people with both sexes?I'm really asking, just wondering if they covered all bases.
Totally off topic, but transgendered people generally are considered to be the gender they identify with, not the gender they were born with. So a M-to-F person is considered female.
Hermaphrodites, ok, you got me there... but again if the person identifies with a particular gender it is considerate to accept that statement.
There are also people (I think the term is pansexual but could be misremembering) that consider themselves genderless or find gender to be an irrelevant societal construct... but I think the catch-all category is probably good enough for all exceptions to the rule.
--
Anye Mercy
"Service Unavailable is not an Error" -- John, ENOM support
"You keep using that word. I do not think it means what you think it means." -- Inigo Montoya in "Princess Bride"
"Civilization exists by geologic consent, subject to change without notice." -- Will Durant
February 23, 2009 at 10:51 am
GilaMonster (2/22/2009)
David.Poole (2/22/2009)
9 = Not applicable?????
If you have a contact table and the contact is "The company secretary" then you are trying to contact a title rather than a person so the gender would be 9.
I'm finding that there is an ever increasing requirement to share data externally either through web services, cloud computing or old fashioned methods and trying to provide documentation, support or bespoke services for each partner/client is an absolute nightmare. Sticking to a standard lets us offload the pain.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply