Skip to main content

SUBSTRING & INSTR in SQL

 Problem: 

I have a case where I need to extract the 853-259020- from 853-259020-101


 Solution: 

SUBSTRING("PRTNO", 1, INSTR("PRTNO", '-', 1, 2)) AS "Part Group"


🔍 Explanation of Each Function:

1. INSTR("PRTNO", '-', 1, 2)

  • This function searches for the position of the second occurrence of the hyphen - in the string "PRTNO".
  • Parameters:
    • "PRTNO": The column you're searching in.
    • '-': The character you're looking for.
    • 1: Start searching from the first character.
    • 2: Find the second occurrence.
  • Example: For '853-259020-101', the second hyphen is at position 11.

2. SUBSTRING("PRTNO", 1, INSTR(...))

  • This extracts a substring from "PRTNO", starting at position 1 (the beginning of the string), and ending at the position of the second hyphen.
  • So, it returns everything up to and including the second hyphen.

✅ Final Output:

For '853-259020-101', the result will be:

853-259020-

Comments

Popular posts from this blog

Convex Lens Vs Concave Lens

What is a Lens A lens is a transparent object that transmits light across it, and its working principle depends on the   law of refraction (Snell’s law) . Applications: Concave Lens help with Far-Sight Convex Lens help with Near-Sight

Herbal Plants

Herbal plants to grow in home Different types of keerai ----- http://www.indiandietrecipes.com/2016/03/pictures-of-various-types-of-greens-or.html Pirandai ----good for bone joins---- https://youtu.be/k2DGubyXLAE Valarai Keerai Benifits ----- https://youtu.be/33HIfLJKTqA Sesma seeds ---- https://easyayurveda.com/2011/02/24/sesame-and-sesame-oil-benefits-total-ayurveda-details/

What is Verbal and Non-Verbal communication

TYPES OF COMMUNICATIONS: