Pages

Wednesday, 30 March 2011

Underscore character (_) in PL/SQL.

The underscore character is a special wildcard character or we can say it is a special SQL pattern which used to represent a single character in a string. This pattern match to a single character and should be escaped if you are in fact looking for an underscore character in the LIKE clause of a query. It can be used in any position and any number of times within the string. Just add the following after a LIKE statement: '_ABC' Underscore character ( _ ) is a very clear separator, it is used to name your variables. That gives you lots of room to come up with unambiguous names. Example:


SQL> -- create demo table
SQL> create table Student(
2    ID                        VARCHAR2(4 BYTE)         NOT NULL,
3    First_Name           VARCHAR2(10 BYTE),
4    Last_Name           VARCHAR2(10 BYTE),
5    DOB                     DATE,
6    Fees                     Number(8,2),
7    City                      VARCHAR2(10 BYTE),
8    Field                     VARCHAR2(15 BYTE)
9  )
10  / Table created. SQL>
SQL> -- prepare data
SQL> insert into Student(ID,  First_Name, Last_Name, DOB,                     End_Date,                       Fees,  City,       Field)
2               values ('01','Jason',    'Martin',  to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto',  'Programmer')
3  / 1 row created. SQL> insert into Student(ID,  First_Name, Last_Name, DOB,                     End_Date,                       Fees,  City,       Field)
2                values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
3  / 1 row created. SQL> insert into Student(ID,  First_Name, Last_Name, DOB,                     End_Date,                       Fees,  City,       Field)
2                values('03','James',    'Smith',   to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')
3  / 1 row created. SQL> insert into Student(ID,  First_Name, Last_Name, DOB,                     End_Date,                       Fees,  City,       Field)
2                values('04','Celia',    'Rice',    to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')
3  / 1 row created. SQL> insert into Student(ID,  First_Name, Last_Name, DOB,                     End_Date,                       Fees,  City,       Field)
2                values('05','Robert',   'Black',   to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')
3  / 1 row created. SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Student
2  /
ID   FIRST_NAME  LAST_NAME       DOB         Fees      CITY                 Field
---- --------------   --------------  -----------   ---------  --------             ----------
01    Harsh               Gupta        25-JUL-06    1234.56   Toronto        Programmer
02    Joliy                 Pera          21-FEB-86    6661.78   Vancouver    Tester
03    Cutter              Selmon     15-MAR-90    6544.78   Vancouver   Tester
04    Brad                 tom          21-APR-99    2344.78   Vancouver    Manager
05    Noman             Javk          08-AUG-98   2334.78   Vancouver    Tester 5 rows selected. SQL>
SQL>
SQL> SELECT * FROM Student WHERE first_name LIKE '_arsh ';
ID   FIRST_NAME   LAST_NAME     DOB           Fees        CITY          Field
---- ----------         --------------  ------------   ---------    --------      ----------
01    Harsh               Gupta         25-JUL-06    1234.56   Toronto       Programmer
1 rows selected.

0 comments:

Post a Comment

 
Powered by Blogger