表の変更Garantor列の変更[誕生日]int
7518 次
2 に答える
3
このようなことを試してください(新しい列を作成し、変換で更新し、古い列を削除してから、新しい列の名前を古い名前に変更する必要があります)
ALTER TABLE dbo.Garantor
ADD newBirthDate int NOT NULL DEFAULT 0 -- NULL and DEFAULT as required
GO
UPDATE dbo.Garantor
SET newBirthDate = CAST([Birth Date] AS int) -- or CONVERT function, it will do the same
GO
ALTER TABLE dbo.Garantor
DROP COLUMN [Birth Date]
GO
SP_RENAME 'dbo.Garantor.newBirthDate', 'dbo.Garantor.[Birth Date]'
GO
于 2012-09-07T11:09:44.120 に答える
1
Convertは私には機能しません。キャストも機能しません。列のデータ型を変更するために、機能したコードは次のとおりです。「date」タイプを「int」タイプに変更し、年のみを保持します。
これは私のために働きます:
ALTER TABLE [dbo].[Garantor]
ADD [newBirthDate] int NOT NULL DEFAULT 0
GO
UPDATE [dbo].[Garantor]
SET [newBirthDate] = DATEPART(yyyy,[Birth Date])
GO
ALTER TABLE [dbo].[Garantor]
DROP COLUMN [Birth Date]
GO
SP_RENAME 'dbo.Garantor.newBirthDate', 'dbo.Garantor.[Birth Date]'
GO
別の解決策は次のとおりです。
= YEAR([Birth Date])
また、テーブルにインデックスがある場合:
ALTER TABLE [dbo].[Garantor]
ADD [newBirthDate] int NOT NULL DEFAULT 0
GO
UPDATE [dbo].[Garantor]
SET [newBirthDate] = DATEPART(yyyy,[Birth Date])
GO
ALTER TABLE [dbo].[Garantor] DROP CONSTRAINT [UQ__Garantor__1C123681D17FE31B] -- [UQ__Garantor__1C123681D17FE31B] change with your
GO
ALTER TABLE [dbo].[Garantor]
DROP COLUMN [Birth Date]
GO
SP_RENAME 'dbo.Garantor.newBirthDate', 'dbo.Garantor.[Birth Date]'
GO
ALTER TABLE [dbo].[Garantor] ADD UNIQUE NONCLUSTERED
(
[Birth Date] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
于 2021-11-18T10:07:01.163 に答える