logo

SUBSTRING_INDEX Funkcija u SQL-u

Funkcija niza SUBSTRING_INDEX prikazuje sve znakove prije određenog simbola u danom nizu.

Sintaksa funkcije niza SUBSTRING_INDEX

Sintaksa1: Ova sintaksa koristi SUBSTRING_INDEX s nazivom stupca SQL tablice:

 SELECT SUBSTRING_INDEX(Column_Name, symbol, starting_position) AS Alias_Name FROM Table_Name; 

U sintaksi moramo navesti naziv tog stupca na kojem želimo izvršiti funkciju niza SUBSTRING_INDEX.

Sintaksa2: Ova sintaksa koristi funkciju SUBSTRING_INDEX s nizom:

 SELECT SUBSTRING_INDEX(Original_String, symbol, starting_position); 

Primjeri funkcije niza SUBSTRING_INDEX

Primjer 1: Sljedeći SELECT upit prikazuje četiri znaka prije zadanog simbola u originalnoj riječi JAVA^TPOINT:

lokalni datumvrijeme java

SELECT SUBSTRING_INDEX( 'JAVA^TPOINT', '^', 1) AS SUBSTRING_INDEX_4_znakova;

Izlaz:

SUBSTRING_INDEX_4_znakova
JAVA

Primjer 2: Sljedeći SELECT upit prikazuje SUBSTRING_INDEX 20 znakova iz zadanog niza:

 SELECT SUBSTRING_INDEX( 'JAVATPOINT is a. good website', '.', 1 ) AS SUBSTRING_INDEX_before.symbol; 

Izlaz:

SUBSTRING_INDEX_before.symbol
JAVATPOINT je a

Primjer 3: Sljedeći SELECT upit prikazuje 5 znakova od trećeg mjesta do zadanog simbola u zadanoj riječi 'čestitke':

c++ cijepanje niza
 SELECT SUBSTRING_INDEX( '[email protected]', '@', 3) AS SUBSTRING_INDEX_before@symbol; 

Izlaz:

SUBSTRING_INDEX_before@symbol
VOLIM TO

Primjer 4: Sljedeći SELECT upit prikazuje 20 znakova od petog mjesta do zadanog simbola u zadanom nizu:

kako pronaći skrivene aplikacije na androidu
 SELECT SUBSTRING_INDEX('New Delhi is the capital# of India', '#', 5) AS SUBSTRING_INDEX_5_characters; 

Izlaz:

SUBSTRING_INDEX_prije#simbola
Delhi je glavni grad

Primjer 6: Ovaj primjer koristi funkciju SUBSTRING_INDEX s tablicom u Structured Query Language.

Da bismo razumjeli funkciju SUBSTRING_INDEX sa SQL-om, prvo moramo kreirati SQL tablicu koristeći naredbu CREATE. Sintaksa za stvaranje nove tablice u SQL bazi podataka je sljedeća:

 CREATE TABLE table_name ( First_Column_of_table Data Type (character_size of 1st Column), Second_Column_of_table Data Type (character_size of the 2nd column ), Third_Column_of_table Data Type (character_size of the 3rd column), ... Last_Column_of_table Data Type (character_size of the Nth column) ); 

Sljedeća naredba CREATE stvara Ocjene_učenika stol:

 CREATE TABLE Student_Marks ( Student_ID INT NOT NULL PRIMARY KEY, Student_First_Name VARCHAR (100), Student_Middle_Name VARCHAR (100), Student_Last_Name VARCHAR (100), Student_Class INT NOT NULL, Student_City Varchar(120), Student_State Varchar (80), Student_Marks INT ); 

Donji INSERT upiti umeću zapise fakultetskih fakulteta u Ocjene_učenika stol:

 INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Stude.nt_Marks) VALUES (4001, Ama.n, Roy, Sha#rma, 4, Chandigarh, Pun#jab, 88); INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES ( 4002, Vish.al, Gurr, Sh#arma, 8, Murthal, Harya#na, 95 ); INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4007, Raj., singhania, Gupt#a, 6, Ghaziabad, Uttar Pr#adesh, 91); INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4004, Yas.h, Chopra, Singh#ania, 9, Jaipur, Rajasthan#, 85); INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4011, Vin.ay, Sharma, Roy#, 8, Chandigarh, Punjab#, 94); INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4006, Man.oj, singhania, Gup#ta, 5, Ghaziabad, Uttar Pr#adesh, 83); INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4010, Ra.m, Raheem, Gupt#a, 9, Lucknow, Uttar Pra#desh, 89); 

Sljedeća izjava SELECT prikazuje umetnute zapise od gore navedenog Ocjene_učenika stol:

 SELECT * FROM Student_Marks; 

Školarac_Id Ime_školarca Školnik_srednje_ime Školarac_Prezime Školarac_razred Školarac_Grad Školarac_država Školarac_Marks
4001 Čovjek Roy Sha#rma 4 Chandigarh Pun#jab 88
4002 Vish.al Gurr S#oružje 8 Murthal Harya#na 95
4007 Raj. Singhania Gupt#a 6 Ghaziabad Uttar Pradesh 91
4004 Yas.h Chopra Singh#ania 9 Jaipur Rajasthan # 85
4011 Vin.ay Sharma Roy# 8 Chandigarh Punjab # 94
4006 čovječe Singhania Gup#ta 5 Ghaziabad Uttar Pra#desh 83
4010 Radna memorija Raheem Gupt#a 9 Lucknow Uttar Pradesh 89

Upit 1: Sljedeći SELECT upit koristi funkciju SUBSTRING_INDEX sa stupcem Student_First_Name gornje tablice Student_Marks:

 SELECT Student_First_Name, SUBSTRING_INDEX(Student_First_Name, ., 1) AS SUBSTRING_INDEX_before.symbol FROM Student_Marks; 

Ova SQL naredba prikazuje znakove od prvog mjesta do simbola # u imenu svakog učenika.

inače java

Izlaz:

Ime_učenika SUBSTRING_INDEX_before.symbol
Čovjek Ili
Vish.al dolazak
Raj. Raj
Yas.h Plastični
Vin.ay doći
čovječe Čovjek
Radna memorija Sunce

Upit 2: Sljedeći SELECT upit koristi funkciju SUBSTRING_INDEX sa stupcem Student_Last_Name gornje tablice Student_Marks:

 SELECT Student_Last_Name, SUBSTRING_INDEX(Student_Last_Name, #, 1) AS SUBSTRING_INDEX_before#symbol FROM Student_Marks; 

Ova SQL naredba prikazuje znakove od prvog mjesta do simbola # u prezimenu svakog učenika.

Izlaz:

c niz nizova
Prezime_učenika SUBSTRING_INDEX_prije#simbola
Sha#rma Piće
S#oružje Sh
Gupt#a Gupt
Singh#ania Singh
Roy# Roy
Gup#ta Gup
Gupt#a Gupt

Upit 3: Sljedeći SELECT upit koristi funkciju SUBSTRING_INDEX sa stupcem Student_Address gornje tablice Student_Marks:

 SELECT Student_State, SUBSTRING_INDEX(Student_State, @, 2) AS SUBSTRING_INDEX_@symbol FROM Student_Marks; 

Ova SQL naredba prikazuje znakove od drugog mjesta do zadanog simbola u stanju svakog učenika.

Izlaz:

Student_State PODNIZ_INDEX_@simbol
Pun@jab a
Harya@na arya
Uttar Pra@adesh ttar Pr
Rajasthan@ vrijeme mjesto
Punjab@ unjab
Uttar Pradesh@desh ttar Pra
Uttar Pra@adesh ttar Pr