locked
How to generate a Sequence / custom value for PK field RRS feed

  • Question

  • User264732274 posted

    i want to write a login in function which will return a custom value for PK field. suppose if there is no data in table first time then a sample value will be generated for ID look like

    C000001

    C000002

    C000003

    so on

    C000010

    C000100

    so tell me how to achieve it with sample table data.

    thanks

    Wednesday, March 16, 2016 9:09 AM

Answers

  • User264732274 posted

    job done this way

    declare @number int
    declare @string varchar(10)
    declare @size_of_fixed_string tinyint
    
    set @size_of_fixed_string = 6
    set @number = 10
    
    
    
    set @string = 'C'+left( replicate( '0', @size_of_fixed_string ), @size_of_fixed_string - len( @number ) ) + cast( @number as varchar(10) )
    
    print @string

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 16, 2016 1:17 PM