Oracle date formatting and use of extract function

  Due to business needs, I have been learning Oracle in the past two days. I found that the date in Oracle will be converted into a very unfamiliar format for you, so I thought about how to display it in the format of year, month and day. Data and documents, and finally found a solution, in fact, the to_char method was used.

  For example, in Oracle, the date in a table is 2017-05-06, and the column name is time. After the query, it is found that the returned format is 06-5-17. It looks unpleasant. If you want to convert it to In the format of year month day, you can do this,

to_char(time, 'YYYY-MM-DD') as time // The time in parentheses indicates the column name in the table, and the second time indicates that the converted date column name is still time

  Now the converted date is like this, 2017-05-06

 

  So what does the extract function do? Extract in English means to extract, select, as the name suggests, it means to intercept a specific part from a date type, for example, select the year or month or day.

  For example, there is a form like this:

      

  Now I want to select all the data in time with the year 2018 from the table myTable, I can do this,  

select title,play,time from myTable where extract(year from time) = 2018;
或者:
select title,play,to_char(time, 'YYYY-MM-DD') as time from myTable where extract(year from time) = 2018

  The results are obviously all returned (here is just a demonstration)

 

  Now I want to select all the data whose month is 5 in time from the table myTable, the operation is:

select title,play,time from myTable extract(month from time) = 5;
或者:
select title,play,to_char(time, 'YYYY-MM-DD') as time from myTable where extract(month from time) = 5

  

  Select all data with date 6 in time from the table myTable, the operation is:

select title,play,time from myTable extract(day from time) = 6;
或者:
slect title,play,to_char(time, 'YYYY-MM-DD') as time from myTable where extract(day from time) = 6;

  

  语法如下:extract(year|month|day|hour|minute|second from column_name) = value

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325264274&siteId=291194637