--需求說明,在signBook2中有一列date_time,值是201210201221,我需要將其前面的2012,改成2014,所以寫了下面這段預存程序
--_id是表中的一列,是主鍵identity的
declare @i intdeclare @datetime varchar(50)set @i=613while(@i<678)beginset @datetime =(select date_time from signBook2 where _id=@i)--下面這句,也是可以的--select @datetime = date_time from signBook2 where _id = @iprint @datetime--此句輸出datetime的值--substring(expression,start,length)print substring(@datetime,5,9)print str(@datetime)--剛開始,我使用這個函數,造成了錯誤,此處會將變數的值變成'**********'set @datetime = '2014'+ltrim(substring(@datetime,5,9))print @datetimeupdate signBook2 set date_time=@datetime where _id=@iset @i=@i+1end
--總結,經網上查證,str函數->str(nExpres[,nLength[,nDecimalPlaces]])--nExpression------str要計算的數值運算式.--nLength------------str返回的字元長度。該長度包括小數點所佔的字元和小數點右邊每個數字所佔的字元。--如果指定長度大於小數點左邊數字位元,str()前置空格填充返回的字串;--如果指定長度小於小數點左邊的數字位元,str()返回一串星號,表示數值溢出。--nDecimalPlaces---由STR()返回字串中的小數位元。若要指定小數位元,必須同時包含nLength。--如果指定的小數位元小於nExpress中的小數位元,則截斷多餘的數字。--返回值類型->字元型--當數字轉換為字串時,始終未Number的符號保留一個前置空格,如果Number為正,則返回字串包含前置空格,並暗含加號。負數將包含減號(-),且沒有前置空格。