无法在雪花程序中转换日期变量
Unable to convert date variable in Snowflake Procedure
我在将日期记录传递到变量时遇到错误。
想要将记录转换为日期格式 'YYYY-MM-DD' 而不是 'Feb 20 2022 HH:MM:SS' 格式。
javascript有日期转换方法吗?
CREATE OR REPLACE PROCEDURE sp_ADDWORKDAYS_CUTOFF_PP(SRC_DB STRING, SRC_SCHEMA STRING)
RETURNS varchar
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var D_STARTDATE = '';
var D_WORKDAYS = '';
var seq_query = `SELECT "DELIV_CR_DT" AS D_STARTDATE,DERIVED_COL AS D_WORKDAYS FROM `+SRC_DB+`.`+SRC_SCHEMA+`.v_v_testing`;
var seq_stmt = snowflake.createStatement({ sqlText: seq_query});
var seq_result = seq_stmt.execute();
while (seq_result.next()){
var STARTDATE = seq_result.getColumnValue(1);
var WORKDAYS = seq_result.getColumnValue(2);
var sub_call_query = `CALL `+SRC_DB+`.`+SRC_SCHEMA+`.sp_OTD_ADDWORKDAYS(`+STARTDATE+`,`+WORKDAYS+`,'01')`;
var sub_call_stmt = snowflake.createStatement({ sqlText: sub_call_query ,binds: [STARTDATE.toISOString(),WORKDAYS] });
var sub_call_result = sub_call_stmt.execute();
sub_call_stmt.next();
var return_value = sub_call_result.getColumnValue(1);
}
$$
;
错误:
SQL编译错误:
位置 52 处的语法错误行 1 意外 'Feb'。
在Statement.execute,第14行位置40(第36行)
您可以尝试在您的日期转换逻辑中包含这样的内容 -
使用Java-script程序-
CREATE OR REPLACE PROCEDURE "GETJOBS"(SRC_DB STRING, SRC_SCHEMA STRING)
RETURNS varchar
LANGUAGE JAVASCRIPT
EXECUTE AS caller
AS
$$
var D_STARTDATE = '';
var D_WORKDAYS = '';
var seq_query = `SELECT to_char(to_timestamp('Feb 20 2022 10:10:10','Mon DD YYYY HH24:MI:SS'), 'YYYY-MM-DD') AS D_STARTDATE`;
var seq_stmt = snowflake.createStatement({ sqlText: seq_query});
var seq_result = seq_stmt.execute();
seq_result.next();
return seq_result.getColumnValue(1);
$$
;
程序调用-
call getjobs('a','b');
GETJOBS
2022-02-20
使用 UDF -
create or replace function fn_getjobs(dt string)
returns string as
$$
select to_char(to_timestamp(dt,'Mon DD YYYY HH24:MI:SS'), 'YYYY-MM-DD') as return_dt
$$
;
函数调用
select fn_getjobs('Feb 20 2022 10:10:10') as dt;
DT
2022-02-20
我在将日期记录传递到变量时遇到错误。
想要将记录转换为日期格式 'YYYY-MM-DD' 而不是 'Feb 20 2022 HH:MM:SS' 格式。
javascript有日期转换方法吗?
CREATE OR REPLACE PROCEDURE sp_ADDWORKDAYS_CUTOFF_PP(SRC_DB STRING, SRC_SCHEMA STRING)
RETURNS varchar
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var D_STARTDATE = '';
var D_WORKDAYS = '';
var seq_query = `SELECT "DELIV_CR_DT" AS D_STARTDATE,DERIVED_COL AS D_WORKDAYS FROM `+SRC_DB+`.`+SRC_SCHEMA+`.v_v_testing`;
var seq_stmt = snowflake.createStatement({ sqlText: seq_query});
var seq_result = seq_stmt.execute();
while (seq_result.next()){
var STARTDATE = seq_result.getColumnValue(1);
var WORKDAYS = seq_result.getColumnValue(2);
var sub_call_query = `CALL `+SRC_DB+`.`+SRC_SCHEMA+`.sp_OTD_ADDWORKDAYS(`+STARTDATE+`,`+WORKDAYS+`,'01')`;
var sub_call_stmt = snowflake.createStatement({ sqlText: sub_call_query ,binds: [STARTDATE.toISOString(),WORKDAYS] });
var sub_call_result = sub_call_stmt.execute();
sub_call_stmt.next();
var return_value = sub_call_result.getColumnValue(1);
}
$$
;
错误: SQL编译错误: 位置 52 处的语法错误行 1 意外 'Feb'。 在Statement.execute,第14行位置40(第36行)
您可以尝试在您的日期转换逻辑中包含这样的内容 -
使用Java-script程序-
CREATE OR REPLACE PROCEDURE "GETJOBS"(SRC_DB STRING, SRC_SCHEMA STRING)
RETURNS varchar
LANGUAGE JAVASCRIPT
EXECUTE AS caller
AS
$$
var D_STARTDATE = '';
var D_WORKDAYS = '';
var seq_query = `SELECT to_char(to_timestamp('Feb 20 2022 10:10:10','Mon DD YYYY HH24:MI:SS'), 'YYYY-MM-DD') AS D_STARTDATE`;
var seq_stmt = snowflake.createStatement({ sqlText: seq_query});
var seq_result = seq_stmt.execute();
seq_result.next();
return seq_result.getColumnValue(1);
$$
;
程序调用-
call getjobs('a','b');
GETJOBS |
---|
2022-02-20 |
使用 UDF -
create or replace function fn_getjobs(dt string)
returns string as
$$
select to_char(to_timestamp(dt,'Mon DD YYYY HH24:MI:SS'), 'YYYY-MM-DD') as return_dt
$$
;
函数调用
select fn_getjobs('Feb 20 2022 10:10:10') as dt;
DT |
---|
2022-02-20 |