-- 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.