如何在PLSQL中格式化日期变量 [英] How to format a Date variable in PLSQL

查看:805
本文介绍了如何在PLSQL中格式化日期变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PL/SQL的新手,并且有这个问题.

I am new to PL/SQL and have this question.

我创建了一个具有以下规范的过程:

I created a procedure with the following specification:

PROCEDURE runschedule (i_RunDate  IN DATE)

i_RunDate具有特定格式,我需要将其更改为以下格式:

this i_RunDate comes in a specific format, and I need to change it to this format:

'MM/dd/yyyy hh:mi:ss PM'

我找不到如何重新格式化Date变量的方法.

I couldn't find how to re-format a Date variable.

推荐答案

DATE没有特定的格式,DATE是DATE.当存储在数据库列中时,日期值包括自午夜以来以秒为单位的一天中的时间,并且DATE变量具有相同的值,仅当您在某个时刻显示该变量已格式化时才如此. PL/SQL可以隐式转换值的数据类型,例如,可以将CHAR值'02 -JUN-92'转换为DATE值..但我不建议您依赖此隐式会话.我总是使用显式转换. 有关此主题的更多信息,您可以阅读以下内容: http://download.oracle.com/docs /cd/B28359_01/appdev.111/b28370/datatypes.htm#i9118

The DATE has not especific format, a DATE is a DATE. When stored in the database column, date values include the time of day in seconds since midnight, and a DATE variable has the same, only that when in some point you show the variable is formatted. PL/SQL can convert the datatype of a value implicitly, for example, can convert the CHAR value '02-JUN-92' to a DATE value.. but I don't recommend you rely on this implicit conversiosn. I always use the explicit conversion. For more on this topic, you could read this: http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/datatypes.htm#i9118

在您的情况下,如果要格式化DATE以记录某些内容,或者以其他格式显示在UI或其他应用程序中,则需要将其关联到Justin所说的varchar变量中,如下所示:

In your case, if you want to format your DATE to log something, or to show in your UI or wathever in a different format, you need to assig it to a varchar variable as Justin said, something like this:

....
v_generated_run_date DATE; 
v_var_date VARCHAR2(30);
BEGIN -- generate sysdate if required 
IF (i_RunDate is null) 
THEN 
v_var_date:=TO_CHAR(sysdate, 'MM/DD/YYYY HH12:MI:SS AM'); 
ELSE 
v_var_date:=TO_CHAR(i_RunDate,'MM/DD/YYYY HH12:MI:SS AM'); 
END IF; 
pkgschedule.createschedule (v_var_date); 
commit; 
END runschedule; 
END 

在这种情况下,您的createchedule过程将具有varchar2参数...

Your createschedule procedure, in this case, will have a varchar2 parameter...

这篇关于如何在PLSQL中格式化日期变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆