我想我的正常化日期時間對於一個給定的數據庫。我需要能夠使用日期做統計。以下是我迄今所做的:如何統一的日期時間格式時的Varchar
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 /二千零一十七分之十三11時58分零零秒'
是否有這樣做的任何方式?
這是我如何做才能日期:
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'
這是從日誌系統。由於這些信息是自信的,我無法提供很多信息。但請注意,有很多人在評論末尾添加時間戳,其中包括他們的姓名和時間戳。隨着查詢在那裏,我能夠獲得大部分日期。有時候沒有辦法獲得日期(日誌中根本沒有timetamp)。這裏是我可以給的例子:
Test Data
---------------------------------------------
FName LName 2/13/2017 1:19:42 PM
有一個非常簡單的方法:將DATETIME值存儲爲' DATETIME'數據類型而不是'VARCHAR'。你不應該在數據庫中介紹一個'DATETIME'。 – Siyual
如果有這麼多的模式,有字面上沒有辦法確切知道正確的日期當使用ambiguos風格:'02-03-2017'是2月3日或3月2日? – Lamak
這是2月13日2017年。但事實是,我不將其存儲爲日期時間,因爲它是一些文本內...我收到手動在大量的文字 – InfiniteLoop