logo

Funkcija LENGTH u SQL-u

Funkcija niza LENGTH Structured Query Language vraća broj znakova zadanog niza ili riječi.

Sintaksa funkcije niza LENGTH

Sintaksa1: Ova sintaksa koristi funkciju LENGTH s nazivom stupca SQL tablice:

 SELECT LENGTH(Column_Name) AS Alias_Name FROM Table_Name; 

U sintaksi moramo navesti naziv tog stupca na kojem želimo izvršiti funkciju niza LENGTH za pronalaženje broja znakova svake vrijednosti.

Sintaksa2: Ova sintaksa koristi funkciju LENGTH s nizom:

 SELECT LENGTH(Original_String); 

Primjeri funkcije niza LENGTH

Primjer 1: Sljedeći SELECT upit prikazuje ukupan broj znakova date JAVATPOINT riječi:

poravnaj css sliku
 SELECT LENGTH( ' JAVATPOINT') AS LENGTH_word; 

Izlaz:

LENGTH_riječ
jedanaest

Primjer 2: Sljedeći SELECT upit prikazuje ukupan broj znakova zadanog niza:

 SELECT LENGTH( 'JAVATPOINT is a good website') AS LENGTH_string; 

Izlaz:

LENGTH_string
28

Primjer 3: Sljedeći SELECT upit prikazuje duljinu zadane rečenice:

 SELECT LENGTH( 'NEW DELHI IS THE CAPITAL OF INDIA') AS LENGTH_Sentence; 

Izlaz:

java hello world primjer
LENGTH_rečenica
33

Primjer 4: Sljedeći SELECT upit prikazuje duljinu zadanog niza:

 SELECT LENGTH( ' ' ) AS LENGTH_space; 

Izlaz:

LENGTH_razmak
1

Primjer 5: Sljedeći SELECT upit prikazuje duljinu riječi NULL:

 SELECT LENGTH( NULL ) AS Length; 

Izlaz:

Duljina
NULL

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

tigar u usporedbi s lavom

U ovom primjeru izradit ćemo novu SQL tablicu na kojoj želimo izvršiti funkciju LENGTH.

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 First Column), Second_Column_of_table Data Type (character_size of the Second column ), Third_Column_of_table Data Type (character_size of the Third column), ... Last_Column_of_table Data Type (character_size of the Last column) ); 

Sljedeća naredba CREATE stvara Employee_Grade stol:

apstrakcija u Javi
 CREATE TABLE Employee_Grade ( Employee_ID INT PRIMARY KEY, First_Name VARCHAR (100), Last_Name VARCHAR (100), First_City Varchar(120), Second_City Varchar(120), New_City Varchar(120), Attendance_Remarks INT, Work_Remarks INT, Grade Varchar (80) ); 

Donji INSERT upiti umeću zapise zaposlenika s ocjenama i primjedbama u Employee_Grade stol:

 INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (10, Ramesh, Sharma, Lucknow Aurangabad, Ghaziabad, 88, 95, A2); INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES ( 02, Yadu, Sharma, Aurangabad, Ghaziabad, Noida, 95, 82, A1 ); INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (07, Vijay, Ramna, Noida, Ghaziabad, Lucknow, 91, 95, A1); INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (04, Bhanu, Rangopalr, Ghaziabad, Noida, Lucknow, 85, 82, A2); INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (11, Harry, Roy, Noida, Kanpur, Ghaziabad, 95, 97, A1); INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (16, Akash, Ramna, Ghaziabad, Meerut, Aurangabad, 95, 90, B1); INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (19, Ram, Ramna, Lucknow, Ghaziabad, Aurangabad, 89, 95, A2); 

Sljedeća izjava SELECT prikazuje umetnute zapise od gore navedenog Employee_Grade stol:

 SELECT * FROM Employee_Grade; 

ID_zaposlenika Ime Prezime Prvi_Grad Drugi_Grad Novi_Grad Prisustvo_Primjedbe Rad_Primjedbe Razred
10 Ramesh Sharma Lucknow Aurangabad Ghaziabad 88 95 A2
02 Širenje Sharma Aurangabad Ghaziabad Ghaziabad 95 82 A1
07 Vijay Ramna Noida Ghaziabad Lucknow 91 95 A1
04 Bhanu Rangopalr Ghaziabad Noida Lucknow 85 82 A2
jedanaest Harry Roy Noida Kanpur Ghaziabad 95 97 A1
16 Akaša Ramna Ghaziabad Meerut Aurangabad 95 90 B1
19 Radna memorija Ramna Lucknow Ghaziabad Aurangabad 89 95 A2

Upit 1: Sljedeći SELECT upit koristi funkciju LENGTH sa stupcem First_Name gornje tablice Employee_Grade:

 SELECT First_Name, LENGTH(First_Name) AS LENGTH_FirstName FROM Employee_Grade; 

Ova izjava pokazuje duljinu imena svakog zaposlenika koji radi u industriji.

Ime LENGTH_Ime
Ramesh 6
Širenje 4
Vijay 5
Bhanu 5
Harry 5
Akaša 5
Radna memorija 3

Upit 2: Sljedeći SELECT upit koristi funkciju LENGTH sa stupcem Last_Name gornje tablice Employee_Grade:

 SELECT Last_Name, LENGTH(Last_Name) AS LENGTH_LastName FROM Employee_Grade; 

Ova izjava pokazuje duljinu prezimena svakog zaposlenika.

java prekidač int

Izlaz:

Prezime LENGTH_Prezime
Sharma 6
Sharma 6
Ramna 5
Rangopalr 9
Roy 3
Ramna 5
Ramna 5

Upit 3: Sljedeći SELECT upit koristi funkciju LENGTH sa stupcima First_City i New_City gornje tablice Employee_Grade:

 SELECT First_City, LENGTH(First_City) AS LENGTH_FirstCity, New_City, LENGTH(New_City) AS LENGTH_NewCity FROM Employee_Grade; 

Ova SQL izjava pokazuje duljinu prvog i novog grada svakog zaposlenika.

Izlaz:

Prvi_Grad LENGTH_FirstCity Novi_Grad LENGTH_Novi grad
Lucknow 7 Ghaziabad 9
Aurangabad 10 Ghaziabad 9
Noida 5 Lucknow 7
Ghaziabad 9 Lucknow 7
Noida 5 Ghaziabad 9
Ghaziabad 9 Aurangabad 10
Lucknow 7 Aurangabad 10