我这里有一个用户定义函数,它返回一个日期时间。我根据原始日期设置了下一个日期的月,日和年。 我想知道如何让函数返回日期时间格式,格式如下:mm / dd / yyyy。
ALTER FUNCTION [dbo].[udf_get_payout_date]
(
-- Add the parameters for the function here
@m_id as int
)
RETURNS Datetime
AS
BEGIN
-- Declare the return variable here
--the original date
DECLARE @m_createddate as Datetime
--the date to be returned
DECLARE @p_date as Datetime
--the day part of the original date
DECLARE @dpart as int
--the month part of the original date
DECLARE @mdate as int
--this determines the day part of the given day ranges
DECLARE @pday as int
--the year part of the original date
DECLARE @ydate as int
--get the original date
SET @m_createddate = (SELECT [m_createddate] FROM rms_month_email where m_id = m_id)
SET @dpart = DATEPART(dd,@m_createddate)
SET @mdate = DATEPART(mm,@m_createddate)
SET @ydate = DATEPART(yyyy,@m_createddate)
--if the dpart ranges between 1 -> 15, the day of pay out will be on the 15th
if @dpart >= 1 and @dpart <= 15
set @pday = 15
else
-- if the dpart ranges between 15-> 16, the day of pay out will be on the 30th
set @pday = 30
--add 1 to month part of the original date to signify that it will be on the next month
SET @mdate = @mdate + 1
if @mdate > 12
SET @mdate = 1
SET @ydate = @ydate + 1
SET @p_date = @mdate+'/'+@pday + '/'+@ydate
-- Return the result of the function
RETURN @p_date
END
但我在值字段中得到null。 你能告诉我的代码有什么问题吗? 非常感谢。
答案 0 :(得分:1)
通过单独打印变量来检查变量是否为空,因为当变量连接变量时,如果任何变量为null,则完整字符串将变为空。
答案 1 :(得分:1)
select convert(varchar(10), cast('2015-10-12' as date), 101)
select convert(varchar(10), cast(<column> as date), 101) from <table>