SQL Server time Format Encyclopedia _mssql

Source: Internet
Author: User
Tags getdate
The specific URL is not saved, but I this person is accustomed to good code to remember the way to save, now posted out, I hope the original author understanding.
Copy Code code as follows:

---The first day of the month
Select CONVERT (varchar (), DATEADD (Mm,datediff (Mm,0,dateadd (Mm,-1,getdate ()), 0), 112)
---The last day of the month
Select CONVERT (varchar (), DATEADD (Ms,-3,dateadd (Mm,datediff (M,0,getdate ()), 0), 112)
--1. Show the first day of the month
SELECT DATEADD (Mm,datediff (Mm,0,getdate ()), 0)
Select CONVERT (Datetime,convert (varchar (8), GETDATE (), 120) + ' 01 ', 120)
--2. Show the last day of the month
Select DATEADD (Day,-1,convert (datetime,convert (varchar (8), DATEADD (Month,1,getdate ()), 120) + ' 01 ', 120)
SELECT DateAdd (Ms,-3,dateadd (Mm,datediff (M,0,getdate ()) +1,0))
--3. Last day of last month
SELECT DateAdd (Ms,-3,dateadd (Mm,datediff (Mm,0,getdate ()), 0)
--4. The first Monday of this month
Select DATEADD (Wk,datediff (wk,0, DATEADD (Dd,6-datepart (Day,getdate ()), GETDATE ()), 0)
--5. The first day of the year
SELECT DATEADD (Yy,datediff (Yy,0,getdate ()), 0)
--6. The last day of the year
SELECT DateAdd (Ms,-3,dateadd (Yy,datediff (Yy,0,getdate ()) +1,0))
--7. Last day of last year
SELECT DateAdd (Ms,-3,dateadd (Yy,datediff (Yy,0,getdate ()), 0)
--8. The first day of the quarter
SELECT DATEADD (Qq,datediff (Qq,0,getdate ()), 0)
--9. Monday of the Week
SELECT DATEADD (Wk,datediff (Wk,0,getdate ()), 0)
--10. Check the records for this month
SELECT * FROM tablename where DATEPART (mm, thedate) =datepart (mm, GETDATE ()) and DATEPART (yy, thedate) = DATEPART (yy, GETDA TE ())
--11. Check this week's records
SELECT * FROM tablename where DATEPART (wk, thedate) = DATEPART (wk, GETDATE ()) and DATEPART (yy, thedate) = DATEPART (yy, get DATE ())
--12 query for this season note: GETDATE () is a function to obtain system time.
SELECT * FROM TableName where DATEPART (QQ, thedate) = DATEPART (QQ, GETDATE ()) and DATEPART (yy, thedate) = DATEPART (yy, get DATE ())
--13. Get total days of the month:
Select DATEDIFF (Dd,getdate (), DATEADD (mm, 1, GETDATE ()))
Select DateDiff (mm, DateDiff (mm, ', GETDATE (), '), DateAdd (mm, DateDiff (mm, ', GETDATE ()), ' 1900-02-01 ') )
--14. Gets the current day of the week
SELECT Datename (Weekday, GETDATE ())
Select CONVERT (varchar (), GETDATE (), 101)--12/28/2007
Select CONVERT (varchar (), GETDATE (), 102)--2007.12.28
Select CONVERT (varchar (), GETDATE (), 103)--28/12/2007
Select CONVERT (varchar (), GETDATE (), 104)--28.12.2007
Select CONVERT (varchar (), GETDATE (), 105)--28-12-2007
Select CONVERT (varchar (), GETDATE (), 106)--28 12 2007
Select CONVERT (varchar (), GETDATE (), 107)--12 28, 2007
Select CONVERT (varchar (), GETDATE (), 108)--10:06:33
Select CONVERT (varchar (), GETDATE (), 109)--12 28 2007 1
Select CONVERT (varchar (), GETDATE (), 110)--12-28-2007
Select CONVERT (varchar (), GETDATE (), 111)--2007/12/28
Select CONVERT (varchar (), GETDATE (), 112)--20071228
Select CONVERT (varchar (), GETDATE (), 113)--28 12 2007 1
Select CONVERT (varchar (), GETDATE (), 114)--10:07:36:623
Select CONVERT (varchar, GETDATE (), 120)--2007-12-28 10:08:56
Select CONVERT (varchar), GETDATE (), 120)--2007-12-28
Select replace (replace (varchar, GETDATE (), 120), '-', '), ', ', ': ', ', ')--20071228101012
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.