How do I update part of the data in a Date field in a database using T-SQL (year? Month? Day? When Score of Seconds? )

Source: Internet
Author: User
Tags first string

Well, I've found some content on the Internet, and we've integrated some of the built-in methods of SQL Server

The first is the Convert method: https://msdn.microsoft.com/zh-cn/library/ms187928.aspx

CONVERT (data_type (length),data_to_be_converted,style)

This method is equivalent to formatting the date data into a string output.

And then the stuff method: https://msdn.microsoft.com/zh-cn/library/ms188043.aspx

STUFF (character_expression, start, length, replacewith_expression)

This method removes the specified length of characters from the beginning of the first string, and then inserts the second string at the beginning of the first string.

That would be a good local replacement.

Test:

 select  top  1  addtime, convert  ( nvarchar  (23 ), Addtime,121  ),  stuff  (convert  (nvarchar  (23 ), Addtime,121 ), 1 , 4 ,  '  

Well, it seems to be pretty good.

Here are the original posts: http://blog.csdn.net/yangbolg/article/details/18596311

--Modify the year of the D table Date field
Update D
Set Birth=stuff (convert (nvarchar, birth,120), 1, 4, ' 2012 ')

--Modify the month of the D table Date field
Update D
Set Birth=stuff (convert (nvarchar, birth,120), 6, 2, ' 3 ')

--Change the number of days in the D Table Date field
Update D
Set Birth=stuff (convert (nvarchar, birth,120), 9, 2, ' 25 ')

--Change the number of hours in the D Table Date field
Update D
Set Birth =stuff (convert (nvarchar, birth,120), 12, 2, ' 9 ')

--Change the number of minutes in the D Table Date field
Update D
Set Birth =stuff (convert (nvarchar, birth,120), 15, 2, ' 15 ')

--Modify the number of seconds in the D Table Date field
Update D
Set Birth =stuff (convert (nvarchar, birth,120), 18, 2, ' 30 ')

--Modify the year, hour of the D table Date field
Update D
Set Birth=convert (Datetime,stuff (Stuff (Convert (char,birth,120), 1, 4, ' 2011 '), 12, 2, ' 02 '))

--Modify the year of the table date field
Update D
Set Birth=dateadd (Year,datediff (Year,birth, ' 2016-01-01 '), birth)
Where year (birth) <>2012

--Modify the hour of the table date field
Update D
Set Birth=dateadd (Hour,3,birth)

How do I update part of the data in a Date field in a database using T-SQL (year? Month? Day? When Score of Seconds? )

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.