我有这个问题:
select SentDate, SentTime, SeenDate, SeenTime
from Sents
结果是这样的:
/////////////////////////////////////////////////////////
SentDate SentTime SeenDate SeenTime
/////////////////////////////////////////////////////////
1394/01/09 10:44 1394/01/22 13:27
1394/01/15 7:15 1394/01/22 14:41
1394/01/15 7:21 1394/01/22 13:18
1394/01/15 7:31 1394/01/22 14:47
1394/01/15 8:28 1394/01/22 13:00
1394/01/15 11:20 1394/01/22 13:41
1394/01/16 9:9 1394/01/22 12:56
1394/01/17 07:48 1394/01/22 12:58
1394/01/17 07:49 1394/01/22 13:28
1394/01/18 12:53 1394/01/22 14:43
1394/01/19 10:33 1394/01/22 14:42
1394/01/19 11:49 1394/01/22 10:59
1394/01/20 08:00 1394/01/22 12:54
1394/01/20 9:43 1394/01/22 10:52
1394/01/20 9:51 1394/01/22 10:51
1394/01/22 6:47 1394/01/22 10:50
1394/01/22 6:50 1394/01/22 10:49
1394/01/22 8:31 1394/01/22 10:39
1394/01/22 9:12 1394/01/22 10:38
1394/01/22 9:12 PM 1394/01/22 10:38 PM
1394/01/23 09:10 PM 1394/01/22 10:9 AM
正如您所看到的,Dates(SentDate,SeenDate)在格式方面没问题,我的意思是他们的模式是^(\d{4})[-/](\d\d)[-/](\d\d(?:\d\d)?)
。问题是SentTime和SeenTime,我想用这种方式合成它们:(\d{2}):(\d{2})
。
首先,我尝试以这种方式获得前5个字符:
select SentDate,LTRim(RTRIM(SUBSTRING(SentTime,1,5))), SeenDate, LTRim(RTRIM(SUBSTRING(SeenTime,1,5)))
from erja
但我不知道格式化7:15
或07:5
或7:7
等值
有什么想法吗?
PS :在结果中我也有这些类型的值:
11:45 ق
11:45 ب
答案 0 :(得分:4)
将其转换为datetime,然后转换回char
SELECT
Value, REPLACE(REPLACE(Value, N'ق', 'AM'), N'ب', 'PM') AS ReplacedValue,
CONVERT(varchar(5),
CONVERT(datetime,
REPLACE(REPLACE(Value, N'ق', 'AM'), N'ب', 'PM')
)
, 108) AS FormatTime
FROM
(values
('10:9 AM'),
('09:10 PM '),
('9:12 PM'),
('9:12'),
('07:5'),
('7:7'),
(N'11:45 ق'),
(N'11:45 ب')
) data(Value)
<强>结果强>
Value ReplacedValue FormatTime
--------- ------------- ----------
10:9 AM 10:9 AM 10:09
09:10 PM 09:10 PM 21:10
9:12 PM 9:12 PM 21:12
9:12 9:12 09:12
07:5 07:5 07:05
7:7 7:7 07:07
11:45 ق 11:45 AM 11:45
11:45 ب 11:45 PM 23:45