Showing posts with label constraint. Show all posts
Showing posts with label constraint. Show all posts

Friday, March 30, 2012

Numeric value only for a character field

Hi
I have a character field (char ot varchar) that I want to force only to
contain numeric characters.
Can that be done by way of defining a constraint on the field ?
or by any other way in the field/table definition ?
What id the syntax ?
Anyone have examples ?

Thanks

David GreenbergOn Thu, 02 Aug 2007 10:05:40 +0200, David Greenberg wrote:

Quote:

Originally Posted by

>Hi
>I have a character field (char ot varchar) that I want to force only to
>contain numeric characters.
>Can that be done by way of defining a constraint on the field ?
>or by any other way in the field/table definition ?
>What id the syntax ?
>Anyone have examples ?


Hi David,

The obvious solution is to declare the column with one of the numeric
data types instead of char or varchar.

But if you insist on using character columns for numeric data, you can
add a CHECK constraint:

CREATE TABLE Example
(SomeCol char(9) CHECK (SomeCol NOT LIKE '%[^0-9]%'));

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Friday, March 9, 2012

Nullable Unique Constraint

I have a table and want to put a unique constraint on a nullable column.
I don't think I can do this and I think I will have to check for uniqueness
on
an Insert / Update trigger (selecting from the inserted buffer where column
is
not null).
Is there a way to do this with a constraint instead of a trigger?WJ
Thank to Steve Kass
CREATE TABLE dupNulls (
pk int identity(1,1) primary key,
X int NULL,
nullbuster as (case when X is null then pk else 0 end),
CONSTRAINT dupNulls_uqX UNIQUE (X,nullbuster)
)
INSERT INTO dupNulls(X) VALUES (1)
INSERT INTO dupNulls(X) VALUES (NULL)
INSERT INTO dupNulls(X) VALUES (NULL)
GO
SELECT pk, X, nullbuster FROM dupNulls
UPDATE dupNulls SET X = 1 WHERE pk = 2
GO
SELECT pk, X, nullbuster FROM dupNulls
UPDATE dupNulls SET X = 2 WHERE pk = 2
SELECT pk, X, nullbuster FROM dupNulls
DROP TABLE dupNulls
"WJ" <wj@.nospam.com> wrote in message
news:%23FteefWbFHA.3196@.TK2MSFTNGP14.phx.gbl...
> I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for
uniqueness on
> an Insert / Update trigger (selecting from the inserted buffer where
column is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
>|||You can do it, but nulls are considered equal in this context, so only one
null value will be allowed which may not be what you want.
Alternative is create a view that includes the nullable column but excludes
the rows where this column is null. Then create an index on the view. Either
way is typically better than via trigger.
Mike John
"WJ" <wj@.nospam.com> wrote in message
news:%23FteefWbFHA.3196@.TK2MSFTNGP14.phx.gbl...
>I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for
> uniqueness on
> an Insert / Update trigger (selecting from the inserted buffer where
> column is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
>|||Another solution is to create view as
SELECT MyUniqueColumn FROM ... WHERE MyUniqueColumn IS NOT NULL
and uniquely index the MyUniqueColumn
HTH,
Gert-Jan
WJ wrote:
> I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for uniquenes
s on
> an Insert / Update trigger (selecting from the inserted buffer where colum
n is
> not null).
> Is there a way to do this with a constraint instead of a trigger?