在数据库中,我有专栏。 - 我的查询是 -
SELECT Emp_Name, Schedule
FROM emp_schedule
重新生成此结果集:
Emp_Name Schedule
M.S.Cave 10:00 AM - 7:00 PM Semi Flexi
M.S.Cave 10:00 AM - 7:00 PM Semi Flexi
K.V.Boreta 9:00 AM - 6:00 PM Fixed
K.V.Boreta 9:00 AM - 6:00 PM Fixed
R.M.Mendoza Branch 8:30 AM to 5:30 PM Sunday RD
R.M.Mendoza Branch 8:30 AM to 5:30 PM Sunday RD
但是我想只显示“时间表”列,只考虑时间,忽略文本。
Emp_Name Schedule
M.S.Cave 10:00 AM - 7:00 PM
M.S.Cave 10:00 AM - 7:00 PM
K.V.Boreta 9:00 AM - 6:00 PM
K.V.Boreta 9:00 AM - 6:00 PM
R.M.Mendoza 8:30 AM to 5:30 PM
R.M.Mendoza 8:30 AM to 5:30 PM
没有TRIM,添加列或编辑我的表等可以做到这一点吗?
答案 0 :(得分:2)
试试这个
我使用REVERSE
,PATINDEX
,SUBSTRING
来实现它。忽略CTE,因为我只想使用临时表。
;WITH CTE (Emp_Name, Schedule)
AS (
SELECT 'M.S.Cave', '10:00 AM - 7:00 PM Semi Flexi'
UNION ALL
SELECT 'M.S.Cave', '10:00 AM - 7:00 PM Semi Flexi'
UNION ALL
SELECT 'K.V.Boreta', '9:00 AM - 6:00 PM Fixed'
UNION ALL
SELECT 'K.V.Boreta', '9:00 AM - 6:00 PM Fixed'
UNION ALL
SELECT 'R.M.Mendoza', 'Branch 8:30 AM to 5:30 PM Sunday RD'
UNION ALL
SELECT 'R.M.Mendoza', 'Branch 8:30 AM to 5:30 PM Sunday RD'
)
, CTE2 AS (
SELECT *
, SUBSTRING(Schedule, PATINDEX('%[0-9]%', Schedule), LEN(Schedule)) SS
FROM CTE
)
SELECT *
,REVERSE(SUBSTRING(REVERSE(SS), PATINDEX('%[0-9]%', REVERSE(SS)) - 3, LEN(SS)))
FROM CTE2
结果就像
+-------------+-------------------------------------+-------------------------------+--------------------+
| Emp_Name | Schedule | SS | (No column name) |
+-------------+-------------------------------------+-------------------------------+--------------------+
| M.S.Cave | 10:00 AM - 7:00 PM Semi Flexi | 10:00 AM - 7:00 PM Semi Flexi | 10:00 AM - 7:00 PM |
| M.S.Cave | 10:00 AM - 7:00 PM Semi Flexi | 10:00 AM - 7:00 PM Semi Flexi | 10:00 AM - 7:00 PM |
| K.V.Boreta | 9:00 AM - 6:00 PM Fixed | 9:00 AM - 6:00 PM Fixed | 9:00 AM - 6:00 PM |
| K.V.Boreta | 9:00 AM - 6:00 PM Fixed | 9:00 AM - 6:00 PM Fixed | 9:00 AM - 6:00 PM |
| R.M.Mendoza | Branch 8:30 AM to 5:30 PM Sunday RD | 8:30 AM to 5:30 PM Sunday RD | 8:30 AM to 5:30 PM |
| R.M.Mendoza | Branch 8:30 AM to 5:30 PM Sunday RD | 8:30 AM to 5:30 PM Sunday RD | 8:30 AM to 5:30 PM |
+-------------+-------------------------------------+-------------------------------+--------------------+
答案 1 :(得分:1)
我尝试如下:
select Emp_name,
reverse(SUBSTRING(reverse(Schedule),charindex(' MP ',REVERSE(Schedule)),len(Schedule)))
,substring(Schedule,charindex(' AM ',Schedule)-5,charindex(' PM ',Schedule)-charindex(' AM ',Schedule)+8)
from emp_schedule