Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Two scripts to spell out numbers in words

By Anthony Zackin,

This script defines two user functions which will "translate" a number into its corresponding English words. The scripts can be used to generate unique character test data or for generating output suitable for speech synthesis.

The first, fnSpellInteger takes an integer argument and returns a VARCHAR(100). For example,

  • fnSpellInteger(10129) ==> "Ten Thousand One Hundred Twenty-Nine"

The second function, fnSpellNumber, is more general and takes a number with an optional decimal value as a VARCHAR(20) string. For example,

  • fnSpellNumber('15.99') ==> "Fifteen and Ninety-Nine Hundredths"

Test queries:

SELECT 0,MASTER.DBO.fnSpellInteger(0)
SELECT 9,MASTER.DBO.fnSpellInteger(9)
SELECT 19,MASTER.DBO.fnSpellInteger(19)
SELECT 29,MASTER.DBO.fnSpellInteger(29)
SELECT 129,MASTER.DBO.fnSpellInteger(129)
SELECT 1929,MASTER.DBO.fnSpellInteger(1929)
SELECT 10029,MASTER.DBO.fnSpellInteger(10029)
SELECT 10129,MASTER.DBO.fnSpellInteger(10129)
SELECT 192129,MASTER.DBO.fnSpellInteger(192129)
SELECT 1092129,MASTER.DBO.fnSpellInteger(1092129)
SELECT 1092.129,MASTER.DBO.fnSpellInteger(1092.129) -- Use fnSpellNumber for this
SELECT ' ', master.dbo.fnSpellNumber(' ')
SELECT 'a', master.dbo.fnSpellNumber('a')
SELECT '1a', master.dbo.fnSpellNumber('1a')
SELECT 'a1', master.dbo.fnSpellNumber('a1')
SELECT '1. ', master.dbo.fnSpellNumber('1. ')
SELECT '1..', master.dbo.fnSpellNumber('1..')
SELECT '1 .', master.dbo.fnSpellNumber('1 .')
SELECT '. 1', master.dbo.fnSpellNumber('. 1')
SELECT '1. 1', master.dbo.fnSpellNumber('1. 1')
SELECT '1.1.1', master.dbo.fnSpellNumber('1.1.1')
SELECT '.1A', master.dbo.fnSpellNumber('.1A')
SELECT '0.A1', master.dbo.fnSpellNumber('0.A1')
SELECT '0.1', master.dbo.fnSpellNumber('0.1')
SELECT '0.9', master.dbo.fnSpellNumber('0.9')
SELECT '0 ', master.dbo.fnSpellNumber('0 ')
SELECT ' 9 ', master.dbo.fnSpellNumber(' 9 ')
SELECT '92093', master.dbo.fnSpellNumber('92093')
SELECT '920093', master.dbo.fnSpellNumber('920093')
SELECT '15.002', master.dbo.fnSpellNumber('15.002')
SELECT '15.99', master.dbo.fnSpellNumber('15.99')
SELECT '.121', master.dbo.fnSpellNumber('.121')
SELECT '.120', master.dbo.fnSpellNumber('.120')
SELECT '15.9901', master.dbo.fnSpellNumber('15.9901')
SELECT '15.99001', master.dbo.fnSpellNumber('15.99001')
SELECT '15.990001', master.dbo.fnSpellNumber('15.990001')
SELECT '15.9900001', master.dbo.fnSpellNumber('15.9900001')
SELECT '99.99000001', master.dbo.fnSpellNumber('99.99000001')
SELECT '9900001.0', master.dbo.fnSpellNumber('9900001.0')
SELECT '99000001.99000001', master.dbo.fnSpellNumber('99000001.99000001')
SELECT '00110.2000', master.dbo.fnSpellNumber('00110.2000')
SELECT '00110', master.dbo.fnSpellNumber('00110')

Total article views: 1528 | Views in the last 30 days: 3
 
Related Articles
FORUM

Select

Select particular table

FORUM

select statements

select statements

FORUM

select query

select query

FORUM

select count

select count

FORUM

Select query

Select query

Tags
function    
number to words    
spell    
 
Contribute