使用case语句创建用户定义的函数

时间:2017-11-07 14:24:46

标签: sql-server tsql

我正在尝试创建一个UDF,将字符串从列转换为日期。我有一个列(last_time_received_services),其中句点被称为字符串。我可以使用以下case语句将其成功转换为日期。如何将此case语句写入函数,以便将此函数应用于整个列?

case 
        when last_time_received_services like 'Jan-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'01'+ '-' +'01' as date) --1
        when last_time_received_services like 'Feb-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'02'+ '-' +'01' as date)--2
        when last_time_received_services like 'Mar-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'03'+ '-' +'01'as date) --3
        when last_time_received_services like 'Apr-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'04'+ '-' +'01' as date)--4
        when last_time_received_services like 'May-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'05'+ '-' +'01' as date)--5
        when last_time_received_services like 'Jun-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'06'+ '-' +'01' as date)--6
        when last_time_received_services like 'Jul-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'07'+ '-' +'01' as date)--7
        when last_time_received_services like 'Aug-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'08'+ '-' +'01' as date)--8
        when last_time_received_services like 'Sep-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'09'+ '-' +'01' as date)--9
        when last_time_received_services like 'Oct-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'10'+ '-' +'01' as date)--10
        when last_time_received_services like 'Nov-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'11'+ '-' +'01' as date)--11
        when last_time_received_services like 'Dec-%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'12'+ '-' +'01' as date)--12
        --Quarterly Payments
        when last_time_received_services like '%- March%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'03'+ '-' +'01'as date) --3
        when last_time_received_services like '%- June%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'06'+ '-' +'01' as date)--6
        when last_time_received_services like '%- September%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'09'+ '-' +'01'as date) --9
        when last_time_received_services like '%- December%' then  cast(('20' + (right(last_time_received_services,2))) + '-' +'12'+ '-' +'01' as date)--12
        else ''
    end  as converted_last_service_date

2 个答案:

答案 0 :(得分:1)

理想情况下,您可能希望将其设为内联表值函数而不是标量函数。把它变成一个函数非常简单。而不是列,你需要一个参数。

#!/bin/sed -f

# usage: $0

# make this:
#
#     echo "'special   chars'"
#
# into this:
#
#     'echo "'\''special   chars'\''"'
#

# escape all '
s/'/'\\''/g

# enclose in '
1 s/^/'/
$ s/$/'/

为了演示如何使用内联表值函数,您需要执行类似这样的操作。内联表值函数的性能增益是惊人的。

create function ConvertMyDate
(
    @last_time_received_services varchar(100) --or whatever is appropriate
) returns table as return

    select 
        case 
            when @last_time_received_services like 'Jan-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'01'+ '-' +'01' as date) --1
            when @last_time_received_services like 'Feb-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'02'+ '-' +'01' as date)--2
            when @last_time_received_services like 'Mar-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'03'+ '-' +'01'as date) --3
            when @last_time_received_services like 'Apr-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'04'+ '-' +'01' as date)--4
            when @last_time_received_services like 'May-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'05'+ '-' +'01' as date)--5
            when @last_time_received_services like 'Jun-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'06'+ '-' +'01' as date)--6
            when @last_time_received_services like 'Jul-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'07'+ '-' +'01' as date)--7
            when @last_time_received_services like 'Aug-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'08'+ '-' +'01' as date)--8
            when @last_time_received_services like 'Sep-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'09'+ '-' +'01' as date)--9
            when @last_time_received_services like 'Oct-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'10'+ '-' +'01' as date)--10
            when @last_time_received_services like 'Nov-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'11'+ '-' +'01' as date)--11
            when @last_time_received_services like 'Dec-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'12'+ '-' +'01' as date)--12
            --Quarterly Payments
            when @last_time_received_services like '%- March%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'03'+ '-' +'01'as date) --3
            when @last_time_received_services like '%- June%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'06'+ '-' +'01' as date)--6
            when @last_time_received_services like '%- September%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'09'+ '-' +'01'as date) --9
            when @last_time_received_services like '%- December%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'12'+ '-' +'01' as date)--12
            else ''
        end  as converted_last_service_date
GO

答案 1 :(得分:0)

我认为use可以创建一个标量值函数

CREATE FUNCTION ConvertStringToDate
(   
    @last_time_received_services NVARCHAR(150)
)
RETURNS DATE
AS
BEGIN
    DECLARE @dateResult DATE
    select @dateResult =
    case 
        when @last_time_received_services like 'Jan-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'01'+ '-' +'01' as date) --1
        when @last_time_received_services like 'Feb-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'02'+ '-' +'01' as date)--2
        when @last_time_received_services like 'Mar-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'03'+ '-' +'01'as date) --3
        when @last_time_received_services like 'Apr-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'04'+ '-' +'01' as date)--4
        when @last_time_received_services like 'May-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'05'+ '-' +'01' as date)--5
        when @last_time_received_services like 'Jun-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'06'+ '-' +'01' as date)--6
        when @last_time_received_services like 'Jul-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'07'+ '-' +'01' as date)--7
        when @last_time_received_services like 'Aug-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'08'+ '-' +'01' as date)--8
        when @last_time_received_services like 'Sep-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'09'+ '-' +'01' as date)--9
        when @last_time_received_services like 'Oct-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'10'+ '-' +'01' as date)--10
        when @last_time_received_services like 'Nov-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'11'+ '-' +'01' as date)--11
        when @last_time_received_services like 'Dec-%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'12'+ '-' +'01' as date)--12
        --Quarterly Payments
        when @last_time_received_services like '%- March%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'03'+ '-' +'01'as date) --3
        when @last_time_received_services like '%- June%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'06'+ '-' +'01' as date)--6
        when @last_time_received_services like '%- September%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'09'+ '-' +'01'as date) --9
        when @last_time_received_services like '%- December%' then  cast(('20' + (right(@last_time_received_services,2))) + '-' +'12'+ '-' +'01' as date)--12
        else ''
    end 

    RETURN @dateResult

END
GO

在您的查询中使用它,如

SELECT dbo.ConvertStringToDate(last_time_received_services)
FROM your_table