开发者

Why does RPAD() on a Date column return only the date component?

开发者 https://www.devze.com 2023-02-12 02:57 出处:网络
I have a column in a table with a data type as DATE. When I fetch the column via a query (I\'ve used SYSDATE as an example, but the behavior is the same), I get the date/time - which I understand.

I have a column in a table with a data type as DATE. When I fetch the column via a query (I've used SYSDATE as an example, but the behavior is the same), I get the date/time - which I understand.

SELECT SYSDATE
FROM DUAL

SYSDATE
--------------------
21-Feb-11 12:24:39 PM

Now, using rpad() returns only the date part

SELECT SYSDATE, RPAD(SYSDATE, '9') 
FROM DUAL

        SYSDATE        | RPAD(SYSDATE, '9') 
 ----------------------|-------------------
 21-Feb-11 12:27:14 PM | 21-FEB-11       

Oracle documentation states:

开发者_Go百科

RPAD returns expr1, right-padded to length n characters with expr2, replicated as many times as necessary. If expr1 is longer than n, then this function returns the portion of expr1 that fits in n.

Now sysdate returns characters > 9, so why doesn't, say rpad(16) return the date and the time ?

SELECT SYSDATE, RPAD(SYSDATE, '16') 
FROM DUAL

        SYSDATE        | RPAD(SYSDATE, '16') 
 ----------------------|-------------------
 21-Feb-11 12:27:14 PM | 21-FEB-11     


RPAD is a string function, so when you apply it to a DATE value Oracle first has to implicitly convert the date to a string, which it does using the session's default format mask, which usually does not include the time component. Try this instead:

SELECT SYSDATE, RPAD (TO_CHAR(SYSDATE,'DD-Mon-YY HH:MI:SS'), 16)
FROM DUAL;

Having said that, you are getting the time when you just select SYSDATE. If I try to replicate your case I see this:

SQL> alter session set nls_date_format = 'DD-MON-RR HH24:MI:SS';
SQL> select sysdate, rpad(sysdate,16) from dual;

SYSDATE            RPAD(SYSDATE,16)
------------------ ----------------
21-FEB-11 11:20:20 21-FEB-11 11:20:

i.e. pretty much what you were hoping to see. Which makes me wonder: how are you setting the format so that SELECT SYSDATE FROM DUAL shows the time?

0

精彩评论

暂无评论...
验证码 换一张
取 消