The following code shows how to generate a date number. The total length of the number is 8, and the first five digits are the year and month information. The format is yymm, and the last four digits are the serial numbers.

Source: Internet
Author: User

-- The followingCodeThis example shows how to generate a date number. The total length of the number is 8. The first five digits are the year and month information. The format is yymm, and the last four digits are the serial numbers.
-- Create a view for the current date
Create view v_getdate
As
Select dt = convert (char (4), getdate (), 12)
Go

-- Get the function with the new number
Create Function f_nextbh ()
Returns char (8)
As
Begin
Declare @ DT char (4)
Select @ dt = dt from v_getdate
Return (
Select @ DT + right (10001 + isnull (right (max (BH), 4), 0), 4)
From TB with (xlock, paglock)
Where BH like @ DT + '% ')
End
Go

-- Apply a function to a table
Create Table Tb (
BH char (8) primary key default DBO. f_nextbh (),
Col INT)

-- Insert data
Insert Tb (COL) values (1)
Insert Tb (COL) values (2)
Insert Tb (COL) values (3)
Delete TB where Col = 3
Insert Tb (COL) values (4)
Insert Tb (BH, col) values (DBO. f_nextbh (), 14)

-- Display Results
Select * from TB
/* -- Result
BH col
-------------------
05040001 1
05040002 2
05040003 4
05040004 14

(The number of affected rows is 4)
--*/
Go

Drop table TB
Drop function f_nextbh
Drop view v_getdate

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.