
程序代码:
如果你的【有效期】这一列的数据类型是varchar类型并且数据格式是'xxxx年xx月xx日'的话,
那么可以仿照下面的代码来实现,先定义一个实现这个功能的函数,然后调用这个函数来更新。
----------Create Table Named TestDate----------
IF EXISTS (SELECT * FROM sys.tables WHERE object_id = OBJECT_ID(N'[dbo].[TestDate]') AND type in (N'U'))
DROP TABLE [dbo].[TestDate]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TestDate](
[有效期] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
----------insert data for test----------
Delete From [dbo].[TestDate]
GO
Insert Into TestDate(有效期) Values('2010年1月1日')
GO
Insert Into TestDate(有效期) Values('2010年3月1日')
GO
Insert Into TestDate(有效期) Values('2010年5月1日')
GO
Insert Into TestDate(有效期) Values('2010年7月1日')
GO
Insert Into TestDate(有效期) Values('2010年8月1日')
GO
Insert Into TestDate(有效期) Values('2010年10月1日')
GO
Insert Into TestDate(有效期) Values('2010年12月1日')
GO
Insert Into TestDate(有效期) Values('2010年4月1日')
GO
Insert Into TestDate(有效期) Values('2010年6月1日')
GO
Insert Into TestDate(有效期) Values('2010年9月1日')
GO
Insert Into TestDate(有效期) Values('2010年11月1日')
GO
Insert Into TestDate(有效期) Values('2010年2月1日')
GO
Insert Into TestDate(有效期) Values('2000年2月1日')
GO
Insert Into TestDate(有效期) Values('2008年2月1日')
GO
Insert Into TestDate(有效期) Values('1900年2月1日')
GO
----------Create Function Named ModifyDate----------
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ModifyDate]') AND type in (N'FN',N'TF',N'IF'))
DROP FUNCTION [dbo].[ModifyDate]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ModifyDate](@DateValue varchar(50))
RETURNS varchar(50)
AS
BEGIN
declare @ReturnValue varchar(50) set @ReturnValue=''
declare @Year int
set @Year=charindex('年',@DateValue)
declare @Month int
set @Month=charindex('月',@DateValue)
declare @Day int
set @Day=charindex('日',@DateValue)
if (select count(*) from (select substring(@DateValue,@Year+1,@Month-@Year-1) as [Month]) as M
where [Month] in (1,3,5,7,8,10,12))>0
set @ReturnValue=substring(@DateValue,0,@Month+1)+'31'+substring(@DateValue,@Day,len(@DateValue)+1-@Day)
if (select count(*) from(select substring(@DateValue,@Year+1,@Month-@Year-1) as [Month]) as M
where [Month] in (4,6,9,11))>0
set @ReturnValue=substring(@DateValue,0,@Month+1)+'30'+substring(@DateValue,@Day,len(@DateValue)+1-@Day)
if (select count(*) from (select substring(@DateValue,@Year+1,@Month-@Year-1) as [Month]) as M
where [Month]=2)>0
begin
declare @YearNumber int
set @YearNumber=cast(substring(@DateValue,0,@Year) as int)
if @YearNumber%400=0 or (@YearNumber%100<>0 and @YearNumber%4=0)
set @ReturnValue=substring(@DateValue,0,@Month+1)+'29'+substring(@DateValue,@Day,len(@DateValue)+1-@Day)
else
set @ReturnValue=substring(@DateValue,0,@Month+1)+'28'+substring(@DateValue,@Day,len(@DateValue)+1-@Day)
end
return @ReturnValue
END
GO
----------------To see the data before update---------------------------------------
select * from [TestDate]
Go
----------------update what you wanted---------------------------------------
update [TestDate] set [有效期]=[dbo].ModifyDate([有效期])
where [有效期] is not null
and len([有效期])>0
and charindex('年',[有效期])>0
and charindex('月',[有效期])>0
and charindex('日',[有效期])>0
Go
----------------To see the data after you updated---------------------------------------
select * from [TestDate]
Go
执行结果如下:
[
本帖最后由 303770957 于 2013-8-19 12:10 编辑 ]