Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

CountWords - SQL Server

RSS
Modified on Wed, Feb 20, 2013, 1:43 PM by Administrator Categorized as SQL Server
The following T-SQL user function counts the number of words in the input, making the assumption that each "word" starts with a capital letter (as in the case of a CamelCase variable or object name).

if OBJECT_ID('CountWords') is not null drop function dbo.CountWords
go
create function dbo.CountWords
    (
    @input nvarchar(1000)
    ) returns int as begin
    
declare @result int = 0

set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'a', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'b', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'c', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'd', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'e', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'f', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'g', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'h', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'i', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'j', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'k', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'l', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'm', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'n', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'o', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'p', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'q', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'r', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 's', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 't', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'u', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'v', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'w', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'x', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'y', '')
set @input = REPLACE(@input COLLATE SQL_Latin1_General_CP1_CS_AS, 'z', '')

return len(@input)
    
end

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.