Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

SQL Substring – use of the Substring function

16 June 2020

SQL Substring - use of the Substring functionThe SQL SUBSTRING function cuts out and returns the specified number of characters from the string.

SQL-function SUBSTRING can be used in Delphi applications working with queries to local SQL, but it is not supported when working with InterBase (IB) and Local InterBase Server (LIBS) tables. Below is the syntax of the SUBSTRING function, examples of its use in local SQL queries, and an alternative to return the same results for IB/LIBS tables.

The first parameter the function accepts a field or a string, the second parameter – from which position to start cutting (the numbering of characters starts with 1), the third parameter – how many characters to take.

The third parameter is optional. If it is not specified, the text will be cut out from the specified position to the end of the line.

Syntax of the Substring function

First syntax:

SELECT SUBSTRING (field where to cut off, how many_symbols to take) FROM table name WHERE condition

Alternative syntax:

SELECT SUBSTRING(FROM field from where_Cut FOR how many_symbols_to take) FROM table name WHERE condition

SUBSTRING(<column> FROM <start> [, FOR <length>])

Where:

  • <column> – column name of the table, from which substring should be obtained.
  • <start> – place in the value of the column from which the substring is extracted.
  • <length> – length of substring to be extracted.

The third parameter is optional, in which case the text will be cut out from the specified position until the end of the line:

SELECT SUBSTRING FROM table name WHERE condition
SELECT SUBSTRING(FROM field from where_cut) FROM name_table WHERE condition

The SUBSTRING function in the example below will return the second, third and fourth characters from the column named COMPANY:

(COMPANY FROM 2 TO 3)

The SUBSTRING function can also be used for a list of fields in the SELECT query, where the WHERE keyword allows comparison of the value with a specific set of columns. The SUBSTRING function can only be used with String type columns (SQL type CHAR). Here is an example of SUBSTRING function using column list in SELECT query (use Paradox CUSTOMER.DB demo table):

SELECT (SUBSTRING(C. "COMPANY" FROM 1 TO 3)) AS SS
FROM "CUSTOMER.DB" C

This SQL query extracts the first three characters from the COMPANY column returned as a computed column with the name SS. Here is an example of the SUBSTRING function used in an SQL query with the WHERE keyword (we use the same table):

SELECT C. "COMPANY"
FROM "CUSTOMER.DB" C
WHERE SUBSTRING(C. "COMPANY" FROM 2 FOR 2) = "an"

This query will return all table rows where the second and third characters in the COMPANY column are equal to “ar”.

Since SUBSTRING function is not supported in IB and LIBS databases, substring operations with the list of columns in the query are impossible (exception: IB can work with substrings through user-defined functions, User-Defined Functions). But with the help of the LIKE operator and the associated symbolic wildcard markers it is also possible to work with the wildcard in the case of WHERE. Here is an example based on the EMPLOYEE table (in the EMPLOYEE.GDB database):

SELECT LAST_NAME, FIRST_NAME
FROM EMPLOYEE
WHERE LAST_NAME LIKE "_an%"

This SQL query will return all table rows where the second and third characters in the LAST_NAME column are equal to “an”, see the previous example based on the Paradox table. Databases IB and LIBS to perform a substring comparison in the query operator WHERE this method is necessary (and can not use the function SUBSTRING), while Paradox and dBASE tables (eg, local SQL) can use any method.

Examples

All examples will be in this table, unless otherwise stated:

id
text
1This is the first long text!
2This is the second long text!

Example №1
In this example, 6 characters are cut out of the string and returned starting from the 5th character:

SELECT id, SUBSTRING(text, 5, 6) as text FROM texts

The SQL query will select the following lines:

idtext
1first
2second

You can rewrite the request in the following form:

SELECT id, SUBSTRING(text FROM 5 FOR 6) as text FROM texts

Example №2
In this example, the entire line is returned to the end, starting with the fifth character:

SELECT id, SUBSTRING(text, 5) as text FROM texts

The SQL query will select the following lines:

idtext
1first long text!
2second long text!

You can rewrite the request in the following form:

SELECT id, SUBSTRING(text FROM 5) as text FROM texts

SQL SUBSTRING functions allows you to extract a substring from a string

 
Tags: , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...