How to write the code in Oracle SQL (like 'CCYYMMDD' into 102 )

前端 未结 3 1441
不知归路
不知归路 2021-01-28 10:12

How to write the code in Oracle SQL (like \'CCYYMMDD\' into 102 )? If someone will enter the date in the frontend, the value should return

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-28 10:33

    You cannot write a function to determine which numeric date string corresponds to which format as the date string could be multiple formats:

    For example, 010203 could be:

    • Format 2: DDMMYY 1st February 03
    • Format 3: MMDDYY 2nd January 03
    • Format 101: YYMMDD 3rd February 01
    • Format 306: DDHHMM 02:03 of Day 1
    • Format 402: HHMMSS 01:02:03
    • Format 405: MMMMSS 102 minutes 3 seconds
    • Format 610: CCYYMM March 0102
    • Format 616: CCYYWW Week 3 of 0102

    Similarly 10080102 could be:

    • Format 4: DDMMCCYY 10th August 102
    • Format 102: CCYYMMDD 2nd January 1008
    • Format 305: MMDDHHMM 8th October 01:02
    • Format 501: HHMMHHMM Time span from 10:08 to 01:01

    If anyone inserts the date in this format "CCYYMMDD" then the value should return only 102 as a default in the frontend. How to write the code in SQL?

    You cannot, as I described above it is ambiguous what some values are and they could return multiple formats. Instead you should create another column and store the date format in that when the user inputs the date rather than trying to reconstruct the format code from an (ambiguous) number.

提交回复
热议问题