避免使用格式时间无效的用户SQL

时间:2019-04-18 11:55:23

标签: sql amazon-athena presto

这是我的简单查询

SELECT
playerId,
max(accountlevel) as level,
max(appActiveTime) as overall_time,
max(matchcount) as matchcount
FROM client
WHERE partition_0 = '2019' 
AND partition_1 = '03'
AND install_date BETWEEN date('2019-03-01')  AND date('2019-03-20')
AND date BETWEEN date('2019-03-01')  AND date('2019-03-31')
AND accountagedays = 0
GROUP BY playerId

雅典娜还给我

INVALID_FUNCTION_ARGUMENT: Invalid format: "2019-0+-28T23:43:57.4142360Z" is malformed at "+-28T23:43:57.4142360Z"

我想知道如何避免出现此消息?

1 个答案:

答案 0 :(得分:0)

这是针对SQL Server的:我建议使用ISDATE函数来验证输入然后进行转换,如果它是正确的日期格式,则应进行转换并检查给定日期之间的时间。您可以使用字符串来验证日期。下面的代码只是为了让您了解如何进行检查和相应地投射。如果为临时表提供数据和列类型,则可以实现实际结果。

declare  @install_date datetime= '2019-03-01'
select CAST(IIF(ISDATE(@install_date) = 1,@install_date,'1900-01-01') as Date)
go
declare  @install_date varchar(10)= '2019-23-01'
select CAST(IIF(ISDATE(@install_date) = 1,@install_date,'19000101') as Date)
go
declare  @install_date varchar(100)= 'string not a date'
select CAST(IIF(ISDATE(@install_date) = 1,@install_date,'19000101') as Date)