Use the value of a column to update itself.

Source: Internet
Author: User

-- Requirement Description: In signBook2, there is a column of date_time with a value of 201210201221. I need to change the first 2012 to 2014, so I wrote the following stored procedure
-- _ Id is a column in the table and is the primary key identity
Declare @ I intdeclare @ datetime varchar (50) set @ I = 613 while (@ I <678) beginset @ datetime = (select date_time from signBook2 where _ id = @ I) -- The following sentence is also acceptable -- select @ datetime = date_time from signBook2 where _ id = @ iprint @ datetime -- this sentence outputs the datetime value -- substring (expression, start, length) print substring (@ datetime, 5, 9) print str (@ datetime) -- at the beginning, I used this function, causing an error, the variable value is changed to '************' set @ datetime = '000000' + ltrim (substring (@ datetime, 2014 )) print @ datetimeupdate signBook2 set date_time = @ datetime where _ id = @ iset @ I = @ I + 1end
-- Conclusion: After online verification, str function-> str (nExpres [, nLength [, nDecimalPlaces]) -- nExpression ------ value expression to be calculated by str. -- nLength ------------ the length of characters returned by str. The length includes the characters occupied by the decimal point and the characters occupied by each number on the right of the decimal point. -- If the specified length is greater than the digits on the left of the decimal point, str () is filled with leading spaces to return the string; -- if the specified length is less than the digits on the left of the decimal point, str () returns an asterisk, indicates a value overflow. -- NDecimalPlaces --- returns the number of decimal places in the string by STR. To specify the number of decimal places, both nLength and nLength must be included. -- If the specified number of decimal places is smaller than the number of decimal places in nExpress, the excess number is truncated. -- Return Value Type-> numeric type-when the Number is converted to a string, a leading space is reserved for the unnumbered symbol. If the Number is positive, a string containing leading space is returned, and a plus sign is implied. Negative Numbers include minus signs (-) without leading spaces.

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.