日期:2014-05-16 浏览次数:20661 次
--Sql中自定义方法 create function StrToDateTime(@DateStr as varchar(18)) returns varchar(24) --注意 returns 不是 return as begin declare @date varchar(24); set @date=@DateStr; set @date=(SUBSTRING(cast (@date as varchar(18)),1,4)+'-'+SUBSTRING(cast (@date as varchar(18)),5,2) +'-'+SUBSTRING(cast (@date as varchar(18)),7,2) +' '+SUBSTRING(cast (@date as varchar(18)),9,2) +':'+SUBSTRING(cast (@date as varchar(18)),11,2) +':'+SUBSTRING(cast (@date as varchar(18)),13,2)); return @date; end
declare @returntime varchar(24) exec @returntime=StrToDateTime '20120401145112827' print convert(varchar(24),convert(datetime,@returntime),20)
select * from ( select id,interID,cpid,intername,reply,lasttime,CASE lasttime WHEN '0' THEN getdate()-3 Else cast((SUBSTRING(cast (lasttime as varchar(18)),1,4) +'-'+SUBSTRING(cast (lasttime as varchar(18)),5,2) +'-'+SUBSTRING(cast (lasttime as varchar(18)),7,2) +' '+SUBSTRING(cast (lasttime as varchar(18)),9,2) +':'+SUBSTRING(cast (lasttime as varchar(18)),11,2) +':'+SUBSTRING(cast (lasttime as varchar(18)),13,2) ) as varchar(24)) end as ldate,setTime from xsreuserinter ) as temp where (datediff(hh,temp.ldate,getdate())>72 and datediff(hh,temp.setTime,getdate())>72) order by lasttime desc
select * from ( select id,interID,cpid,intername,reply,lasttime,CASE lasttime WHEN '0' THEN getdate()-3 Else convert(datetime,dbo.StrToDateTime(lasttime),20) --该处调用function end as ldate,setTime from xsreuserinter ) as temp where (datediff(hh,temp.ldate,getdate())>72 and datediff(hh,temp.setTime,getdate())>72) order by lasttime desc