loading..
Ðóññêèé    English
16:42

Recursive CTE page 2

Let's solve another challenge as the answer to the question that I have held numerous meetings on the Internet landscape.

Convert text in a table column in such a way that each word begins with an uppercase letter.

Here's an example of data and the desired result:

name     rep       
delta     Delta       
KSI PSI     Ksi Psi       
alfa beta gamma    zeta     Alfa Beta Gamma    Zeta       
ALfa and omegA     Alfa And Omega    

For a few exceptions (among which may be mentioned abbreviations and initials) can be assumed that the word inside the text is preceded by a space. This can be used as a search criterion we need the elements of the text. I propose to implement a following trivial algorithm:

1. The first letter of the text makes a capital, and the rest - lower.

2. Then each "space + letter" combination translates to upper case.

From the first paragraph of the algorithm's simple:

Console
Execute
  1. SELECT name, UPPER(LEFT(name, 1)) +
  2. LOWER(SUBSTRING(name, 2, LEN(name) - 1)) rep
  3. FROM
  4. (SELECT 'ALfa and omegA' AS name
  5. UNION ALL SELECT 'alfa beta gamma    zeta'
  6. UNION ALL SELECT 'KSI PSI'
  7. UNION ALL SELECT 'delta'
  8. ) X;

name     rep       
ALfa and omegA     Alfa and omega       
alfa beta gamma    zeta     Alfa beta gamma    zeta       
KSI PSI     Ksi psi       
delta     Delta    

To implement the second paragraph, there are some ways. Since the Latin alphabet is not so much (26), we can just make 26 substitutions. I was not lazy and will bring the full query so you can experiment.

So,

Console
Execute
  1. SELECT name, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  2. REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  3. REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  4. REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  5. REPLACE(REPLACE(rep, ' a', ' A'), ' b', ' B'), ' c', ' C'), ' d', ' D'),
  6. ' e', ' E'), ' f', ' F'), ' g', ' G'), ' h', ' H'), ' i', ' I'), ' j', ' J'), ' k', ' K'),
  7. ' l', ' L'), ' m', ' M'), ' n',' N'), ' o', ' O'), ' p', ' P'), ' q', ' Q'), ' r', ' R'),
  8. ' s', ' S'), ' t', ' T'), ' u', ' U'), ' v', ' V'), ' w', ' W'),
  9. ' x', ' X'), ' y', ' Y'), ' z', ' Z')
  10. FROM(
  11. SELECT name, UPPER(LEFT(name,1)) + LOWER(SUBSTRING(name, 2, LEN(name)-1)) rep
  12. FROM
  13. (SELECT 'ALfa and omegA' AS name
  14. UNION ALL SELECT 'alfa beta gamma    zeta'
  15. UNION ALL SELECT 'KSI PSI'
  16. UNION ALL SELECT 'delta'
  17. ) X
  18. ) Y;

It is easy to guess that the next query will use a recursive CTE.

Bookmark and Share
Pages 1 2 3
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
https://exchangesumo.com/obmen/NIXBTC-AKBBRUB/
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.