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 8519537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:16:37+00:00 2026-06-11T06:16:37+00:00

There is my table : ID code(varchar) 1 006 2 008 3 005 4

  • 0

There is my table :

ID  code(varchar)
1   006
2   008
3   005
4   010
5   002

I have created a stored procedure:

CREATE PROC pp
    @code VARCHAR(10),
    @new_code VARCHAR(10) OUTPUT
AS
BEGIN 
    SELECT TOP 1 @new_code = RIGHT(1000+code+1,3) 
    FROM tb AS a 
    WHERE [Code] >= @code
    AND NOT EXISTS (SELECT 1 FROM tb WHERE [Code]=RIGHT(1000+a.[Code]+1,3)) 
    ORDER BY RIGHT(1000+code+1,3)
END 

When I call this stored procedure below the result is 011, it is correct:

DECLARE @code VARCHAR(3)

EXEC pp '009', @code OUTPUT

SELECT @code 

When I call the stored procedure below, the result is 011 remain, so the result is wrong. It should be ‘004’.

DECLARE @code VARCHAR(3)

EXEC pp '003', @code OUTPUT

SELECT @code 

How to correct this? TKS!

  • 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-06-11T06:16:39+00:00Added an answer on June 11, 2026 at 6:16 am

    It probably has something to do with the fact that you are selecting top 1, and ordering by a field that is not a number, but is a varchar, when you are expecting the order by to behave like a number. Have you tried casting/converting the [Code] field to an int, and then ordering by that value?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table as Student and there is a varchar type column as
I have a table called Jrl with three columns: code AS varchar(15) total AS
If there are are no values in the table how can should the code
I have an issue in entity framework code first, three table in database as
I have a table with the following fields: id VARCHAR(32) PRIMARY KEY, parent VARCHAR(32),
I want to know why note table doesn't create even there's no error in
I have a table with autoincremented primary key. In my code I am trying
I have tried to create a temporary table (sqlite) in Android Here is the
For example, there is a table: Table: PATIENT_DETAIL PATIENT_NAME(varchar), DISEASE(varchar), SICK_DEGREE(number), START_TIME(date), END_TIME(date) There
I was able to connect to mysql db and create a table but there

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.