Question : Unique Transaction

I need a trigger to generate into a nvarchar field a unique alpha/numeric transaction id using the current full date/time/seconds

so it would be HSC + mytable.identity_field + full date string including seconds

for example
the result for record 251 inserted on 07/14/2010 @ 11:57:20 would look like..

HSC25107142010115720

thanks in advance!!

Answer : Unique Transaction

I recommend a different approach.  Store the timestamp created as  a field in it's own right.   Then, present your tranID as a computed column.  Like this:

create table mytable
(id int identity
,anycol varchar(10)
,tsCreated datetime default getdate()
,tranID as 'HSC' + cast(id as varchar(20)) +
replace(convert(varchar(10),tsCreated,101),'/','') + replace(convert(varchar(10),tsCreated,108),':','')
)

insert into myTable (anycol) select 'a'
insert into myTable (anycol) select 'b'

select * from mytable


PS.  It may be forward-thinking to also store the 'HSC' prefix as a column in it's own right.

Random Solutions  
 
programming4us programming4us