alter table Garantor alter column [Birth Date] 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
转换对我不起作用。演员也不起作用。要更改列的数据类型,下面是有效的代码。将“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 回答