Collections:
CHARINDEX() and SUBSTRING() - Locating and Taking Substrings in SQL Server
How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions in SQL Server Transact-SQL?
✍: FYIcenter.com
Transact-SQL is not a language designed for manipulating strings, but it does have two simple functions to locate and take substrings: CHARINDEX() and SUBSTRING().
The tutorial exercise below assumes two given strings: 'Pages: 18' and 'Words: 3240'. The objective is to calculate the number of words per page. Read the script below to see how this is done by using CHARINDEX() and SUBSTRING() functions:
DECLARE @sPages VARCHAR(40), @sWords VARCHAR(40); SET @sPages = 'Pages: 18'; SET @sWords = 'Words: 3240'; SET @sPages = SUBSTRING(@sPages, CHARINDEX(':', @sPages)+1, 20); SET @sWords = SUBSTRING(@sWords, CHARINDEX(':', @sWords)+1, 20); PRINT 'Number of words per page: ' + CONVERT(VARCHAR(20), CONVERT(INT, @sWords)/CONVERT(INT, @sPages)); GO Number of words per page: 180
If you are a PHP developer, you can get this done in a much quick way.
⇒ Concatenating Two Binary Strings in SQL Server
⇐ Inserting New Line Characters into Strings in SQL Server
⇑ Character Strings and Binary Strings in SQL Server Transact-SQL
2017-03-07, 3374🔥, 0💬
Popular Posts:
How To Verify Your PHP Installation in MySQL? PHP provides two execution interfaces: Command Line In...
What Is ISAM in MySQL? ISAM (Indexed Sequential Access Method) was developed by IBM to store and ret...
How To Run SQL Commands in SQL*Plus in Oracle? If you want to run a SQL command in SQL*Plus, you nee...
How to put statements into a statement block in SQL Server Transact-SQL? You can put statements into...
How To Get a List of All Tables with "sys.tables" View in SQL Server? If you want to see the table y...