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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:28:08+00:00 2026-05-14T03:28:08+00:00

I am using SQL Server 2008 & 2005 (Express). I’m trying to extract part

  • 0

I am using SQL Server 2008 & 2005 (Express). I’m trying to extract part of an alpha numeric string from a varchar field.

RIGHT(str_field, 3) yields null values but SUBSTRING(str_field, LEN(str_field)-2, LEN(str_field)) gives the right value. LEFT(str_field, 7) gives the expected values. What gives?

I would have thought that RIGHT(str_field, 3) and SUBSTRING(str_field, LEN(str_field)-2, LEN(str_field)) are equivalent expressions.

  • 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-14T03:28:09+00:00Added an answer on May 14, 2026 at 3:28 am

    You have trailing spaces

    RIGHT will yield spaces but LEN ignores trailing spaces

    DECLARE @foo varchar(100)
    SET @foo = 'abc12345def   ' --3 spaces
    
    --right or substring
    SELECT RIGHT(@foo, 3)
    SELECT SUBSTRING(@foo, LEN(@foo)-2, LEN(@foo))
    
    --demonstrate you get spaces
    SELECT REPLACE(RIGHT(@foo, 3), ' ', 'z') --single space
    
    --length differences
    SELECT LEN(@foo), DATALENGTH(@foo)
    
    --solution
    SELECT RIGHT(RTRIM(@foo), 3)
    --or trim your column values before storing
    

    See SET ANSI_PADDING

    Note: you won’t get NULL for non NULL input…

    --only NULL if you send in NULL
    SELECT RIGHT(NULL, 3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You just need to make some small changes to your… May 16, 2026 at 12:43 pm
  • Editorial Team
    Editorial Team added an answer The difference is that you cannot (directly) read the response… May 16, 2026 at 12:43 pm
  • Editorial Team
    Editorial Team added an answer This is the routine I use. Perhaps you'll find it… May 16, 2026 at 12:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm using the INFORMATION_SCHEMA views in Sql Server 2005 & 2008 to obtain metadata
I recently migrated a database from sql server 2005 to 2008 on windows server
Using sql server 2008 I am getting and invalid precision value error in the
I am using SQL Server 2008 Web Edition and it seems my SQL queries
We have updated our servers with Windows 2008 Server, but we are still using
I only have SQL Server 2008 (Dev Edition) on my development machine I only
This code was once working on sql server 2005. Now isolated in a visual
In some of our SQL Server Reporting Services reports we wrote for SQL 2005,
I am getting following error. Restore failed for Server I have recently upgraded SQL
I'm using LLBL in a solution for both MS SQL Compact and regular MS

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.