SQL Vertica,Cast Long问题

时间:2018-03-08 16:26:36

标签: sql vertica

我有以下SQL语句

 cast('long', ((substr(D.business_day, 1, 4)||substr(D.business_day, 6, 2))||substr(D.business_day, 9, 2))) AS bdate_id_yyyymmdd,

我尝试将'long'更改为整数以查看是否可行。

cast(integer, ((substr(D.business_day, 1, 4)||substr(D.business_day, 6, 2))||substr(D.business_day, 9, 2))) AS bdate_id_yyyymmdd,

还尝试integer(8)

收到错误:

Error: [Vertica][VJDBC](4856) ERROR: Syntax error at or near ","
SQLState:  42601
ErrorCode: 4856

1 个答案:

答案 0 :(得分:0)

这不仅是语法错误。 Vertica在数据类型方面也相对苛刻。

所以你想从看起来像ISO日期的东西中创建一个由yyyymmdd组成的整数。

字符串?

约会?

有两种方法可以解决这个问题。你不能在一个日期SUBSTR();你无法通过ISO格式的字符串调用函数来获取日,月或年数。

此示例说明了您可以执行的操作。请记住,从日期中提取整数比使用字符串和子字符串更快。如果您的business_day是DATE类型,请执行以下对business_date的处理。

哦,Vertica也支持这种语法:

<expression>::INTEGER

转换为整数。

开心玩......

WITH
d(business_date,business_day) AS (
          SELECT DATE '2018-03-04',  '2018-03-04'
UNION ALL SELECT DATE '1957-04-22',  '1957-04-22'
UNION ALL SELECT DATE '1959-09-27',  '1959-09-27'
)
SELECT
  CAST(
         substr(d.business_day, 1, 4)
       ||substr(d.business_day, 6, 2)
       ||substr(d.business_day, 9, 2)
    AS int
  ) AS with_substr
, (  YEAR (business_date)*10000
   + MONTH(business_date)*100
   + DAY  (business_date)
  ) AS with_calc
FROM d;

with_substr|with_calc
 20,180,304|20,180,304
 19,570,422|19,570,422
 19,590,927|19,590,927