The year, month, and day of the T-SQL time

Source: Internet
Author: User

Data that is often stored in the datetime format of SQL Server is sometimes, minute, second, and so on. However, when the page is read, the notification only requires year, month, and day,

You can use the following method to read data from the system:

View plaincopy to clipboardprint?

  1. Convert (varchar (12), time column name, 111)

 

For example, if the column operdate in table 1 is in the datetime format, you must read the year, month, and day of operdate.

View plaincopy to clipboardprint?

  1. Select convert (varchar (12), operdate, 111) from Table1

 

 

-============================================== ============

-- Some tips for getting time in SQL
*****************
-- ===================================================== ============
Select dateadd (mm, datediff (mm, 0, getdate (), 0)

Select datediff (mm, 0, getdate ())

Select dateadd (wk, datediff (wk, 0, getdate (), 0)
Select dateadd (YY, datediff (YY, 0, getdate (), 0)
Select dateadd (QQ, datediff (QQ, 0, getdate (), 0)

-------------- Midnight --------------------------------------------
Select dateadd (DD, datediff (DD, 0, getdate (), 0)

-------------- Last day --------------------------------------------
Select dateadd (MS,-3, dateadd (mm, datediff (mm, 0, getdate (), 0 ))
Select dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate (), 0 ))
Select dateadd (MS,-3, dateadd (mm, datediff (M, 0, getdate () + 1, 0 ))
Select dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate () + 1, 0 ))

Select dateadd (wk, datediff (wk, 0, dateadd (DD, 6-datepart (day, getdate (), getdate (), 0)

---------- Remove the hour, minute, second ----------------------------
Declare @ A datetime
Set @ A = getdate ()
Select @ A, dateadd (day, datediff (day, 0, @ A), 0)

---------- Weekly notice ----------------------------
Select datename (weekday, getdate ())

---------- How to obtain the data of a certain month -------------------
Declare @ M int
Set @ M = 3 -- month
Select datediff (day, '2017-'+ Cast (@ M as varchar) +'-15', '2017-'+ Cast (@ m + 1 as varchar) + '-15 ')

---------- Or use the Statement on the last day of the month, and then use the day function to calculate the last day of the month.
Select Day (dateadd (MS,-3, dateadd (mm, datediff (M, 0, getdate () + 1, 0 )))

---------- Determine whether to renew the subscription for one year :------------------------
Select case day (dateadd (mm, 2, dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate (), 0 )))) when 28 then 'Ping

'Else' begin year' end

Select case datediff (day, datename (year, getdate () + '-02-01', dateadd (mm, 1, datename (year, getdate () + '-02-

01 '))
When 28 then 'Year' else 'Year' end

---------- How many days a quarter ------------------------
Declare @ M tinyint, @ time smalldatetime
Select @ M = month (getdate ())
Select @ M = case when @ m between 1 and 3 then 1
When @ m between 4 and 6 then 4
When @ m between 7 and 9 then 7
Else 10 end
Select @ time = datename (year, getdate () + '-' + convert (varchar (10), @ M) + '-01'
Select datediff (day, @ time, dateadd (mm, 3, @ time ))

Select convert (varchar (8), dateadd (M, datediff (M, '', getdate (),''), 112) as [early month]
Select convert (varchar (8), dateadd (M,-3, getdate (), 112) as [three months ago]

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.