几天来,我在Firebird查询中遇到日期问题。我试图在select语句中的一个字段中连接3个日期。
构建它来测试它:
select
data_wyst, data_sp, data_pop,
data_wyst ||' | '|| data_sp ||' | '|| data_pop
from rv_sp_dok
结果是:
2015-12-14; 2015-12-14; 2015-12-31; 14-DEC-2015 | 14-DEC-2015 | 31-DEC-2015
2015-12-31; 2015-12-31; null; null
2015-12-31; 2015-12-31; 2015-12-31; 31-DEC-2015 | 31-DEC-2015 | 31-DEC-2015
因此,当一个日期为空时,整个concat为空,日期格式从' YYYY-MM-DD'到DD-MM-YYYY'。结果应该是:
2015-12-14; 2015-12-14; 2015-12-31; 2015-12-14 | 2015-12-14 | 2015-12-31
2015-12-31; 2015-12-31; null; 2015-12-31 | 2015-12-31 |
2015-12-31; 2015-12-31; 2015-12-31; 2015-12-31 | 2015-12-31 | 2015-12-31
有没有办法阻止Firebird在转换为字符串时更改日期格式(转换date2string的过程除外)?
//编辑: 似乎最好的方法是将日期转换为字符串的过程:
CREATE OR ALTER PROCEDURE DATE2STRING (DataIN Date)
returns (DataOut Varchar(10))
AS
declare variable y Varchar(4);
declare variable m Varchar(2);
declare variable d Varchar(2);
begin
IF (DataIn is null) then
BEGIN
DataOut = '';
END
ELSE
BEGIN
y = Substring(Extract(Year FROM DataIn) FROM 1 FOR 4);
m = Substring(Extract(Month FROM DataIn)+100 FROM 2 FOR 2);
d = Substring(Extract(Day FROM DataIn)+100 FROM 2 FOR 2);
DataOut = y || '-' || m || '-' || d;
END
SUSPEND;
end
答案 0 :(得分:4)
连接可能NULL
使用coalesce功能的字段,即
select
data_wyst, data_sp, data_pop,
COALESCE(data_wyst, '') ||' | '|| COALESCE(data_sp, '') ||' | '|| COALESCE(data_pop, '')
from rv_sp_dok
要在不使用任何外部函数(date2string
,AFAIK)的情况下控制日期格式,您可以使用extract函数,即
EXTRACT(YEAR FROM data_pop) ||'-'|| EXTRACT(MONTH FROM data_pop)||'-'|| EXTRACT(DAY FROM data_pop)