Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract Certain nth character from a string in SQL

I have a field that returns the value as xxx-xxx-xxx-xxxxx-xx-x. How do i extract the 10th character from that code.

like image 950
user3383390 Avatar asked Apr 29 '14 12:04

user3383390


People also ask

How do you find the Nth character of a string in SQL?

T-SQL's CHARINDEX() function is useful for parsing out characters within a string. However, it only returns the first occurrence of a character. Over at SQL Server Central, there is a function that Cade Bryant wrote that returns the Nth occurrence of a character.

How do you select nth value in SQL?

ROW_NUMBER (Window Function) ROW_NUMBER (Window Function) is a standard way of selecting the nth row of a table. It is supported by all the major databases like MySQL, SQL Server, Oracle, PostgreSQL, SQLite, etc.

How do I select the first 3 characters in SQL?

You can use LEN() or LENGTH()(in case of oracle sql) function to get the length of a column. SELECT LEN(column_name) FROM table_name; And you can use SUBSTRING or SUBSTR() function go get first three characters of a column.


2 Answers

select substring('xxx-xxx-xxx-xxxxx-xx-x', 10, 1)

The documentation for the function on MSDN is here.

The SQL Fiddle demo is here (with different letters so you can clearly see which letter is extracted).

like image 64
Joseph B Avatar answered Oct 15 '22 13:10

Joseph B


Use substring function

select substring('xxx-xxx-xax-xxxxx-xx-x', 10, 1)

like image 26
G one Avatar answered Oct 15 '22 14:10

G one