Home > database >  If the string type fields such as REGDATE (8, 20161230) format for '2016-12-30'
If the string type fields such as REGDATE (8, 20161230) format for '2016-12-30'

Time:10-03

In addition to joining together with substr method and direct call is not in a better way

CodePudding user response:

SELECT substr (' 20161230 ', 1, 4) | | '-' | | substr (' 20161230 ', 5, 2) | | '-' | | substr (' 20161230 ', 7, 2) the as REGDATE from dual
Such writing is affect efficiency

CodePudding user response:

Expression efficiency of computing is doesn't matter, but the writing is more easy to read, and date value is wrong (such as 20160230) will be an error, kill two birds with one stone,
 SELECT TO_CHAR (TO_DATE (' 20161230 ', 'yyyymmdd'), '- dd yyyy - mm) as REGDATE from dual 

CodePudding user response:

refer to the second floor Tiger_Zhao response:
expression operation is not efficiency, but the writing is more easy to read, and date values (such as 20160230) will be an error when wrong, kill two birds with one stone,
 SELECT TO_CHAR (TO_DATE (' 20161230 ', 'yyyymmdd'), '- dd yyyy - mm) as REGDATE from dual 

It would have been better, won't go wrong

CodePudding user response:

 
- if not verify the validity of the data, just mechanical splicing, consider regular

SQL>
SQL> As with m (
2 select '20161230' c from dual
3)
4 select regexp_replace (c, '(... ) (.. ) (.. ), 1 -, 2 - \ ', '3') result from m;
The RESULT
-- -- -- -- -- -- -- -- -- --
The 2016-12-30

SQL>

CodePudding user response:

Your approach should be to the fastest speed, because there is no transfer type and other operations, 2nd floor method is safe, because if it wasn't a date type complains
  • Related