Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
closed by

I need to substring the seventh and eighth character from string by starting on the right

And I need to make dynamic this try :

select substring(right(P.Name,8), 1,2)

Please tell me where am I wrong?

closed

4 Answers

0 votes
by (13k points)
 
Best answer

Your approach is almost correct. However, there is a small mistake in the order of the parameters for the `substring` function. The correct order is `substring(string, start, length)`. 

To substring the seventh and eighth characters from the right side of a string, you can modify your query as follows:

SELECT SUBSTRING(RIGHT(P.Name, 8), 7, 2)

In this query, `RIGHT(P.Name, 8)` retrieves the rightmost 8 characters from the `P.Name` string. Then, `SUBSTRING` is used to extract a substring starting from the 7th character with a length of 2.

This will give you the desired result.

0 votes
by (11.7k points)

If you want SUBSTRING starting from the right according to your comments, you can use REVERSE:

SELECT SUBSTRING(REVERSE('123456789'),7,2)

If you want to get more insights of SQL, checkout this SQL Course from Intellipaat.

0 votes
by (7.8k points)
In your current query, you are correctly using the `RIGHT` function to extract the rightmost characters from the string `P.Name`. However, when using the `SUBSTRING` function, you should specify the starting position as `1` and the length as `2` to extract the seventh and eighth characters from the right substring.

Here's the modified version of your query:

SELECT SUBSTRING(RIGHT(P.Name, 8), 1, 2)

This will give you the desired result, extracting the seventh and eighth characters from the right substring of `P.Name`.

Remember, if you want to make it dynamic and extract the characters from the right based on a specific length, you can replace the number `8` in `RIGHT(P.Name, 8)` with a variable or a column value that represents the desired length.
0 votes
by (11.4k points)
Based on your description, it seems that you want to extract the seventh and eighth characters from the right end of a string in SQL.

To achieve this, you can use the `RIGHT` function to retrieve the rightmost characters of the string, and then apply the `SUBSTRING` function to extract the desired characters.

The correct syntax would be:

SELECT SUBSTRING(RIGHT(P.Name, 8), 7, 2)

FROM YourTable AS P

Here's an explanation of the syntax:

- `RIGHT(P.Name, 8)` retrieves the rightmost 8 characters from the `P.Name` column.

- `SUBSTRING(RIGHT(P.Name, 8), 7, 2)` extracts 2 characters starting from the 7th position of the right substring.

Make sure to replace `YourTable` with the actual name of your table where the column `P.Name` resides.

If you're encountering an error, please provide the specific error message or behavior you're experiencing, so I can assist you further.

Related questions

0 votes
1 answer
asked Mar 30, 2021 in SQL by rahulnayar01123 (6.1k points)
0 votes
1 answer
asked Apr 26, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
0 answers
asked Jun 24, 2021 in SQL by akhil.singh0123123 (320 points)

Browse Categories

...