Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a unique identifier using SQL

Tags:

sql

sql-server

Is there some SQL coding which can generate a unique identifier on the click of a get default button? I'm looking for a system to number physical paper files before they are placed into storage. Problem is there are 3 offices, and each office needs a sequential number system (i.e. P001, P002, P003 and C001, C002...).

Below is the code i have so far to generate the prefix to the unique id number.

SELECT CASE WHEN ptBranch=3 THEN 'P' WHEN ptBranch=4 THEN 'A' ELSE 'C' END + CONVERT(VARCHAR(2),GETDATE(),12) FROM LAMatter WHERE ptMatter = $Matter$

The idea will be that the code could generate the whole file number e.g. P110001, P110002 (where P, C or A denotes the office the file is in, and 11 denotes the year the file was placed into storage)

any pointers greatly appreciated

like image 960
James Parish Avatar asked Apr 15 '26 10:04

James Parish


1 Answers

The SQL Server function newid() will generate a GUID.

Your SQL queries do not generate buttons or anything else. SQL is a language for querying databases, not for writing software interfaces.

like image 156
Dan Grossman Avatar answered Apr 18 '26 00:04

Dan Grossman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!