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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:38:38+00:00 2026-05-15T18:38:38+00:00

I want to take the 01 part of a string abcd_01 using SQL. What

  • 0

I want to take the 01 part of a string abcd_01 using SQL. What should be the query for this, where the length before the _ varies? That is, it may be abcde_01 or ab_01. Basically, I want part after the _.

  • 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-15T18:38:39+00:00Added an answer on May 15, 2026 at 6:38 pm

    This is one of those examples of how there’s similar functionality between SQL and the various extensions, but are just different enough that you can not guarantee portability between all databases.

    The SUBSTRING keyword, using PostgreSQL syntax (no mention of pattern matching) is ANSI-99. Why this took them so long, I don’t know.

    The crux of your need is to obtain a substring of the existing column value, so you need to know what the database substring function(s) are called.

    Oracle


    SELECT SUBSTR('abcd_01', -2) FROM DUAL
    

    Oracle doesn’t have a RIGHT function, with is really just a wrapper for the substring function anyway. But Oracle’s SUBSTR does allow you to specify a negative number in order to process the string in reverse (end towards the start).

    SQL Server


    Two options – SUBSTRING, and RIGHT:

    SELECT SUBSTRING('abcd_01', LEN('abcd_01') - 1, 2)
    SELECT RIGHT('abcd_01', 2)
    

    For brevity, RIGHT is ideal. But for portability, SUBSTRING is a better choice…

    MySQL


    Like SQL Server, three options – SUBSTR, SUBSTRING, and RIGHT:

    SELECT SUBSTR('abcd_01', LENGTH('abcd_01') - 1, 2)
    SELECT SUBSTRING('abcd_01', LENGTH('abcd_01') - 1, 2)
    SELECT RIGHT('abcd_01', 2)
    

    PostgreSQL


    PostgreSQL only has SUBSTRING:

     SELECT SUBSTRING('abcd_01' FROM LENGTH('abcd_01')-1 for 2)
    

    …but it does support limited pattern matching, which you can see is not supported elsewhere.

    SQLite


    SQLite only supports SUBSTR:

    SELECT SUBSTR('abcd_01', LENGTH('abcd_01') - 1, 2)
    

    Conclusion


    Use RIGHT if it’s available, while SUBSTR/SUBSTRING would be better if there’s a need to port the query to other databases so it’s explicit to others what is happening and should be easier to find equivalent functionality.

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

Sidebar

Related Questions

Given this string: http://s.opencalais.com/1/pred/BusinessRelationType I want to get the last part of it: BusinessRelationType
I have a database (MS SQL) with a column that contains a query string.
Let’s take this string has an example: D:/firstdir/Another One/and 2/bla bla bla/media/reports/Darth_Vader_Report.pdf I want
I have an Expression<Func<T,DateTime>> I want to take the DateTime part of the expression
I want my Android app to take a picture, as part of something larger
I want to take a math expression that takes variables and print (assign to
I want to take HTML form data and handle the submited data(string for example)
I want to take advantage of the new features in Windows 7 using C#
Today my wish is to take text form the string. This string must be,
I am trying to pass a List<string> to a method that will take it

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.