--Function:格式化字串類型的日期表達式
--Author: Benjamin Zhou
--2008-05-22
CREATE FUNCTION [dbo].[FormatVarcharDatetime]
(@VarDatetime varchar(50))
RETURNS datetime AS
BEGIN
Declare @SplitFlag int
Declare @SplitStr varchar(10)
Declare @DateStr varchar(50)
Declare @TimeStr Varchar(50)
Set @VarDatetime=Replace(@VarDatetime,'AM','Split')
Set @VarDatetime=Replace(@VarDatetime,'PM','Split')
Set @VarDatetime=Replace(@VarDatetime,'上午','Split')
Set @VarDatetime=Replace(@VarDatetime,'下午','Split')
Set @SplitStr='Split'
Set @SplitFlag=charIndex(@SplitStr,@VarDatetime)
If @SplitFlag<>0
Begin
Set @DateStr=RTrim(LTrim(substring(@VarDatetime,0,@SplitFlag)))
Set @TimeStr=RTrim(LTrim(substring(@VarDatetime,@SplitFlag+Len(@SplitStr),Len(@VarDatetime)-@SplitFlag)))
End
return Cast(@DateStr+' '+@TimeStr as datetime)
END