Basic knowledge of SQL conversion functions (SQL Server)

Source: Internet
Author: User

1. Cast

Cast (columnname as nvarchar (30) columnname is converted to bytes

2. Round

Round (columnname, 2) takes two decimal places

3. Convert

Select convert (varchar (12), getdate (), 102) -- 2012.12.26
Select convert (varchar (12), getdate (), 111) -- 2012/12/26
Select convert (varchar (12), getdate (), 112) -- 20121226
Select convert (varchar (23), getdate (), 121) -- 23:21:56. 910
Select convert (varchar (19), getdate (), 120) -- 23:21:56
Select convert (varchar (10), getdate (), 120) --
Select convert (varchar (8), getdate (), 114) -- 23:21:56

4. getdate ()

Select getdate () -- 2012-12-26 23:21:56. 910

5. datepart

Select datepart (year, getdate () --- 2012
Select datepart (quarter, getdate () -- 4
Select datepart (month, getdate () -- 12
Select datepart (day, getdate () -- 26
Select Day (getdate () -- 26
Select datepart (dy, getdate () -- 361 [add dy to day of year, the day of the year]
Select datepart (weekday, getdate () -- 4 [minus 1, because foreign countries are different from China, indicating Today's Wednesday]
Select datepart (Week, getdate () -- 52 [week 52nd]
Select datepart (hour, getdate () -- 23
Select datepart (minute, getdate () -- 21
Select datepart (second, getdate () -- 56
Select datepart (millisecond, getdate () -- 910

6. datename

Select datename (day, getdate () -- 26
Select datename (dy, getdate () -- 361
Select datename (weekday, getdate () -- Wednesday [note the difference with datepart]
Select datename (Week, getdate () -- 52

7. dateadd, time Addition

Select dateadd (day, 3, getdate () -- 23:21:56. 910
Select dateadd (month, 3, getdate () -- 23:21:56. 910

8. datediff, time subtraction, followed by time minus the previous time difference

Select datediff (day, '2017-12-20 ', getdate () -- 6
Select datediff (month, '2014-12-20 ', getdate () -- 0

9. Rand

Select cast (floor (RAND (checksum (newid () * 100) as INT)

 

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.