Between Clause For dates not working in Impala

Between Clause For dates not working in Impala

我的 table EMPLOYEE 包含具有字符串数据类型的加载日期列。

在此列中,日期的存储方式类似于

'2019-12-8',
'2019-12-9',
'2019-12-10',
'2019-12-11',
'2019-12-12',
'2019-12-13',
'2019-12-14',
'2019-12-15',

当我运行低于查询

SELECT * FROM employee where loaddate between '2019-12-8' and '2019-12-14';

我得到 0 条记录。 但是当我在 运行 下面查询获取记录时,但仅从 2019-12-10 到 2019-12-14' 虽然 table 包含 8 和 9

的记录
SELECT * FROM employee where loaddate between '2019-12-08' and '2019-12-14';

以上两个查询的区别是2019-12-8和2019-12-08。 所以不明白为什么第一个查询没有给出任何记录而第二个查询给出了记录但不是全部。

答案在于对以下部分的理解。

1.在运算符功能之间。

参考:impala between operator

BETWEEN Operator : expression BETWEEN lower_bound AND upper_bound

a. In a WHERE clause, compares an expression to both a lower_bound and upper_bound. 
b. The comparison is successful if the expression is greater than or equal to the lower_bound, and less than or equal to the upper_bound. 
c. If the bound values are switched, so the lower_bound is greater than the upper_bound, does not match any values.

2。使用的数据类型是字符串,但根据文档,运算符之间通常与数字数据类型一起使用。

3。 loaddate

的顺序

很明显,值“2019-12-8”按升序大于“2019-12-15”。再插入两个值“2019-12-08”和“2019-12-09”,当查询的相同顺序为 运行 时,它们将位于顶部,因为它们将小于“2019-12-10”。

SELECT loaddate FROM employee order by loaddate;
'2019-12-10'
'2019-12-11'
'2019-12-12'
'2019-12-13'
'2019-12-14'
'2019-12-15'
'2019-12-8'
'2019-12-9'

SELECT * FROM employee where loaddate between '2019-12-8' and '2019-12-14'; ---zero results because lower_bound('2019-12-8') is the larger value than upper_bound('2019-12-14'). (case c)

SELECT * FROM employee where loaddate between '2019-12-08' and '2019-12-14'; ---getting records from 2019-12-10 to 2019-12-14 because 08 and 09 don't exist.

SELECT * FROM employee where loaddate between '2019-12-08' and '2019-12-9'; ---this will fetch all the data in the sample considering 10 is greater than 08(assuming 08 and 09 are not inserted yet).