Friday, February 17, 2012

Data Type

I am a SQL newbie! What is the correct datatype to use, if I want to record
a checkbox from a web form? Thanks,
DarrenScrappy (celtics@.lan-specialist.com) writes:
> I am a SQL newbie! What is the correct datatype to use, if I want to
> record a checkbox from a web form? Thanks,

The most natural type to use would be bit, which only can hold the values 0
or 1.

If you want to stick to standards, you may avoid this datatype since it
is propritary to SQL Server and Sybase (or at least so claims Joe Celko).
In such case a char(1) column with the values T/F ot Y/N could be an
alternative. Or a tinyint column with the values 0/1.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||"Erland Sommarskog" <sommar@.algonet.se> wrote in message news:Xns93ACF34D8F415Yazorman@.127.0.0.1...
> Scrappy (celtics@.lan-specialist.com) writes:
> > I am a SQL newbie! What is the correct datatype to use, if I want to
> > record a checkbox from a web form? Thanks,
> The most natural type to use would be bit, which only can hold the values 0
> or 1.
> If you want to stick to standards, you may avoid this datatype since it
> is propritary to SQL Server and Sybase (or at least so claims Joe Celko).

Hi Erland, actually, BIT, and the more general BIT(n), are defined in both
SQL:1992 and SQL:1999. I believe the difference is that the Standard
requires a bit string literal to be written like B'1'. Not sure if the integer
1 would be automatically cast to B'1' but seems reasonable.

Regards,
jag

> In such case a char(1) column with the values T/F ot Y/N could be an
> alternative. Or a tinyint column with the values 0/1.
> --
> Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp|||>> the more general BIT(n), are defined in both SQL:1992 and SQL:1999 <<

Yes, and in the Standards, you treat them as if they were character
strings. The operations are bit string comparison, assignment, bit
concatenation operator, bit substring function, length, and position.
You do not have bit-wise operators like you do in T-SQL or assembly
language.

When you try to use them with a host languages, you get all kinds of
problems -- high end or low end bytes? Does 0 or 1 mean "TRUE"?

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

No comments:

Post a Comment