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) 9 9:12am SELECT convert (varchar (), GETDATE (), 1) 05/09/11select CO Nvert (varchar), GETDATE (), 2) 11.05.09SELECT convert (varchar (), GETDATE (), 3) 09/05/11select convert (varchar ( (+), GETDATE (), 4) 09.05.11SELECT convert (varchar), GETDATE (), 5) 09-05-11select convert (varchar), GETDATE (), 6 ) 11SELECT CONVERT (varchar), GETDATE (), 7), 11SELECT convert (varchar, GETDATE (), 8) 09:13:14select CO Nvert (varchar), GETDATE (), 9) 9 9:13:14:670amselect CONVERT (varchar), GETDATE (), 05-09-11select CONV ERT (varchar), GETDATE (), one) 11/05/09select convert (varchar), GETDATE (), 110509SELECT convert (varchar (100) , GETDATE (), () 09:13:14:670select convert (varchar), GETDATE (), 09:13:14:670select convert (varchar (10 0), GETDATE (), 2011-05-09 09:13:14select convert (varchar), GETDATE (), 2011-05-09 09:13:14.670select convert (  varchar (+), GETDATE (), 22) 05/09/119:15:33 amselect CONVERT (varchar), GETDATE (), 2011-05-09select convert (varchar), GETDATE (), 24) 09:15:33select CONVERT (varchar), GETDATE (), 2011-05-09 09:15:33.140select convert (varchar), GETDATE (), 100 ) to 9 9:15amselect CONVERT (varchar (102), GETDATE (), 101) 05/09/2011select CONVERT (varchar (+), GETDATE (),) 20 11.05.09SELECT CONVERT (varchar), GETDATE (), 103) 09/05/2011select CONVERT (varchar), GETDATE (), 104) 09.05.2011SELECT CONVERT (varchar), GETDATE (), 09-05-2011select CONVERT (varchar), GETDATE (), 106) 09 05 2011SELECT CONVERT (varchar), GETDATE (), 107), 2011SELECT CONVERT (varchar (+), GETDATE (), 108) 09:16:38select C Onvert (varchar), GETDATE (), 109) 9 9:16:38:543amselect CONVERT (varchar (+), GETDATE (), +) 05-09-2011selec T CONVERT (varchar), GETDATE (), 111) 2011/05/09select CONVERT (varchar), GETDATE (), 20110509SELECT convert ( varchar (+), GETDATE (), 113) 09:17:19:857select CONVERT(varchar), GETDATE (), 09:17:19:857select CONVERT (varchar), GETDATE (), 2011-05-09 09:17:19select CONVERT (varchar), GETDATE (), 121) 2011-05-09 09:17:19.857select CONVERT (varchar), GETDATE (), 126) 2011-05-09t09:17:19.857select CONVERT (varchar), GETDATE (), 6????? ??????? 1432 9:17:19:857amselect CONVERT (varchar (+), GETDATE (), 131) 6/06/1432 9:17:19:857am

  

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.