1 USE [master]
2 GO
3 /****** Object: UserDefinedFunction [dbo].[dtGetXun] Script Date: 04/06/2016 17:41:12 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 -- =============================================
9 -- Author: <Author,,Name>
10 -- Create date: <Create Date, ,>
11 -- Description: 从datetime类型转换为带旬的格式的字符串
12 -- =============================================
13 CREATE FUNCTION [dbo].[dtGetXun]
14 (
15 -- Add the parameters for the function here
16 @dt datetime
17 )
18 RETURNS varchar(50)
19 AS
20 BEGIN
21 declare @dtWithXun varchar(50)
22 declare @xun varchar(50)
23 if(day(@dt) between 0 and 10)
24 begin
25 set @xun=‘上旬‘
26 end
27 if(day(@dt) between 11 and 20)
28 begin
29 set @xun=‘中旬‘
30 end
31 if(day(@dt) >20)
32 begin
33 set @xun=‘下旬‘
34 end
35
36 select @dtWithXun=cast(year(@dt) as varchar(50))+‘年‘+cast(month(@dt) as varchar(50))+‘月‘+@xun
37 return @dtWithXun
38
39 END