Showing posts with label zero. Show all posts
Showing posts with label zero. Show all posts

Sunday, March 25, 2012

Changing the identity seed & increment programatically

Hi,
I want to change the identity values of an identity column of a table
that already has rows in it, such that, it begins at zero and goes up
in even numbers - that is, like 0, 2, 4, 6, 8, etc... instead of the
usual 1, 2, 3, 4, etc...
The current values of the identity seed & identity increment are the
default values of "1" respectively.
I'm going to use the following commands. Let me know if you think
these commands will do the trick will you please?. The table name is
"A" for the sake of simplicity:-
Firstly change the identity increment value to 2
ALTER TABLE A
ALTER COLUMN identityColumn
IDENTITY (1, 2)
Then reseed all values so that odd become
DBCC CHECKIDENT('A', RESEED, 0)
Comments/corrections/suggestions much appreciated.
Al.Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<almurph@.altavista.com> wrote in message
news:1157033831.447625.273890@.b28g2000cwb.googlegroups.com...
> Hi,
>
> I want to change the identity values of an identity column of a table
> that already has rows in it, such that, it begins at zero and goes up
> in even numbers - that is, like 0, 2, 4, 6, 8, etc... instead of the
> usual 1, 2, 3, 4, etc...
> The current values of the identity seed & identity increment are the
> default values of "1" respectively.
> I'm going to use the following commands. Let me know if you think
> these commands will do the trick will you please?. The table name is
> "A" for the sake of simplicity:-
> Firstly change the identity increment value to 2
> ALTER TABLE A
> ALTER COLUMN identityColumn
> IDENTITY (1, 2)
> Then reseed all values so that odd become
> DBCC CHECKIDENT('A', RESEED, 0)
>
> Comments/corrections/suggestions much appreciated.
> Al.
>|||I think that the best option for you is to create a new table with the
IDENTITY seed value you desire, and copy the current table (without the
IDENTITY column) to the new table, then drop the old table and rename the
new table to the old name.
I don't think that DBCC CHECKIDENT() will renumber the existing data.
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<almurph@.altavista.com> wrote in message
news:1157033831.447625.273890@.b28g2000cwb.googlegroups.com...
> Hi,
>
> I want to change the identity values of an identity column of a table
> that already has rows in it, such that, it begins at zero and goes up
> in even numbers - that is, like 0, 2, 4, 6, 8, etc... instead of the
> usual 1, 2, 3, 4, etc...
> The current values of the identity seed & identity increment are the
> default values of "1" respectively.
> I'm going to use the following commands. Let me know if you think
> these commands will do the trick will you please?. The table name is
> "A" for the sake of simplicity:-
> Firstly change the identity increment value to 2
> ALTER TABLE A
> ALTER COLUMN identityColumn
> IDENTITY (1, 2)
> Then reseed all values so that odd become
> DBCC CHECKIDENT('A', RESEED, 0)
>
> Comments/corrections/suggestions much appreciated.
> Al.
>|||Hi Arnie,
Thanks for that but do you know how I would renumber the values
from 1, 2, 3, 4, to the 1, 3, 5, 7 sequence?
Puzzled,
Al.|||On the new table, set the IDENTITY seed and increment values as you wish
before you copy the old table values.
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<almurph@.altavista.com> wrote in message
news:1157046915.027819.46040@.b28g2000cwb.googlegroups.com...
> Hi Arnie,
> Thanks for that but do you know how I would renumber the values
> from 1, 2, 3, 4, to the 1, 3, 5, 7 sequence?
> Puzzled,
> Al.
>

Changing the identity seed & increment programatically

Hi,
I want to change the identity values of an identity column of a table
that already has rows in it, such that, it begins at zero and goes up
in even numbers - that is, like 0, 2, 4, 6, 8, etc... instead of the
usual 1, 2, 3, 4, etc...
The current values of the identity seed & identity increment are the
default values of "1" respectively.
I'm going to use the following commands. Let me know if you think
these commands will do the trick will you please?. The table name is
"A" for the sake of simplicity:-
Firstly change the identity increment value to 2
ALTER TABLE A
ALTER COLUMN identityColumn
IDENTITY (1, 2)
Then reseed all values so that odd become
DBCC CHECKIDENT('A', RESEED, 0)
Comments/corrections/suggestions much appreciated.
Al.--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<almurph@.altavista.com> wrote in message
news:1157033831.447625.273890@.b28g2000cwb.googlegroups.com...
> Hi,
>
> I want to change the identity values of an identity column of a table
> that already has rows in it, such that, it begins at zero and goes up
> in even numbers - that is, like 0, 2, 4, 6, 8, etc... instead of the
> usual 1, 2, 3, 4, etc...
> The current values of the identity seed & identity increment are the
> default values of "1" respectively.
> I'm going to use the following commands. Let me know if you think
> these commands will do the trick will you please?. The table name is
> "A" for the sake of simplicity:-
> Firstly change the identity increment value to 2
> ALTER TABLE A
> ALTER COLUMN identityColumn
> IDENTITY (1, 2)
> Then reseed all values so that odd become
> DBCC CHECKIDENT('A', RESEED, 0)
>
> Comments/corrections/suggestions much appreciated.
> Al.
>|||I think that the best option for you is to create a new table with the
IDENTITY seed value you desire, and copy the current table (without the
IDENTITY column) to the new table, then drop the old table and rename the
new table to the old name.
I don't think that DBCC CHECKIDENT() will renumber the existing data.
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<almurph@.altavista.com> wrote in message
news:1157033831.447625.273890@.b28g2000cwb.googlegroups.com...
> Hi,
>
> I want to change the identity values of an identity column of a table
> that already has rows in it, such that, it begins at zero and goes up
> in even numbers - that is, like 0, 2, 4, 6, 8, etc... instead of the
> usual 1, 2, 3, 4, etc...
> The current values of the identity seed & identity increment are the
> default values of "1" respectively.
> I'm going to use the following commands. Let me know if you think
> these commands will do the trick will you please?. The table name is
> "A" for the sake of simplicity:-
> Firstly change the identity increment value to 2
> ALTER TABLE A
> ALTER COLUMN identityColumn
> IDENTITY (1, 2)
> Then reseed all values so that odd become
> DBCC CHECKIDENT('A', RESEED, 0)
>
> Comments/corrections/suggestions much appreciated.
> Al.
>|||Hi Arnie,
Thanks for that but do you know how I would renumber the values
from 1, 2, 3, 4, to the 1, 3, 5, 7 sequence?
Puzzled,
Al.|||On the new table, set the IDENTITY seed and increment values as you wish
before you copy the old table values.
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<almurph@.altavista.com> wrote in message
news:1157046915.027819.46040@.b28g2000cwb.googlegroups.com...
> Hi Arnie,
> Thanks for that but do you know how I would renumber the values
> from 1, 2, 3, 4, to the 1, 3, 5, 7 sequence?
> Puzzled,
> Al.
>

Saturday, February 25, 2012

Changing NULL to zero

Greetings folks,
I have a dilemma that I thought was going to be a slam dunk. I have a
table with several integer and money columns that contain about 50%
null values. I need to sum them, and have the nulls treated as zero.
Secondly, I need to insert all of the values into another table. If
there happens to be a null in the source columns, it needs to become a
zero in the destination. (I know someone is going to fuss about the
storing of calculated values. Not my choice, I don't get a say-so).
The tables have millions of records, and there are roughly 40 int and
money columns. So I'm looking for a solution that will perform well,
and will not require me to update those 40 columns individually. Any
suggestions?
thanks,
CL--isnull will return the column if it is not null, and the second value (in
this case 0) if it is null
select isnull(Column1, 0) from table
or
--Coalesce returns the first non-null value in the list. I think if you
have only one value you want to use isnull, but here is is anyway...
select coalesce(column1,0) from table
"CL" <clhawkins74@.yahoo.com> wrote in message
news:1137790173.259283.202260@.g49g2000cwa.googlegroups.com...
> Greetings folks,
> I have a dilemma that I thought was going to be a slam dunk. I have a
> table with several integer and money columns that contain about 50%
> null values. I need to sum them, and have the nulls treated as zero.
> Secondly, I need to insert all of the values into another table. If
> there happens to be a null in the source columns, it needs to become a
> zero in the destination. (I know someone is going to fuss about the
> storing of calculated values. Not my choice, I don't get a say-so).
> The tables have millions of records, and there are roughly 40 int and
> money columns. So I'm looking for a solution that will perform well,
> and will not require me to update those 40 columns individually. Any
> suggestions?
> thanks,
> CL
>

Tuesday, February 14, 2012

Changing default value for a column

I have an existing database and I want to change it so that if no value is
inserted into a column that it populates that column with a zero instead of
a
null value. How do I make this change? I suspect I need to change all the
existing nulls to zero before making the change? Any help would be greatly
appreciated.That depends. Are you happy with the existing nulls? If so, simply add a
default constraint:
alter table MyTable
add
constraint DF1_MyTable default (0) for MyColumn
However, if you need to keep all nulls out, then populate the null columns
with some value and then alter the column:
alter table MyTable
alter column
MyColumn int not null
go
Then, add the default constraint as shown above.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"rk rider" <rkrider@.discussions.microsoft.com> wrote in message
news:722BC848-15EA-474D-8F40-6BB078F7CDF3@.microsoft.com...
I have an existing database and I want to change it so that if no value is
inserted into a column that it populates that column with a zero instead of
a
null value. How do I make this change? I suspect I need to change all the
existing nulls to zero before making the change? Any help would be greatly
appreciated.

Changing default value for a column

I have an existing database and I want to change it so that if no value is
inserted into a column that it populates that column with a zero instead of a
null value. How do I make this change? I suspect I need to change all the
existing nulls to zero before making the change? Any help would be greatly
appreciated.
That depends. Are you happy with the existing nulls? If so, simply add a
default constraint:
alter table MyTable
add
constraint DF1_MyTable default (0) for MyColumn
However, if you need to keep all nulls out, then populate the null columns
with some value and then alter the column:
alter table MyTable
alter column
MyColumn int not null
go
Then, add the default constraint as shown above.
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"rk rider" <rkrider@.discussions.microsoft.com> wrote in message
news:722BC848-15EA-474D-8F40-6BB078F7CDF3@.microsoft.com...
I have an existing database and I want to change it so that if no value is
inserted into a column that it populates that column with a zero instead of
a
null value. How do I make this change? I suspect I need to change all the
existing nulls to zero before making the change? Any help would be greatly
appreciated.

Changing default value for a column

I have an existing database and I want to change it so that if no value is
inserted into a column that it populates that column with a zero instead of a
null value. How do I make this change? I suspect I need to change all the
existing nulls to zero before making the change? Any help would be greatly
appreciated.That depends. Are you happy with the existing nulls? If so, simply add a
default constraint:
alter table MyTable
add
constraint DF1_MyTable default (0) for MyColumn
However, if you need to keep all nulls out, then populate the null columns
with some value and then alter the column:
alter table MyTable
alter column
MyColumn int not null
go
Then, add the default constraint as shown above.
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"rk rider" <rkrider@.discussions.microsoft.com> wrote in message
news:722BC848-15EA-474D-8F40-6BB078F7CDF3@.microsoft.com...
I have an existing database and I want to change it so that if no value is
inserted into a column that it populates that column with a zero instead of
a
null value. How do I make this change? I suspect I need to change all the
existing nulls to zero before making the change? Any help would be greatly
appreciated.