Skip to content

Instantly share code, notes, and snippets.

@taniomi
Created April 10, 2025 19:49
Show Gist options
  • Select an option

  • Save taniomi/d6d63046923c5da9c5ea20cad3e4632d to your computer and use it in GitHub Desktop.

Select an option

Save taniomi/d6d63046923c5da9c5ea20cad3e4632d to your computer and use it in GitHub Desktop.
Id creation and formatting with computed column in SQLServer
-- 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