TSQL - Solve it YOUR Way - Text to Speech
As part of the blog series TSQL - Solve it YOUR Way, I will present a topic recently discussed in the Transact-SQL MSDN forum here, followed by three different solutions to this problem supplied by three of the most active and helpful contributors in the forums, Tom Cooper, Naomi Nosonovsky, and Kent Waldrop. More importantly, they have included thoughts as to how they arrived at each solution. By seeing the different solutions along with a glimpse into their thought process, you can compare the different approaches to the solutions and learn from each of them.
Topic: I am writing a Text to Speech application and want to parse integer values into digits and convert them into the text representation of each digit. Example: Convert 16498 into the string "one six four nine eight".
Solution #1: Provided by Tom Cooper
- -- Test Data
- Declare @Test Table(InputString varchar(10));
- Insert @Test(InputString) Values ('164285'), ('1746');
- -- Solution
- SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
- InputString, '0', 'Zero '), '1', 'One '), '2', 'Two ')
- , '3', 'Three '), '4', 'Four '), '5', 'Five ')
- , '6', 'Six '), '7', 'Seven '), '8', 'Eight ')
- , '9', 'Nine ')
- FROM @Test;
The reason that I picked this solution is it is simple and easy to understand. That means it is easy to maintain. Maintainability is a very important consideration when developing software. So, for example, let's consider sometime in the future that this problem is extended to include the requirement that the number may have embedded commas, and/or the number may be preceded by a minus sign (-). If the requirement is to remove the comma’s and change the minus sign to the word “Minus”, then you could give this to a junior programmer who could easily understand this code and make the updates. Note, I have changed this slightly from my original answer. I have made the second argument to each REPLACE function call a string constant, not an integer constant. Either way works, but, of course, since REPLACE wants a string there, it is better to give it as a string rather than relying on the implicit conversion that will take place.
Solution #2: Provided by Naomi Nosonsovsky
DECLARE @NumToText TABLE (
-- Create lookup table
INSERT INTO @NumToText
-- Test values
DECLARE @Value VARCHAR(100) = '125790784533'
SELECT SUBSTRING(@Value, N.Number, 1) AS [NumberToParse]
FROM master..spt_values N where N.type = 'P'
AND N.number BETWEEN 1
FROM @NumToText T
INNER JOIN cte ON T.Number = cte.NumberToParse
SELECT @Output = LTRIM((
SELECT ' ' + Word
ORDER BY number
FOR XML PATH('')
SELECT @Output AS Result
I first created a table that contains the mapping/conversion (ex: 1:one, 2:two, etc). Then using the numbers table, I parse the original value into individual digits using common table expressions (CTE's), join that with the conversion/lookup table, and then finally concatenate back using the XML PATH approach in the final @Output parameter.
Solution #3 - Provided by Kent Waldrop
- declare @test table(some_String varchar(10) );
- insert into @test
- select '164285' union all select '0987654321'
- /* ------------------------------------------------
- This query gets the name of a string of digits.
- (1) "name_List" provides a list of digit names as a string that is divided into 6-char segments -- a six-char segment
- that names each decimal digit.
- (2) "digits" fetches each individual digit of the input string.
- (3) "set_Names" fetches the name of each specific digit
- (4) the SELECT clause concatenates together the names obtained from "set_Names" to form the output string.
- ------------------------------------------------ */
- coalesce(seg_1, '')
- + coalesce(seg_2, '')
- + coalesce(seg_3, '')
- + coalesce(seg_4, '')
- + coalesce(seg_5, '')
- + coalesce(seg_6, '')
- + coalesce(seg_7, '')
- + coalesce(seg_8, '')
- + coalesce(seg_9, '')
- + coalesce(seg_10, '')
- from @test
- cross apply
- ( select'zero one two three four five six seven eight nine '
- as segments
- ) as name_List
- cross apply
- ( select
- nullif(substring(some_String, 1, 1),'') as digit_1,
- nullif(substring(some_String, 2, 1),'') as digit_2,
- nullif(substring(some_String, 3, 1),'') as digit_3,
- nullif(substring(some_String, 4, 1),'') as digit_4,
- nullif(substring(some_String, 5, 1),'') as digit_5,
- nullif(substring(some_String, 6, 1),'') as digit_6,
- nullif(substring(some_String, 7, 1),'') as digit_7,
- nullif(substring(some_String, 8, 1),'') as digit_8,
- nullif(substring(some_String, 9, 1),'') as digit_9,
- nullif(substring(some_String,10, 1),'') as digit_10
- ) as digits
- cross apply
- ( select
- rtrim(substring(segments, 6*cast(digit_1 as int) + 1, 6)) + ' ' as seg_1,
- rtrim(substring(segments, 6*cast(digit_2 as int) + 1, 6)) + ' ' as seg_2,
- rtrim(substring(segments, 6*cast(digit_3 as int) + 1, 6)) + ' ' as seg_3,
- rtrim(substring(segments, 6*cast(digit_4 as int) + 1, 6)) + ' ' as seg_4,
- rtrim(substring(segments, 6*cast(digit_5 as int) + 1, 6)) + ' ' as seg_5,
- rtrim(substring(segments, 6*cast(digit_6 as int) + 1, 6)) + ' ' as seg_6,
- rtrim(substring(segments, 6*cast(digit_7 as int) + 1, 6)) + ' ' as seg_7,
- rtrim(substring(segments, 6*cast(digit_8 as int) + 1, 6)) + ' ' as seg_8,
- rtrim(substring(segments, 6*cast(digit_9 as int) + 1, 6)) + ' ' as seg_9,
- rtrim(substring(segments, 6*cast(digit_10 as int) + 1, 6)) + ' ' as seg_10
- ) as set_Names;
I used the APPLY operators because it is a method of clarifying the code with which I am comfortable. Frequently, I will initially build a query in-line and then push complexity down into APPLY operators. This is a design alternative to use of CTEs or derived tables. However, if I am working with either Oracle or DB2 I tend toward the CTEs because the APPLY operator does not exist for either of these dialects. Notice how well the CROSS APPLY partitions the calculations into more easily understandable portions. Of course the same thing could be done with CTEs or derived tables.
There are a number of bookend operations that can be used for conditional processing. The most widely used combination is the CASE structure. Other potentials bookends include the use of NULLIF and COALESCE, the SIGN function and the IIF function if you using SQL Server 2012. In this case I chose to use the NULLIF and COALESCE bookends for conditional processing – but why?
Look again at the structure of the code. Notice that the COALESCE function is in the SELECT clause and that the NULLIF is in one of the CROSS APPLY clauses. By using these two operations to bookend my conditional code I can divide my code across a big boundary like this in order to reveal each distinct portion of the code.
As you can see, all three of the above solutions provide the result we were looking for, but do so in a very different style. The original thread provides variations of the solutions presented here as well as one additional solution using a CASE statement. Each of these solutions leverages different SQL Server language constructs and includes different considerations in the final solutions. I hope that you are able to learn a lot by trying out the problem yourself and then reading through the additional solutions.
Special thanks to Tom, Naomi, and Kent for their valuable forums contribution and for contributing to this series!
Hope that helps,
Sam Lester (MSFT)
Tom Cooper began his programming career in 1968, began working with database software in 1977, and first worked with Microsoft SQL Server in 1994 (version 4.21). He is now very happily retired.
Naomi Nosonovsky, Senior Software Developer, has more than 15 years of enterprise experience in analysis, design, development and implementation of high-performance client/server data management solutions. She is a Personality of the Year for 2008, 2009, 2010 and 2011 at www.universalthread.com in .NET, SQL Server & VFP categories. She is also an All Star contributor/MCC Community Contributor at forums.asp.net and MSDN T-SQL forum. She also actively blogs at http://blogs.lessthandot.com/index.php/All/?disp=authdir&author=218 and http://beyondrelational.com/members/naomi/modules/2/posts.aspx?Tab=16.
Kent Waldrop started working with Sybase Transact SQL in 1989 as an application developer and continued working with Sybase until 1995 when SQL Server 6 came out. At that time, he became a Microsoft SQL Server database administrator and has continued to work with Microsoft SQL Server ever since. Currently he is a database architect working with Microsoft SQL Server, Oracle and UDB/DB2.