SQL:如何找到类型为 varchar2 的最大时间
SQL : how to find max time where its type is varchar2
我有一个问题,我必须从周末最晚开张的商店 table 中查找商店的详细信息。在商店 table 中,我有一个名为 weekendHours 的属性,类型为 varchar2,示例是:(10:00 am - 5:00pm)。
我试过
To_char( S.WeekendHours=max(S.WeekendHours );
但它不起作用!
我该怎么做?
如果你想要最新开的店,那么你可以这样做:
select t.*
from t
order by t.weekendhours desc
fetch first 1 row only;
但是,对于您的时间格式,这将不起作用,因为 2:00 a.m。将在 1:00 p.m.
之后
因此,您需要将范围转换为时间。这是一种方法:
select t.*
from t
order by cast(substring(t.weekendhours, 1, 8) as time) desc
fetch first 1 row only;
当然,这些查询使用跨许多数据库的通用方法。该逻辑可能适用于您的数据库,但语法可能不同。
注意:所有这些都是因为 weekendhours
列已损坏。它应该在两列中,weekend_opentime
和 weekend_closetime
。那么查询将是微不足道的。
您可以使用正则表达式提取打开和关闭时间,然后根据需要对其进行操作:
SELECT s.*,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 1)) AS OPENING_HOUR_STRING,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 2)) AS CLOSING_HOUR_STRING
FROM STORE s
一旦你有了上面的基本信息,你就可以操作它来获取作为日期的开盘和收盘时间,然后在适当的字段上使用 MAX:
WITH cteSTORE_STRING_VALUES AS
(SELECT s.*,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 1)) AS OPENING_HOUR_STRING,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 2)) AS CLOSING_HOUR_STRING
FROM STORE s),
cteALL_STORE_VALUES AS
(SELECT s.*,
CASE
WHEN LENGTH(OPENING_HOUR_STRING) < 4 THEN LPAD(OPENING_HOUR_STRING, 4, '0')
ELSE OPENING_HOUR_STRING
END AS PADDED_OPENING_STRING,
CASE
WHEN LENGTH(CLOSING_HOUR_STRING) < 4 THEN LPAD(CLOSING_HOUR_STRING, 4, '0')
ELSE CLOSING_HOUR_STRING
END AS PADDED_CLOSING_STRING
FROM cteSTORE_STRING_VALUES s),
cteSTORES_WITH_HOURS AS
(SELECT a.*,
CASE
WHEN UPPER(SUBSTR(a.OPENING_HOUR_STRING, -3)) IN (' AM', ' PM') THEN
TO_DATE(a.OPENING_HOUR_STRING, 'HH:MI AM')
WHEN UPPER(SUBSTR(a.OPENING_HOUR_STRING, -2)) IN ('AM', 'PM') THEN
TO_DATE(a.OPENING_HOUR_STRING, 'HH:MIAM')
WHEN INSTR(a.OPENING_HOUR_STRING, ':') > 0 THEN
TO_DATE(a.OPENING_HOUR_STRING, 'HH24:MI')
ELSE
TO_DATE(a.PADDED_OPENING_STRING, 'HH24MI')
END AS OPENING_HOURS,
CASE
WHEN UPPER(SUBSTR(a.CLOSING_HOUR_STRING, -3)) IN (' AM', ' PM') THEN
TO_DATE(a.CLOSING_HOUR_STRING, 'HH:MI AM')
WHEN UPPER(SUBSTR(a.CLOSING_HOUR_STRING, -2)) IN ('AM', 'PM') THEN
TO_DATE(a.CLOSING_HOUR_STRING, 'HH:MIAM')
WHEN INSTR(a.CLOSING_HOUR_STRING, ':') > 0 THEN
TO_DATE(a.CLOSING_HOUR_STRING, 'HH24:MI')
ELSE
TO_DATE(a.PADDED_CLOSING_STRING, 'HH24MI')
END AS CLOSING_HOURS
FROM cteALL_STORE_VALUES a)
SELECT *
FROM cteSTORES_WITH_HOURS
WHERE OPENING_HOURS = (SELECT MAX(OPENING_HOURS)
FROM cteSTORES_WITH_HOURS);
祝你好运。
我有一个问题,我必须从周末最晚开张的商店 table 中查找商店的详细信息。在商店 table 中,我有一个名为 weekendHours 的属性,类型为 varchar2,示例是:(10:00 am - 5:00pm)。 我试过 To_char( S.WeekendHours=max(S.WeekendHours ); 但它不起作用! 我该怎么做?
如果你想要最新开的店,那么你可以这样做:
select t.*
from t
order by t.weekendhours desc
fetch first 1 row only;
但是,对于您的时间格式,这将不起作用,因为 2:00 a.m。将在 1:00 p.m.
之后因此,您需要将范围转换为时间。这是一种方法:
select t.*
from t
order by cast(substring(t.weekendhours, 1, 8) as time) desc
fetch first 1 row only;
当然,这些查询使用跨许多数据库的通用方法。该逻辑可能适用于您的数据库,但语法可能不同。
注意:所有这些都是因为 weekendhours
列已损坏。它应该在两列中,weekend_opentime
和 weekend_closetime
。那么查询将是微不足道的。
您可以使用正则表达式提取打开和关闭时间,然后根据需要对其进行操作:
SELECT s.*,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 1)) AS OPENING_HOUR_STRING,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 2)) AS CLOSING_HOUR_STRING
FROM STORE s
一旦你有了上面的基本信息,你就可以操作它来获取作为日期的开盘和收盘时间,然后在适当的字段上使用 MAX:
WITH cteSTORE_STRING_VALUES AS
(SELECT s.*,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 1)) AS OPENING_HOUR_STRING,
TRIM(REGEXP_SUBSTR(s.weekend_hours, '[0-9]+:*[0-9]* *(am|pm)*', 1, 2)) AS CLOSING_HOUR_STRING
FROM STORE s),
cteALL_STORE_VALUES AS
(SELECT s.*,
CASE
WHEN LENGTH(OPENING_HOUR_STRING) < 4 THEN LPAD(OPENING_HOUR_STRING, 4, '0')
ELSE OPENING_HOUR_STRING
END AS PADDED_OPENING_STRING,
CASE
WHEN LENGTH(CLOSING_HOUR_STRING) < 4 THEN LPAD(CLOSING_HOUR_STRING, 4, '0')
ELSE CLOSING_HOUR_STRING
END AS PADDED_CLOSING_STRING
FROM cteSTORE_STRING_VALUES s),
cteSTORES_WITH_HOURS AS
(SELECT a.*,
CASE
WHEN UPPER(SUBSTR(a.OPENING_HOUR_STRING, -3)) IN (' AM', ' PM') THEN
TO_DATE(a.OPENING_HOUR_STRING, 'HH:MI AM')
WHEN UPPER(SUBSTR(a.OPENING_HOUR_STRING, -2)) IN ('AM', 'PM') THEN
TO_DATE(a.OPENING_HOUR_STRING, 'HH:MIAM')
WHEN INSTR(a.OPENING_HOUR_STRING, ':') > 0 THEN
TO_DATE(a.OPENING_HOUR_STRING, 'HH24:MI')
ELSE
TO_DATE(a.PADDED_OPENING_STRING, 'HH24MI')
END AS OPENING_HOURS,
CASE
WHEN UPPER(SUBSTR(a.CLOSING_HOUR_STRING, -3)) IN (' AM', ' PM') THEN
TO_DATE(a.CLOSING_HOUR_STRING, 'HH:MI AM')
WHEN UPPER(SUBSTR(a.CLOSING_HOUR_STRING, -2)) IN ('AM', 'PM') THEN
TO_DATE(a.CLOSING_HOUR_STRING, 'HH:MIAM')
WHEN INSTR(a.CLOSING_HOUR_STRING, ':') > 0 THEN
TO_DATE(a.CLOSING_HOUR_STRING, 'HH24:MI')
ELSE
TO_DATE(a.PADDED_CLOSING_STRING, 'HH24MI')
END AS CLOSING_HOURS
FROM cteALL_STORE_VALUES a)
SELECT *
FROM cteSTORES_WITH_HOURS
WHERE OPENING_HOURS = (SELECT MAX(OPENING_HOURS)
FROM cteSTORES_WITH_HOURS);
祝你好运。