SQL Server take date time part

Source: Internet
Author: User
Tags getdate

In this article, the date GetDate () is made up of two parts, namely today's date and time of day: Select GetDate ()
With Datename () you can get the corresponding year, month, day, and then connect them together:

Select Datename (Year,getdate ()) + '-' +datename
(Month,getdate ()) + '-' +datename (Day,getdate ())

In addition, Datename () can also get hours, times, seconds, days of the week, weeks, respectively, as follows:

Select Datename (Hour,getdate ())
Select Datename (Minute,getdate ())
Select Datename (Second,getdate ())
Select Datename (Weekday,getdate ())
Select Datename (Week,getdate ())

Date type in SQL the default format for datetime is YYYY-MM-DD hh:mi:ss:mmm, but most of the time we just want to get his date part, not the later. The previous article mentions using the Datename () function to intercept a date that does not contain a time part, and now it is more simple to get to a date that does not contain time!


Use the CONVERT () function:

Select CONVERT (char), GetDate (), as Date

* The 3rd parameter is used to set the display style of the date type data, the following describes the parameters of several styles:

MM DD yyyy

101 mm/dd/yyyy

102 YYYY.MM.DD

103 dd/mm/yyyy

106 dd MM YYYY

108 Hh:mi:ss (Time)

111 YYYY/MM/DD

Yyyymmdd

Yyyy-mm-dd

SELECT CONVERT (varchar ( -), GETDATE (),0) to  9  .  9: 12AM SELECT CONVERT (varchar ( -), GETDATE (),1) to/ the/ OneSELECT CONVERT (varchar ( -), GETDATE (),2)11.05. theSELECT CONVERT (varchar ( -), GETDATE (),3) the/ to/ OneSELECT CONVERT (varchar ( -), GETDATE (),4)09.05. OneSELECT CONVERT (varchar ( -), GETDATE (),5) the- to- OneSELECT CONVERT (varchar ( -), GETDATE (),6) the  to  OneSELECT CONVERT (varchar ( -), GETDATE (),7) to  the, OneSELECT CONVERT (varchar ( -), GETDATE (),8) the: -: -SELECT CONVERT (varchar ( -), GETDATE (),9) to  9  .  9: -: -: 670AMSELECT CONVERT (varchar ( -), GETDATE (),Ten) to- the- OneSELECT CONVERT (varchar ( -), GETDATE (), One) One/ to/ theSELECT CONVERT (varchar ( -), GETDATE (), A)110509SELECT CONVERT (varchar ( -), GETDATE (), -) the  to  .  the: -: -:670SELECT CONVERT (varchar ( -), GETDATE (), -) the: -: -:670SELECT CONVERT (varchar ( -), GETDATE (), -) .- to- the  the: -: -SELECT CONVERT (varchar ( -), GETDATE (), +) .- to- the  the: -:14.670SELECT CONVERT (varchar ( -), GETDATE (), A) to/ the/ One  9: the: -amselect CONVERT (varchar ( -), GETDATE (), at) .- to- theSELECT CONVERT (varchar ( -), GETDATE (), -) the: the: -SELECT CONVERT (varchar ( -), GETDATE (), -) .- to- the  the: the:33.140SELECT CONVERT (varchar ( -), GETDATE (), -) to  9  .  9: 15AMSELECT CONVERT (varchar ( -), GETDATE (),101) to/ the/ .SELECT CONVERT (varchar ( -), GETDATE (),102)2011.05. theSELECT CONVERT (varchar ( -), GETDATE (),103) the/ to/ .SELECT CONVERT (varchar ( -), GETDATE (),104)09.05. .SELECT CONVERT (varchar ( -), GETDATE (), the) the- to- .SELECT CONVERT (varchar ( -), GETDATE (),106) the  to  .SELECT CONVERT (varchar ( -), GETDATE (),107) to  the, .SELECT CONVERT (varchar ( -), GETDATE (),108) the: -: -SELECT CONVERT (varchar ( -), GETDATE (),109) to  9  .  9: -: -: 543AMSELECT CONVERT (varchar ( -), GETDATE (), the) to- the- .SELECT CONVERT (varchar ( -), GETDATE (),111) ./ to/ theSELECT CONVERT (varchar ( -), GETDATE (), the)20110509SELECT CONVERT (varchar ( -), GETDATE (),113) the  to  .  the: -: +:857SELECT CONVERT (varchar ( -), GETDATE (), the) the: -: +:857SELECT CONVERT (varchar ( -), GETDATE (), -) .- to- the  the: -: +SELECT CONVERT (varchar ( -), GETDATE (),121) .- to- the  the: -:19.857SELECT CONVERT (varchar ( -), GETDATE (),126) .- to-09t09: -:19.857SELECT CONVERT (varchar ( -), GETDATE (), the)6????? ???????1432  9: -: +: 857AMSELECT CONVERT (varchar ( -), GETDATE (),131)6/ ./1432  9: -: +: 857AM

Transferred from: http://www.cnblogs.com/weiqt/articles/2040800.html

SQL Server take date time part

Related Article

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.