sqlserver 自定义日期函数(MMdd)

在项目过程中,需要判断用户的生日,只能取月日格式(MMdd),由于sqlserver没有这种格式,所以写了一个自定义的函数。

可能会有其他的解决方法,但是本人是第一次使用sqlserver,对此也不了解。所以能想到的就是写个自定义函数了。

if object_id('getMonthAndDay') is not null
    drop function getMonthAndDay
go
create function getMonthAndDay(@dt datetime)
returns varchar(4)
as
begin
		DECLARE @now VARCHAR(8)
	  set @now = CONVERT(varchar(12), @dt, 112)
    set @now = substring(@now,5,8)
    return @now
end
go

使用:

1. 

select dbo.getMonthAndDay('2010-04-06 16:52:04.093');

返回结果 

0406

2. 

select dbo.getMonthAndDay(MANAGER_BIRTHDAY) from RM_CUST;

猜你喜欢

转载自xurichusheng.iteye.com/blog/1833088
今日推荐