我想规范化给定数据库的日期时间。我需要能够使用日期来进行统计。这是我到目前为止所做的:
select
CASE PATINDEX('%[0-9]/[0-9]/[0-9][0-9][0-9][0-9] [0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 then 'Pattern1'--CAST([Last Updated] AS Datetime) --2/8/2017 2:30:14 PM
ELSE
CASE PATINDEX('%[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 then 'Pattern2' --2015-03-02 03:46:38 PM
ELSE
CASE PATINDEX('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern3'
ELSE
CASE PATINDEX('%[0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern4'
ELSE
CASE PATINDEX('%[0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] [0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern5'
ELSE
CASE PATINDEX('%[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern6'
ELSE
CASE PATINDEX('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] [0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern7'
ELSE
CASE PATINDEX('%[0-9]/[0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern8'
ELSE
CASE PATINDEX('%[0-9][0-9]/[0-9]/[0-9][0-9][0-9][0-9] [0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern9'
ELSE
CASE PATINDEX('%[0-9][0-9]/[0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]%',[Last Updated])
WHEN 1 THEN 'Pattern10'
END
END
END
END
END
END
END
END
END
END
as 'Pattern'
当我尝试演员作为日期时间时,它会给我一个错误......以下是一些例子:
Pattern1:
2/8/2017 8:06:56 AM
2/2/2017 2:42:09 PM
1/3/2017 9:10:20 AM
Pattern 2:
2016-12-20 11:08:20
2016-11-09 10:04:35
2016-11-01 10:53:11 AM
2017-02-03 09:13:14
2016-11-09 10:09:09
Pattern3:
12/14/2016 11:54:53 AM
12/16/2016 11:05:24 AM
12/19/2016 12:23:51 PM
Pattern4:
8/15/2016 12:13:35 PM
4/17/2015 12:29:54 PM
2/22/2016 10:44:11 AM
6/12/2014 10:08:07 AM
9/16/2013 12:18:22 PM
我希望他们都能按照以下格式进行标准化:' 02/13/2017 11:58:00'
有没有办法做到这一点??
以下是我如何获取日期:
WHEN (SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)) = null THEN 'UNAVAILABLE'
WHEN (SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)) = '' THEN 'UNAVAILABLE'
WHEN (SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)) like '%--%'
THEN 'UNAVAILABLE'
ELSE
(Case
WHEN SUBSTRING(REVERSE((RTRIM(LTRIM((SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)))))),0,5)
--FOR AM
LIKE '%MA%' THEN
SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)
WHEN SUBSTRING(REVERSE((RTRIM(LTRIM((SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)))))),0,5)
--FOR PM
LIKE '%MP%' THEN
SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)
ELSE
(CASE
WHEN LEN(RTRIM(LTRIM((SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22))))) <20
THEN 'UNAVAILABLE'
ELSE
LEFT(RTRIM(LTRIM((SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)))),
LEN(RTRIM(LTRIM((SUBSTRING(SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100),PATINDEX('%[0-9]%',SUBSTRING(Cast(Resolution as varchar(8000)),LEN(/**/Cast(Resolution as varchar(8000))/**/) - CHARINDEX('---------------------------------------------',REVERSE(/**/Cast(Resolution as varchar(8000))/**/)),100)),22)))))-2)
END)
END)
END)
as 'Last Updated'
这是来自日志系统。我无法提供太多信息,因为这些都是保密信息。但请注意,有许多人在评论结束时将时间戳放在评论中,其中包括他们的姓名和时间戳。通过那里的查询,我能够获得大部分日期。有时候没有办法获得日期(日志中根本没有时间戳)。这是我可以给出的例子:
Test Data
---------------------------------------------
FName LName 2/13/2017 1:19:42 PM
答案 0 :(得分:1)
我认为您的问题有一个更简单的解决方案。这是一个命题:
SELECT CONVERT(datetime, any_datetime_format) unified_datetime
from
(
SELECT '2/8/2017 8:06:56 AM' any_datetime_format UNION
SELECT '2/2/2017 2:42:09 PM' any_datetime_format UNION
SELECT '1/3/2017 9:10:20 AM' any_datetime_format UNION
SELECT '2016-12-20 11:08:20' any_datetime_format UNION
SELECT '2016-11-09 10:04:35' any_datetime_format UNION
SELECT '2016-11-01 10:53:11 AM' any_datetime_format UNION
SELECT '2017-02-03 09:13:14' any_datetime_format UNION
SELECT '2016-11-09 10:09:09' any_datetime_format UNION
SELECT '12/14/2016 11:54:53 AM' any_datetime_format UNION
SELECT '12/16/2016 11:05:24 AM' any_datetime_format UNION
SELECT '12/19/2016 12:23:51 PM' any_datetime_format UNION
SELECT '8/15/2016 12:13:35 PM' any_datetime_format UNION
SELECT '4/17/2015 12:29:54 PM' any_datetime_format UNION
SELECT '2/22/2016 10:44:11 AM' any_datetime_format UNION
SELECT '6/12/2014 10:08:07 AM' any_datetime_format UNION
SELECT '9/16/2013 12:18:22 PM' any_datetime_format
) t
如果格式不合适并且您确实需要“2/13/2017 1:19:42 PM”之类的内容,则可以将' CONVERT(datetime,any_datetime_format)'替换为'< strong> FORMAT(转换(datetime,any_datetime_format),'MM / dd / yyyy HH:mm:ss tt')'