日期:2014-05-17 浏览次数:20733 次
if OBJECT_ID('f_ToDate') is not null
drop function f_ToDate
go
create function f_ToDate
(
@Date varchar(50)
)
returns datetime
as
begin
--declare @Date varchar(50)='15-3-2011 0:00:00'
declare @day varchar(2)
declare @month varchar(2)
declare @year varchar(4)
select @day=left(@Date,CHARINDEX('-',@Date)-1)
--print @day
select @month=substring(@Date,CHARINDEX('-',@Date)+1,charindex('-',@Date,CHARINDEX('-',@Date)+1)-CHARINDEX('-',@Date)-1)
--print @month
select @year=right(replace(@Date,' 0:00:00',''),4)
--print @year
return @year+'-'+@month+'-'+@day+right(@Date,8)
end
go
select dbo.f_ToDate('15-3-2011 0:00:00')
/*
2011-03-15 00:00:00.000
*/