Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:12:42+00:00 2026-05-27T19:12:42+00:00

I generating a string for my Primarykey column similar identity function. but its behavior

  • 0

I generating a string for my Primarykey column similar identity function. but its behavior is unexpected.
it Removing all 0 by String and storing only a single character. Correct me Where am i wrong..

Alter FUNCTION [dbo].[UDF_GenerateBatchNumber]  (@BatchNumber as Varchar(10))
Returns Varchar
AS
BEGIN
    -- Declare the return variable here

    -- Add the T-SQL statements to compute the return value here
    Select Top(1) @BatchNumber=[BatchNo] From TBL_iBATCH Order by [BatchNo] desc
    IF @BatchNumber is Null
        BEGIN
         Set @BatchNumber ='B000001'
        END
    Else
        BEGIN
          Set @BatchNumber='B'+Convert(Varchar(7),(Cast(SUBSTRING(@BatchNumber,2,7) as Integer)+1))
        END  

    RETURN @BatchNumber

END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:12:43+00:00Added an answer on May 27, 2026 at 7:12 pm

    The usual way is to have a computed column using your IDENTITY column.

    This UDF will fail under load and give 2 keys the same

    ALTER  TABLE TBL_iBATCH
       ADD BatchNumber AS CAST('B' + RIGHT('000000' + CAST(MyIDCol AS varchar(6)), 6) AS char(7)) PERSISTED;
    
    ALTER  TABLE TBL_iBATCHWITH CHECK 
       ADD CONSTRAINT PK_TBL_iBATCH PRIMARY KEY (BatchNumber);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For generating random string i written code like this But if the function is
I dont think its needed but i was curious instead of generating a query
I'm currently generating an 8-character pseudo-random uppercase string for A .. Z: value =
Generating normal columnar data in excel file is quite easy but does any one
Im generating a file to present to the user for download, but the server
I've build a random string generator but I'm having a problem whereby if I
I'm generating a string in PHP and then eventually passing this string into a
I am generating a string, which right after generating I want to display a
Is there way of generating a string made up of 250 underscores, without using
I've been trying to find a more pythonic way of generating random string in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.