Home Forums SQL Server 2008 T-SQL (SS2K8) tsql query - Count the number of spaces in a string RE: tsql query - Count the number of spaces in a string

  • Like this:

    CREATE FUNCTION [dbo].[ifn_NumOccurrences]

    (

    @sourceString varchar(1000),

    @searchString varchar(10)

    )

    RETURNS TABLE

    AS

    return

    SELECT numTimes = (DATALENGTH(@sourceString) - DATALENGTH(REPLACE(@sourceString COLLATE Latin1_General_BIN2, @searchString, ''))) / DATALENGTH(@searchString);

    GO