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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:11:39+00:00 2026-05-29T08:11:39+00:00

I have values like 11(space), 109, X(space)(space), 9(space)(space), 15(space) etc. in a column called

  • 0

I have values like ” 11(space)”, “109”, “X(space)(space)”, “9(space)(space)”, “15(space)” etc. in a column called MyCode that is varchar(3).

When I order them I have a prolbme like 11 appears before 109 when I ORDER them in ASC order.

I want to align the values to right by adding empty char in front so that ORDER would result:

 " 11"

 .. 

"109"

Question: How can I write my SELECT MyChar FROM MyTable so that for a value “1(space)(space)” it should return “(space)(space)1” etc. ?


@gbn: Your answer did what I needed but the output didnt change:

enter image description here

  • 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-29T08:11:40+00:00Added an answer on May 29, 2026 at 8:11 am

    Use this to always pad with leading spaces to a fixed length

    SELECT RIGHT(
          SPACE(5) + 
          REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(MyChar), CHAR(160), ''), CHAR(9), ''), CHAR(10), ''), CHAR(13), '')
        , 5) AS Justified
    FROM MyTable 
    ORDER BY Justified
    

    If you change one 5, you need to change both

    Edit: added RTRIM

    Edit 2: added hard space + other non-printable detection

    What does this give you?

    SELECT ASCII(RIGHT(MyChar, 1) FROM MyTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have column with varchar values like 022008 that I need to convert into
I have some values in a configuration file (XML file) with some values like
Consider the scenario I have values assigned like these Amazon -1 Walmart -2 Target
I have variables with values like 1.7m 1.8k and 1.2b how can I convert
I have a list of values like this 1000, 20400 22200, 24444 The ranges
I have a String[] with values like so: public static final String[] VALUES =
I have two arrays of values like X,Y,Z and 1,2 There is a table
I have 3 tables with values like below **tbl_product** recID pID price colour 1
well i have this messages table with sample values like these: msg_id recipient_id read
I have a table where we record per user values like money_spent, money_spent_on_candy and

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.