当前位置:Gxlcms > 数据库问题 > SQL SERVER 字段统一补0方法

SQL SERVER 字段统一补0方法

时间:2021-07-01 10:21:17 帮助过:127人阅读

CREATE TABLE [Sales].[Customer](
[CustomerID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[PersonID] [int] NULL,
[StoreID] [int] NULL,
[TerritoryID] [int] NULL,
[AccountNumber] AS (isnull(‘AW‘+[dbo].[ufnLeadingZeros]([CustomerID]),‘‘)),
[rowguid] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_Customer_CustomerID] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

=====

Scalar function used by the Sales.Customer table to help set the account number.

---

CREATE FUNCTION [dbo].[ufnLeadingZeros](
   @Value int
)
RETURNS varchar(8)
WITH SCHEMABINDING
AS
BEGIN
   DECLARE @ReturnValue varchar(8);

   SET @ReturnValue = CONVERT(varchar(8), @Value);
   SET @ReturnValue = REPLICATE(‘0‘, 8 - DATALENGTH(@ReturnValue)) + @ReturnValue;

   RETURN (@ReturnValue);
END;

SQL SERVER 字段统一补0方法

标签:ret   work   store   cluster   char   identity   number   ica   var   

人气教程排行