6. Basic SQL sorting (date and time data types, conversion functions) and SQL data types

Source: Internet
Author: User

6. Basic SQL sorting (date and time data types, conversion functions) and SQL data types
Date and Time Data Type

* System constant:

@ DATEFIRST (returns the current time)

DATEADD

Increase time

Syntax: DATEADD (datepart, number, date)

Select DATEADD (YEAR, 2, '1970-11-2 ')

 

DATEDIFF

Distance between two dates

Select DATEDIFF (YEAR, '2017-7-18 ', '2017-11-2 ')

 

DATENAME

Returns the number of blocks in a certain time value.

Select DATENAME (YEAR, '2017-7-8 ')

 

DATEPART

Select DATEPART (second, '2017-11-2 00:00:01. 123 ')

 

 

(DatenameThe returned string is datepart.Int is returned.Type)

Select DATENAME (weekday, '2017-11-2 ')

Select DATEPART (weekday, '2017-11-2 ')

 

DAY

Returns the day of the current month, and the int value.

Select DAY ('2017-11-2 ')

 

MONTH/YEAR

Similar to DAY

GETDATE

Get current server time

Select GETDATE ()

 

* GETUTCDATE is similar to GETDATE.

ISDATE

If the time format is used, "1" is returned"

Select ISDATE ('2014-2-29 ')

Select ISDATE ('2014-2-28 ')

 

SYSDATETIME

Obtain system time (precise but slow)

Select GETDATE ()

Select SYSDATETIME ()

 

Type conversion, Conversion Function

Cast, convert, parse

Select CAST (123 as varchar (20 ))

 

Select CONVERT (int, '123 ')

 

Parse cannot be used in version 08

 

Exercise

--- Cut the birthday

Select name, substring (cid,) + 'Year' + SUBSTRING (cid,) + 'month' + SUBSTRING (cid,) + 'day' from haha

 

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.