Obtain a date that does not contain the time section in SQL + Oracle

Source: Internet
Author: User

In this article, the date obtained by getdate () is composed of two parts: today's date and then time: Select getdate ()

Use datename () to obtain the corresponding year, month, and day, and then connect them:

Select datename (year, getdate () + '-' + datename

(Month, getdate () + '-' + datename (day, getdate ())

In addition, datename () can also be used to obtain the hour, time, second, day of week, and week number, as shown below:

Select datename (hour, getdate ())

Select datename (minute, getdate ())

Select datename (second, getdate ())

Select datename (weekday, getdate ())

Select datename (Week, getdate ())

The default format of datetime in SQL is yyyy-mm-dd hh: MI: SS: Mmm. In most cases, we only want to get its date part, instead of the next time. As mentioned in the previous article, we used the datename () function to extract the date that does not contain the time part. Now we can use another method to obtain a date that does not contain the time!

Use the convert () function:

Select convert (char (10), getdate (), 120) as date

* The 3rd parameters are used to set the display style of the date data. The following describes several style parameters:

100mm dd yyyy

101/DD/YYYY

102 YYYY. Mm. dd

103 dd/mm/yyyy

106 DD MM YYYY

108 hh: MI: SS (time)

111 yyyy/mm/dd

112 yyyymmdd

120 yyyy-mm-dd

You can find that it is more convenient to use this method to obtain the date part. Compared with this method, the advantage of the datename () function is to obtain the specific part of the date.

In the Oracle database, if the field is of the Data Type, only a certain segment of the field is taken out. Here we use the tcrq table (production date, date type) as an example.

If both the date and time values are obtained, select * from table name directly, as long as the date: String AA = "select to_char (tcrq, 'yyyy-mm-dd ') as tcrq from fw_jcxxb "; time is the same.

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.