6, SQL Basic collation (date time data type, conversion function)

Source: Internet
Author: User
Tags add time

Date Time Data type

* System constants:

@ @DATEFIRST (back to current time)

DATEADD

Add Time

Syntax: DATEADD (datepart, number, date)

Select DATEADD (year,2, ' 2013-11-2 ')

DATEDIFF

Distance between two dates

Select DATEDIFF (year, ' 2011-7-18 ', ' 2014-11-2 ')

Datename

Returns a time value in which you want to get the number of a block type

Select Datename (year, ' 2011-7-8 ')

DATEPART

Select DATEPART (Second, ' 2014-11-2 00:00:01.1234556 ')

(Datename returns a string, DatePart the return is an int type)

Select Datename (Weekday, ' 2014-11-2 ')

Select DATEPART (Weekday, ' 2014-11-2 ')

Day

Returns the day of the month in which the date is returned, an int value

Select Day (' 2014-11-2 ')

Month/year

Similar to day

GETDATE

Gets the current time of the server

Select GETDATE ()

*getutcdate is similar to getdate.

ISDATE

is the time format to return "1"

Select ISDATE (' 2013-2-29 ')

Select ISDATE (' 2013-2-28 ')

Sysdatetime

Get system time (precise, but slow)

Select GETDATE ()

Select Sysdatetime ()

Type conversions, conversion functions

Cast,convert,parse

Select CAST (123 as varchar )

Select CONVERT (int, ' 123 ')

Parse is not available in version 08

Practice

---intercept birthdays

Select Name,substring (cid,7,4) + ' year ' +substring (cid,11,2) + ' month ' +substring (cid,13,2) + ' Day ' from haha

6, SQL Basic collation (date time data type, conversion function)

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.