在 Oracle 中转换为具有多个可能掩码的日期
碰巧我必须从oracle中的varchar2列中获取日期,但格式不一致。某些字段可能包含“2009.12.31”,其他字段可能包含“2009/12/32 00:00:00”。是否有任何我可以使用的标准构造,这样我就不必通过
begin
to_date(date, mask1)
exception
begin
to_date(date,mask2)
exception
..
end
end
块或之前的日期字符串分析来确定正确的掩码?像 to_date(date, mask1,mask2,..)
之类的东西?
It so happens that I have to get a date from a varchar2 column in oracle, but it is inconsistent in formatting. Some fields might have '2009.12.31', others '2009/12/32 00:00:00'. Is there any standard construct I could use, so that I don't have to go through
begin
to_date(date, mask1)
exception
begin
to_date(date,mask2)
exception
..
end
end
blocks or prior date string analysis to determine the correct mask? Something like to_date(date, mask1,mask2,..)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,但某些 Oracle 日期格式可以“容忍”差异,例如,
这样可能会减少您需要处理的情况数量。我建议您按照您的想法编写一个函数,以便可以从代码中需要处理此类日期的所有位置调用它。
No, but some Oracle date formats are "forgiving" of differences e.g.
So that may reduce the amount of cases you need to handle. I suggest you write a function along the lines you were thinking of, so that it can be called from all the places in your code where you need to handle dates like this.
您需要枚举您想要支持的所有可能的格式 - 请记住,有些格式是不明确的(例如,“10-11-2009”是 11 月 10 日还是 10 月 11 日?),因此您的代码必须优先选择其中一种格式。
正如托尼所说,某些格式将接受各种输入,主要是关于分隔符字符和丢失位的解析(例如“DD-MON-YYYY HH24:MI:SS”将匹配“31-DEC-2009 10:30”) :00', '31/deC/2009 10:30', '2009 年 12 月 31 日')。
除了这些简化之外,您还需要一系列 BEGIN (format1) EXCEPTION WHEN OTHERS THEN BEGIN (format2) EXCEPTION WHEN OTHERS THEN BEGIN ....
You need to enumerate all the possible formats you want to support - remember that some are ambiguous (e.g. is "10-11-2009" 10 Nov or Oct 11?) so your code will have to favour one over the other.
As Tony has said, some formats will accept a variety of inputs, mainly in regards to parsing of separator characters and missing bits (e.g. 'DD-MON-YYYY HH24:MI:SS' will match '31-DEC-2009 10:30:00', '31/deC/2009 10:30', '31 dec 2009').
Apart from these simplifications, you're going to need a series of BEGIN (format1) EXCEPTION WHEN OTHERS THEN BEGIN (format2) EXCEPTION WHEN OTHERS THEN BEGIN ....