当前位置:网站首页>Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
2022-07-05 18:47:00 【HaHa_ Sir】
Oracle Date format conversion to_date,to_char,to_timetamp transformation
One 、 summary
1、 Main records of this paper Oracle In the database , Common date format conversion . Use to_char function , Convert date to string ; Use to_date 、to_timetamp function , Convert the string to the corresponding date type .
2、Oracle The database version is :Oracle 11g.
Two 、 Date conversion string to_char
1、 frequently-used 2 A date function :SYSDATE , SYSTIMESTAMP
SELECT SYSDATE , SYSTIMESTAMP FROM dual ;
2、SYSDATE Turn the string
SELECT
SYSDATE
, TO_CHAR(SYSDATE, 'yyyy-mm-dd hh:mm:ss') AS "h12" -- 12 hourly
, TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mi:ss') AS "h24" -- 24 The hour system
FROM dual ;
3、SYSTIMESTAMP Turn the string
SELECT
SYSTIMESTAMP
, TO_CHAR(SYSTIMESTAMP, 'yyyy-mm-dd hh24:mi:ss') AS "h24" -- 24 Hours
, TO_CHAR(SYSTIMESTAMP, 'yyyy-mm-dd hh24:mi:ss.ff3') AS "h24mi" -- 24 Hours mi
FROM
dual ;
4、SYSDATE Common conversion
SELECT
TO_CHAR(SYSDATE , 'yyyy') AS year ,
TO_CHAR(to_date('2022-07-01', 'yyyy-mm-dd'), 'q') AS quarter ,
TO_CHAR(SYSDATE , 'mm') AS month ,
TO_CHAR(SYSDATE , 'dd') AS Japan , -- The day of the month
TO_CHAR(SYSDATE , 'd') AS " Japan ( Zhou )", -- What day of the week ( The first day is Sunday )
TO_CHAR(SYSDATE , 'ddd') AS " Japan ( year )" , -- What day of the year
TO_CHAR(SYSDATE , 'ww') AS " Zhou ( year )", -- The weeks of the year
TO_CHAR(to_date('2022-01-08', 'yyyy-mm-dd') , 'ww') AS " Zhou ( year )" -- The weeks of the year
FROM
dual ;
3、 ... and 、 String to date
1、to_date String rotation date :
SELECT
TO_DATE('2022-06-20 17:08:06' , 'yyyy-mm-dd hh24:mi:ss') AS "date"
FROM
dual ;
2、TO_TIMESTAMP String rotation date :
SELECT
TO_TIMESTAMP('2022-06-20 17:08:06.114', 'yyyy-mm-dd hh24:mi:ss.ff3') AS "t1"
, TO_TIMESTAMP('2022-06-20 17:08:06', 'yyyy-mm-dd hh24:mi:ss') AS "t1"
FROM
dual ;
Four 、 summary
1、Oracle in , Date to string : Use to_char Function , but to_char Function also supports more format conversions , The parameters you can refer to are as follows :
【 The first parameter is a numeric type 】
Serial number | Format | A brief example | explain |
1 | ,( comma ) | '9999,999' | comma , It usually appears in the thousandth place , Used as a grouping symbol . If you need to, you can also take it as a tenth , The percentile appears , Can appear N Time , It depends on the size of the number . |
An example of perversion is to_char(1234,'9,9,9,9'). | |||
matters needing attention : Can only appear in the integral part . | |||
2 | .( Order number ) | '99.99' | Order number , Don't read it as " Full stop ", A full stop is a circle , A good point can only appear in the place corresponding to the decimal point . Only once . |
to_char(1234.34,'9,9,9,9.99') | |||
matters needing attention : Only in one place , It's the decimal point of the original data | |||
3 | $( Dollar symbol ) | '$999.99' | dollar . You can actually put it anywhere ( stay 10G Next ) |
to_char(1234.34,'9,9,9,9.$99') | |||
matters needing attention : Only once . | |||
4 | 0( zero ) | '0999.99' | zero . Returns the corresponding character in the corresponding position , If not, take '0' fill . |
to_char(0.34,'9,9,9,0.$99')='$0.34';to_char(1234,'9999.00')='1234.00'; | |||
matters needing attention : It's a mandatory symbol , There is no corresponding bit , with 'o' fill , This is a 9 It's very different | |||
5 | 9 | '999.99' | 9. In decimal places , It means to convert to the corresponding character , If not, take 0 Express ; In the whole number , If there is no correspondence, the characters will not be filled . |
to_char(123,'999.99')=123.00; TO_CHAR(123,'99999.9')=123.0; | |||
matters needing attention : about 0 and 9 for , If the number of digits in the format is less than the number of digits in the number , Returns the '#'. | |||
for example to_char(12345,'9999')='#####' | |||
6 | B( Space character ) | 'B999' | No other special effect , Add a space to the front of the integer part , Can appear anywhere . |
'S'||TO_CHAR(1234,'99B99')='S 1234'; | |||
matters needing attention : It can only appear in the integer part . | |||
7 | C( International currency symbol ) | 'C9999' | Returns a... At a specific location ISO Currency symbols ( Namely NLS_ISO_CURRENCY The value represented by the parameter ) |
TO_CHAR(1233,'C9999')='CNY1234' , This is a new international standard RMB, You can find out about this " International currency symbol " | |||
matters needing attention : It can only appear in the first digit of the integer part . | |||
Can pass alter session set NLS_ISO_CURRENCY='JAPAN'; To modify the settings of the current session . | |||
8 | D(ISO Decimal symbols ) | '999D99' | This is a " Order number " International version of (ISO), It's equivalent to a point , It can only appear once . The difference is , The database will be based on NLS_NUMERIC_CHARACTER To set the content . The default value is the dot . |
matters needing attention : There is no special need. Generally, do not use this format symbol . Don't change the parameter value easily . | |||
Also available alter sesssion set To modify the . | |||
alter session set nls_numeric_characters='!,'; to_char(1234.34,'9999d99')=1234!34 | |||
9 | EEEE( Scientific operators ) | 9.9EEEE | Scientific computing symbols |
TO_CHAR(2008032001,'9.9EEEE')='2.01E+09', Because it's a scientific calculation method , So add one before the decimal 9 perhaps 0 that will do , There's no point in having more than one . | |||
10 | G( Grouping symbols ) | 999G999 | It's a comma (,) Of course ISO standard , Used as a grouping symbol , It can be used in many places . |
TO_CHAR(123456,'999G9G99')=123,4,56 | |||
matters needing attention : Same as item 8 -D, In addition, if you want to convert the decimal point , It's going to be with D In combination with , Can't match the point number . | |||
11 | L( Local currency symbol ) | 'L999' | yes C Local version of . It can be placed at the front and back of the whole format . |
TO_CHAR(123456,'999G9G99D00L')=123,4,56.00¥ | |||
matters needing attention : Same as item 7 C | |||
12 | MI( Minus sign ) | '9999MI' | If it's a negative number , Put a minus sign at the end (-), If it's a positive number , Then add a space to the tail |
to_char(1234,'9999mi')||'S'||TO_CHAR(-5678,'9999MI') =1234 S5678- | |||
matters needing attention : Can only be placed at the end of the format | |||
13 | PR( Symbol ) | 9999PR | It's another way to express negative numbers . If it's a positive number , Then add a space to the head ; If it's a negative number , Use parentheses <> Wrap up the numbers . |
TO_CHAR(-1234.89,'9G999D00PR')=<1,234.89> | |||
matters needing attention : Same as 12 | |||
14 | RN(rn) | RN(rn) | Integer (1-3999) Convert to Roman .RN Turn to capital ,rn It means lowercase . |
declare | |||
i int; | |||
begin | |||
for i in 1..20 loop | |||
dbms_output.put_line(to_char(i,'RN')); | |||
end loop; | |||
end; | |||
matters needing attention : You can only use , Cannot be used in combination with other symbols . | |||
15 | S | '9999S' | yes 12,13 A comprehensive and improved version of . Add a positive sign to an integer +, Add a sign to a negative number -.S In the front, add in the front , After, after . |
TO_CHAR(-1234,'S9999')=-1234;TO_CHAR(1234,'S9999')=+1234 | |||
16 | TM | TM9/TMe | To use this parameter is to use no parameter to_char(number) equally , Should be 'tm9' Is the default format parameter . |
to_char(1234,'tme')=1234 | |||
matters needing attention : The format is either TM9, Or TME. | |||
When the number length exceeds 64 At the same time ,TM9 The output of is equivalent to TME Output . | |||
17 | U | U999 | Double Coin symbol , For example, the euro . Work with 11 Of L |
TO_CHAR(999,'U999')=¥999 | |||
matters needing attention : adopt NLS_DUAL_CURRENCY control | |||
18 | V | 999V9 | It's a little weird , It's not a very common symbol . Its purpose is to do a calculation . |
for example TO_CHAR(N,'999V9'), With p Express V The location of , Then the expression =to_char(N×(10 Of P-1 Power )). however 9 The number must be greater than or equal to the number of digits after the product . | |||
TO_CHAR(5,'9V')=5*1=5; | |||
TO_CHAR(5,'9V9')=5*10=50 | |||
TO_CHAR(5,'9V99')=500 | |||
TO_CHAR(50,'9V99')='######' 9 There are not enough of them | |||
matters needing attention : The format cannot be written with decimal expression , But you can mix currencies and so on . | |||
19 | X | xxxx | Convert to 16 Base number . |
TO_CHAR(100,'XX')= 64 | |||
matters needing attention : The value must be greater than or equal to 0 The integer of . The front can only be connected with 0 perhaps FM Use a combination of . | |||
20 |
|
| Through the above example , We learned about the formats of various numbers . It can be said that there are too many formats , It's hard to remember , It's best to exist as a reference . |
classified : | |||
Numerical class : 0,9, | |||
Grouping classes : (.),(,),D,G , There's a dot good and a comma because it's not obvious , So use parentheses to highlight . | |||
Currency : $,C,L,U | |||
Compute transformation class :EEEE,RN,V,X | |||
Positive and negative signs :MI,PR,S | |||
Other classes :B | |||
Orthodox class :TM |
【 The first parameter is zero : Date type 】
Serial number | Format | A brief example | explain |
1 | - / , . ; : | A little | The time separator , Except for the standard ones , Text is also allowed as a segmentation symbol . |
"text" | for example to_char(sysdate,'YYYY" year "mm" month "dd" Japan "')=2008 year 04 month 24 Japan | ||
2 | AD |
| Latin Anno Domini Abbreviation , It means A.D . Will be based on nls The difference between A.D. and A.D ad etc. |
A.D. | No special considerations | ||
3 | AM |
| Short for morning , Same as pm, p.m. ( Afternoon ) , The output of Chinese environment is in the morning ( If it's morning ) |
A.M. | |||
4 | BC |
| Although the standard writing is B.c. (c A lowercase letter ) perhaps BC, Fortunately Oracle I don't care about this . B.C |
B.C. | |||
5 | CC |
| Back in the century , In Arabic numerals |
SCC | If the last two places of the year are between 01-99 that , Back to the top two +1, Otherwise go back to the top two | ||
6 | D |
| One day of the week , It returns the serial number 1-7 |
7 | DAY |
| One day of the week , But it's just the day of the week , It has to do with language settings , In the Chinese environment NLS_DATE_LANGUAGE=SIMPLIFIED CHINESE , From Monday to Sunday |
8 | DD |
| One day in the month (1-31) |
9 | DDD |
| One day in the year (1-366) |
10 | DL | 'DL' | Returns the long date format . suffer NLS_TERRITORY,NLS_LANGUAGE Parameter control . example 2008 year 4 month 28 Japan Monday |
Limit : except DL, Nothing else can be set . | |||
11 | DS |
| Returns the short date format . suffer NLS_TERRITORY,NLS_LANGUAGE Parameter control . for example 2008-04-28 |
Limit : except DL, Nothing else can be set . | |||
12 | DY |
| Abbreviation of date , It's the day of the week ( Of course, this refers to the Chinese environment ) |
13 | E |
| Era is short for , But only for the following centralized calendars : The Japanese royal family , Republic of China , Too much Buddhist calendar |
14 | EE |
| The whole journey of the era , Suitable for the situation E |
15 | FF [1..9] |
| It's milliseconds , If you don't change the number, you use the default precision . |
It can only be used for timestamp Type of . | |||
16 | FM |
| A function worth noting : Don't return anything . |
I don't understand oracle Why set this thing . | |||
17 | FX |
| ditto |
18 | HH |
| For hours , by 12 hourly , Same as hh12(1-12) |
19 | HH12 |
| For hours , by 12 hourly (1-12) |
20 | HH24 |
| For hours , by 24 hourly (0-23) |
21 | IW |
| ISO Standard week number (1-52, perhaps 1-53) |
22 | IYYY |
| IYY,IY,I, ISO year (4 position ) Of 4,3,2,1 Digit number ( Reciprocal ) |
IYY | to_char(to_date(21120401,'yyyymmdd'),'iyyy, iyy,iy,i')=2112, 112,12,2 | ||
IY |
| ||
I |
| ||
23 | J |
| Julian day ( A calendar often used in astronomy ), From b.c. 4712 From January 1, 2008 , The result is an integer , The algorithm is basically ( The date of A.D +4712)* Julian calendar year average days |
24 | MI |
| second (0-59) |
25 | MM |
| 2 A month (1-12) |
26 | MON |
| The abbreviation of the month , It has something to do with the country NLS_DATE_LANGUAGE, for example 04 Using in Chinese environment 4 Monthly representation . |
27 | MONTH |
| The name of the month , The country has something to do with NLS_DATE_LANGUAGE, Now in Chinese 04 Expressed as 4 month . |
28 | PM |
| Same as am,a.m. In the afternoon |
P.M. | |||
29 | Q |
| quarter (1-4) |
30 | RM |
| The month in Roman numerals ,I ,II ,III ,IV ,V ,VI ,VII ,VIII,IX ,X ,XI ,XII |
31 | RR |
| It's a bit of a round off for years , The specific usage is a little complicated . |
With s Represents the last two digits of the year entered ,c Represents the last two digits of the current year , Its output ( The top two in the new year ) You can use functions r=f(s,c) To express ,s2,c2 respectively s,c The first two of . | |||
1)s=[0,49],c=[0,49], be r=c2 | |||
2) s=[0,49],c=[50,99], be r=c2+1 | |||
3) s=[50,99],c=[0,49], be r=c2-1 | |||
4) s=[50,99],c=[50,99], be r=c2 | |||
In short, the principle of being close to the current year , If it's in the same region as the current year, then it's the same , If it's larger than the current area , Then it is regarded as the current century and the previous century , Otherwise it will be the next century . | |||
for instance , With to_date As an example | |||
SQL> select to_date('89-01-01','rr-mm-dd') ,to_date('12-01-01','rr-mm-dd') FROM DUAL; | |||
| |||
TO_DATE('89-01-01','RR-MM-DD') TO_DATE('12-01-01','RR-MM-DD') | |||
------------------------------ ------------------------------ | |||
1989-01-01 2012-01-01 | |||
I think oracle I'm going to do this , It is estimated that there are two considerations. One is for convenience , One is to deal with a century or a millennium problem . | |||
32 | RRRR |
| If the input parameter has only two digits , Same with rr, Otherwise, it will be the same as yyyy effect . |
33 | SS |
| second (0-59), In a minute |
34 | SSSSS |
| The cumulative number of seconds a day from midnight .(0-86399) |
35 | TS |
| Return short date format content , Including hours, minutes, seconds, etc , And the only dl,ds Use a combination of , The format is : |
dl ts perhaps dl ts , The middle is separated by a space .TO_CHAR(SYSDATE,'TS')= Afternoon 4:50:04 | |||
Form is affected by NLS_TERRITORY and NLS_LANGUAGE influence . | |||
36 | TZD |
| Daylight saving time information , Time zone abbreviation plus daylight saving time information , Must be consistent with format tzr The set time zone corresponds to . |
Including the following three TZ At the beginning , It's all about time zones , Not directly for to_char | |||
37 | TZH |
| Hours in a time zone , for example hh:mi:ss.fftzh:tzm' |
38 | TZM |
| Minutes in the time zone . |
39 | TZR |
| Area information in the time zone , It must be a database supported time zone , for example US/Pacific |
40 | WW |
| and iw similar , It's also the serial number of the week , From the first day of the year to the last seventh day of the year . The two values are basically the same .(1-53) , for example 2008-01-01 To 2008-01-07 count 1,2008-01-09~2008-01-13 count 2 |
41 | W |
| The week of the month , The algorithm is the same as ww, It's just limited to one month , and iso Different . |
42 | X |
| Represents the local root symbol , It's not particularly useful , And the only timestamp Type used together . |
43 | Y,YYY |
| Four years , It's good to separate them for example 2,008 |
44 | YEAR |
| The year of articulation , for example 2008=two thousand eight |
SYEAR | S The prefix means B.C BC | ||
45 | YYYY |
| Four years ,S The prefix means B.C BC |
SYYYY | |||
46 | YYY |
| One time for the back 3,2,1 The year of a bit , for example 2008 It can be taken as 008,08,8 |
YY | |||
Y | |||
| summary |
| From the above , It is mainly the format of several parts of time : century 、 year , month , Japan , when , branch , second , millisecond , And other mixed formats . Every part of time can be expressed in many ways , It's easier to remember by classifying in this way . |
Many formats can be combined , In this way, we can finally form a rich enough expression of its situation ; | |||
Secondly, a lot of formats and nls Is closely related ; Finally, some outputs ( return ) It has something to do with format case , This is not reflected in the Chinese environment ( I haven't seen it so far ), however english Under the circumstances, under the name of , With to_char(sysdate,'day') As an example , If it's a western language environment, it's a return sun( hypothesis sysdate On weekends ), If to_char(sysdate,'DAY') Then return to SUN |
2、 Be careful :SYSDATE , SYSTIMESTAMP The time precision of the function is different ;SYSDATE Accurate to seconds ; SYSTIMESTAMP It can be accurate to seconds 6 position .
边栏推荐
- 紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
- SAP feature description
- sample_rate(采样率),sample(采样),duration(时长)是什么关系
- FCN: Fully Convolutional Networks for Semantic Segmentation
- 中文版Postman?功能真心强大!
- Powerful tool for collection processing
- How to automatically install pythn third-party libraries
- Linear table - abstract data type
- 华律网牵手观测云,上线系统全链路可观测平台
- 什么是文本挖掘 ?「建议收藏」
猜你喜欢
进程间通信(IPC):共享内存
2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
Redhat7.4 configure Yum software warehouse (rhel7.4)
LeetCode 6109. Number of people who know the secret
中文版Postman?功能真心强大!
How much does the mlperf list weigh when AI is named?
[HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD
Share: ZTE Yuanhang 30 Pro root unlock BL magick ZTE 7532n 8040n 9041n brush mask original brush package root method Download
Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换
随机推荐
Solutions contents have differences only in line separators
vs2017 qt的各种坑
Problems encountered in the project u-parse component rendering problems
The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!
小程序 修改样式 ( placeholder、checkbox的样式)
Take a look at semaphore, the current limiting tool provided by JUC
为什么 BI 软件都搞不定关联分析?带你分析分析
Case sharing | integrated construction of data operation and maintenance in the financial industry
Various pits of vs2017 QT
2022年5月腾讯云开发者社区视频月度榜单公布
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources
鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!
ROS installation error sudo: rosdep: command not found
Is it safe for Apple mobile phone to speculate in stocks? Is it a fraud to get new debts?
案例分享|金融业数据运营运维一体化建设
Chinese postman? Really powerful!
达梦数据库udf实现
Emqx 5.0 officially released: a single cluster supports 100million mqtt connections
Is it safe to make fund fixed investment on access letter?