Created
April 10, 2025 19:49
-
-
Save taniomi/d6d63046923c5da9c5ea20cad3e4632d to your computer and use it in GitHub Desktop.
Id creation and formatting with computed column in SQLServer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Add auto-incrementing primary key column starting from 1 and incrementing 1 | |
| ALTER TABLE dbo.invoice | |
| ADD Id INT IDENTITY(1,1) PRIMARY KEY; | |
| -- Add computed column with formatted InvoiceId (e.g., F-00000042) | |
| ALTER TABLE dbo.invoice | |
| ADD InvoiceId AS ( | |
| 'F-' + RIGHT('00000000' + CAST(Id AS VARCHAR(8)), 8) | |
| ) PERSISTED; | |
| -- Preview the first 8 rows | |
| SELECT TOP 8 * | |
| FROM dbo.invoice; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment