简体   繁体   English

在SQL Server中接受所有varchar类型并返回特定日期时间格式的泛型函数

[英]Generic function which accept all varchar type and return specific datetime format in Sql server

Input: 输入:

11-07-2016 14:21:59 
08/07/2016 5:12:52 PM

Output: 输出:

2016-07-11 14:21:59
2016-07-08 17:12:52

My date in the format of dd-mm-yyyy hh:mm:ss 我的日期格式为dd-mm-yyyy hh:mm:ss

The input need to return in specific format as yyyy-mm-dd hh:mm:ss 输入需要以yyyy-mm-dd hh:mm:ss特定格式返回

Please suggest. 请提出建议。

Using the below function you can get your date into the specific format: 使用以下功能,您可以将日期更改为特定格式:

CREATE FUNCTION [dbo].[fn_ReturnSpecificDateFormat]
(@InputDate VARCHAR (100))
RETURNS VARCHAR(50)
AS
BEGIN

    DECLARE @RetrunFormat AS VARCHAR (19) = '';

    IF CHARINDEX(' ', @InputDate, 1) = 9
        SET @RetrunFormat = CONVERT(VARCHAR(19), CONVERT(DATETIME, @InputDate, 5), 120)
    ELSE
        SET @RetrunFormat = CONVERT(VARCHAR(19), CONVERT(DATETIME, @InputDate, 105), 120) 

    RETURN @RetrunFormat

END

Sample execution with the given sample data: 使用给定的样本数据执行样本:

DECLARE @DateSpecificFormat TABLE (TestDate VARCHAR (100));

INSERT INTO @DateSpecificFormat (TestDate) VALUES
('17/07/16 2:56:20 PM'), 
('11-07-2016 14:21:59'),
('08/07/2016 5:12:52 PM'),
('14-07-2016 05:12:52 PM');

SELECT TestDate, [dbo].[fn_ReturnSpecificDateFormat] (TestDate) AS SpecificFormat
FROM @DateSpecificFormat;

Result: 结果:

TestDate                 SpecificFormat
----------------------   --------------------
17/07/16 2:56:20 PM      2016-07-17 14:56:20
11-07-2016 14:21:59      2016-07-11 14:21:59
08/07/2016 5:12:52 PM    2016-07-08 17:12:52
14-07-2016 05:12:52 PM   2016-07-14 17:12:52

This page contains various number of date format. 此页面包含各种日期格式。

1) A simple COALESCE call with TRY_CONVERT for every date/time style should be enough. 1)对于每个日期/时间样式,只需使用TRY_CONVERT进行简单的COALESCE调用就足够了。 You don't have to create a dedicated UDF: 您不必创建专用的UDF:

SELECT  x.VarCharCol, 
    COALESCE( TRY_CONVERT(DATETIME, NULLIF(x.VarCharCol, ''), 103), TRY_CONVERT(DATETIME, NULLIF(x.VarCharCol, ''), 105) ) AS DateTimeCol,
    CONVERT( VARCHAR(100), COALESCE( TRY_CONVERT(DATETIME, NULLIF(x.VarCharCol, ''), 103), TRY_CONVERT(DATETIME, NULLIF(x.VarCharCol, ''), 105) ), 120) AS DTAsVarCharWithCustomFormat
FROM    (VALUES 
    ('11-07-2016 14:21:59 '),
    ('08/07/2016 5:12:52 PM'),
    (''),
    (NULL),
    ('dafdf')
) x(VarCharCol)
GO
/*
VarCharCol            DateTimeCol             DTAsVarCharWithCustomFormat
--------------------- ----------------------- ---------------------------
11-07-2016 14:21:59   2016-07-11 14:21:59.000 2016-07-11 14:21:59
08/07/2016 5:12:52 PM 2016-07-08 17:12:52.000 2016-07-08 17:12:52
                      NULL                    NULL
NULL                  NULL                    NULL
dafdf                 NULL                    NULL
*/

103 = dd/mm/yyyy 105 = dd-mm-yyyy 120 = yyyy-mm-dd hh:mi:ss(24h) 103 = dd / mm / yyyy 105 = dd-mm-yyyy 120 = yyyy-mm-dd hh:mi:ss(24h)

2) But if I would have to create a UDF I would choose an inline UDF. 2)但是,如果必须创建UDF,则可以选择内联UDF。 Usually, inline UDF have a better performance compared with scalar function. 通常,与标量函数相比,嵌入式UDF具有更好的性能。

CREATE FUNCTION dbo.GetDateTimeValueFromString(@VarCharValue VARCHAR(50))
RETURNS TABLE
AS
RETURN
SELECT COALESCE( TRY_CONVERT(DATETIME, NULLIF(@VarCharValue, ''), 103), TRY_CONVERT(DATETIME, NULLIF(@VarCharValue, ''), 105) ) AS DateTimeValue
GO

SELECT  x.VarCharCol, 
    y.DateTimeValue AS DateTimeCol,
    CONVERT( VARCHAR(100), y.DateTimeValue, 120) AS DTAsVarCharWithCustomFormat
FROM    (VALUES 
    ('11-07-2016 14:21:59 '),
    ('08/07/2016 5:12:52 PM'),
    (''),
    (NULL),
    ('dafdf')
) x(VarCharCol)
OUTER APPLY dbo.GetDateTimeValueFromString(x.VarCharCol) AS y
GO

Note: for scalar UDF I would use WITH SCHEMABINDING 注意:对于标量UDF,我将使用WITH SCHEMABINDING

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM