Oracle trunc()函数与round()函数对于数字的处理
一.TRUNC函数
1.TRUNC(for dates)
TRUNC函数为指定元素而截去的日期值。
其具体的语法格式如下:
TRUNC(date[,fmt])
其中:
date 一个日期值
fmt 日期格式,,该日期将由指定的元素格式所截去。忽略它则由最近的日期截去
下面是该函数的使用情况:
TRUNC(TO_DATE(’24-Nov-1999 08:00 pm’,’dd-mon-yyyy hh:mi am’))
=’24-Nov-1999 12:00:00 am’
TRUNC(TO_DATE(’24-Nov-1999 08:37 pm’,’dd-mon-yyyy hh:mi am’,’hh’)) =’24-Nov-1999 08:00:00 am’
round (date,''format'')未指定format时,如果日期中的时间在中午之前,则将日期中的时间截断为12 A.M.(午夜,一天的开始),否则进到第二天。
TRUNC(date,''format'')未指定format时,将日期截为12 A.M.,不考虑是否在中午之前的条件。
2.TRUNC(for number)
TRUNC函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定小数前或后的部分做相应舍入选择处理,而统统截去。
其具体的语法格式如下
TRUNC(number[,decimals])
其中:
number 待做截取处理的数值
decimals 指明需保留小数点后面的位数。可选项,忽略它则截去所有的小数部分
下面是该函数的使用情况:
TRUNC(89.985,2)=89.98
TRUNC(89.985)=89
TRUNC(89.985,-1)=80
注意:第二个参数可以为负数,表示为小数点左边指定位数后面的部分截去,即均以0记。
{
format为day时,只精确到天,而不管几年几月只要是符合的day就可以了,要想确定一年中的某月的某一天就要用trunc(date,''dd'').
通俗的说吧,format为年时,精确到-----年
为月时,精确到------年,月(不管哪年,只要是相同的月和哪天)
为日时,精确到------年,月,日(不管哪年的哪月,只关心是哪天)
}
///////////////////////////////////////////////////////////////////////////////
实例:
对数字,日期进行的
SQL> select trunc(sysdate) from dual;
TRUNC(SYSD
----------
07-1月 -03
SQL> select trunc(sysdate,'mm') from dual;
TRUNC(SYSD
----------
01-1月 -03
SQL> select trunc(sysdate,'yy') from dual;
TRUNC(SYSD
----------
01-1月 -03
SQL> select trunc(234.5565) from dual;
TRUNC(234.5565)
---------------
234
SQL> select trunc(sysdate,'D') from dual;
TRUNC(SYSD
----------
05-1月 -03
////////////////////////////////////////////////////////////////////////////
select trunc(sysdate ,'dd') from dual ; -- 2007-9-19
select trunc(sysdate ,'yyyy') from dual ; --2007-1-1
select trunc(sysdate ,'mm') from dual ; --2007-9-1
begin
dbms_output.put_line( to_char ( (sysdate) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( to_char ( (sysdate)+ 1/24/60/10 , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( to_char ( ((sysdate)+ 10 / ( 24*60*60 ) ) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( to_char ( trunc((sysdate)+ 10 / ( 24*60*60 ) ) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
end ;
begin
dbms_output.put_line( '当前时间 ' ) ;
dbms_output.put_line( to_char ( (sysdate) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( '当前时间 + 1 s ' ) ;
dbms_output.put_line( to_char ( (sysdate)+ (((1/24)/60)/60 ) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( '当前时间 + 1 s ' ) ;
dbms_output.put_line( to_char ( (sysdate)+ (((5/24)/60)/60 ) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( '当前时间 + 10s ' ) ;
dbms_output.put_line( to_char ( ((sysdate)+ ( 10 / ( 24*60*60 )) ) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( '当前 日 ' ) ;
dbms_output.put_line( to_char ( trunc((sysdate)) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( '当前 第2天 1点 ' ) ;
dbms_output.put_line( to_char ( trunc(sysdate)+( 1 + 1/24 ) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
dbms_output.put_line( '当前 第2天 9点 ' ) ;
dbms_output.put_line( to_char ( trunc(sysdate)+( 1 + 9/24 ) , 'yyyy-mm-dd hh24:mi:ss' ) ) ;
end ;
/
////////////////////////////////////////////////////////////////////////////
Oracle 日期常用函數 (SYSDATE、日期格式)
SYSDATE
--◎ 可得到目前系統的時間
ex.
select sysdate from dual;
sysdate
----------
20-SEP-07
常用之日期格式
日期格式 說明
------------------------------------------------------------------------
YYYY/MM/DD -- 年/月/日
YYYY -- 年(4位)
YYY -- 年(3位)
YY -- 年(2位)
MM -- 月份
DD -- 日期
D -- 星期
-- 星期日 = 1 星期一 = 2 星期二 = 3
-- 星期三 = 4 星期四 = 5 星期五 = 6 星期六 = 7
DDD -- 一年之第幾天
WW -- 一年之第幾週
W -- 一月之第幾週
YYYY/MM/DD HH24:MI:SS -- 年/月/日 時(24小時制):分:秒
YYYY/MM/DD HH:MI:SS -- 年/月/日 時(非24小時制):分:秒
J -- Julian day,Bc 4712/01/01 為1
RR/MM/DD -- 公元2000問題
-- 00-49 = 下世紀;50-99 = 本世紀
ex.
select to_char(sysdate,'YYYY/MM/DD') FROM DUAL; -- 2007/09/20
select to_char(sysdate,'YYYY') FROM DUAL; -- 2007
select to_char(sysdate,'YYY') FROM DUAL; -- 007
select to_char(sysdate,'YY') FROM DUAL; -- 07
select to_char(sysdate,'MM') FROM DUAL; -- 09
select to_char(sysdate,'DD') FROM DUAL; -- 20
select to_char(sysdate,'D') FROM DUAL; -- 5
select to_char(sysdate,'DDD') FROM DUAL; -- 263
select to_char(sysdate,'WW') FROM DUAL; -- 38
select to_char(sysdate,'W') FROM DUAL; -- 3
select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') FROM DUAL; -- 2007/09/20 15:24:13
select to_char(sysdate,'YYYY/MM/DD HH:MI:SS') FROM DUAL; -- 2007/09/20 03:25:23
select to_char(sysdate,'J') FROM DUAL; -- 2454364
select to_char(sysdate,'RR/MM/DD') FROM DUAL; -- 07/09/20
二.ROUND函数
SELECT ROUND( number, [ decimal_places ] ) FROM DUAL
说明:
number : 将要处理的数值
decimal_places : 四舍五入,小数取几位,不填默认为0
Sample :
select round(123.456) from dual; 结果: 123
select round(123.456, 0) from dual; 结果: 123
select round(123.456, 1) from dual; 结果: 123.5
select round(123.456, 2) from dual; 结果:123.46
select round(123.456, 3) from dual; 结果: 123.456
select round(-123.456, 2) from dual; 结果:-123.46

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
