[英]CASTING issue with SQL query in Databricks with Apache Spark
I am trying to use the CAST function on a field in SQL to cast a Time field as a string, but I'm getting unexpected errors我正在尝试在 SQL 中的字段上使用 CAST function 将时间字段转换为字符串,但出现意外错误
The code is as follows:代码如下:
select DATE_ADD(cast(LAST_MOD_TIME as STRING) % 100 + CAST(LAST_MOD_TIME AS STRING) / 100 % 100 * 60 + CAST(LAST_MOD_TIME AS STRING) / 10000 * 3600, CAST(LAST_MOD_DATE AS STRING)) from DmWo
But I'm getting the error:但我收到错误:
Error in SQL statement: AnalysisException: cannot resolve 'date_add((((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) % CAST(100 AS DOUBLE)) + (((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(100 AS DOUBLE)) % CAST(100 AS DOUBLE)) * CAST(60 AS DOUBLE))) + ((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(10000 AS DOUBLE)) * CAST(3600 AS DOUBLE))), CAST(dmwo.`LAST_MOD_DATE` AS STRING))' due to data type mismatch: argument 1 requires date type, however, '(((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) % CAST(100 AS DOUBLE)) + (((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(100 AS DOUBLE)) % CAST(100 AS DOUBLE)) * CAST(60 AS DOUBLE))) + ((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(10000 AS DOUBLE)) * CAST(3600 AS DOUBLE)))' is of double type. argument 2 requires (int or smallint or tinyint) type, however, 'CAST(dmwo.`LAST_MOD_DATE` AS STRING)' is of string type.; line 1 pos 7;
'Project [unresolvedalias(date_add((((cast(cast(LAST_MOD_TIME#88591 as string) as double) % cast(100 as double)) + (((cast(cast(LAST_MOD_TIME#88591 as string) as double) / cast(100 as double)) % cast(100 as double)) * cast(60 as double))) + ((cast(cast(LAST_MOD_TIME#88591 as string) as double) / cast(10000 as double)) * cast(3600 as double))), cast(LAST_MOD_DATE#88590 as string)), None)]
Any thoughts on where I might be going wrong with the CAST function?关于 CAST function 可能出错的地方有什么想法吗?
There are several mistakes here:这里有几个错误:
start_date
, and needs to be a date
type, but you're passing in the time of the day in seconds based on the logic, and the type is double
due to the division operator. start_date
,并且需要是date
类型,但是您根据逻辑以秒为单位传递一天中的时间,并且由于除法运算符,该类型为double
精度。num_days
after the starting date and needs to be int
type. num_days
,需要是int
类型。 You're passing in the starting date itself as a string
type.string
类型传递。num_days
after start_date
, but you are adding the date and the time of day counted in seconds. start_date
之后num_days
的日期,但您要添加以秒为单位的日期和时间。 That's not what this function does for you. A simpler way is to use the to_timestamp()
function:一种更简单的方法是使用
to_timestamp()
function:
select to_timestamp(concat(LAST_MOD_DATE, LAST_MOD_TIME), 'yyyyMMddHHmmss')
from DmWo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.