RSS
 

Posts Tagged ‘sql function’

SQL Format Name Function

10 Dec

I was working for an ASP (application service provider) awhile back and got sick of formatting our members names in code at the time of rendering the page.

I asked our DBA for advice if he thought it would be better performance to simply do this in the SQL calls to the database, of course his answer was yes, however what he suggested was to simply concatenate the name parts into an alias field.

Needless to say, it ended up bombing the page out around 1000 records.

After searching online for a few hours I came across a SQL Function that does exactly what was needed, and WHAMMMOOO!!!   No more page bombing.

Here it is, with usage to share with all (because it took so long to find it)

  1. SET ANSI_NULLS ON
  2. SET QUOTED_IDENTIFIER ON
  3. GO
  4. ALTER FUNCTION [dbo].[FormatName](@NameString varchar(100), @NameFormat varchar(20))
  5. returns varchar(100) AS
  6. begin
  7. –FormatName parses a NameString into its component parts and returns it in a requested format.
  8. –@NameString is the raw value to be parsed.
  9. –@NameFormat is a string that defines the output format.  Each letter in the string represents
  10. –a component of the name in the order that it is to be returned.
  11. –    [H] = Full honorific
  12. –    [h] = Abbreviated honorific
  13. –    [F] = First name
  14. –    [f] = First initial
  15. –    [M] = Middle name
  16. –    [m] = Middle initial
  17. –    [L] = Last name
  18. –    [l] = Last initial
  19. –    [S] = Full suffix
  20. –    [s] = Abbreviated suffix
  21. –    [.] = Period
  22. –    [,] = Comma
  23. –    [ ] = Space
  24.  
  25. –Example: select dbo.Formatname(‘Reverend Gregory Robert Von Finzer Junior’, ‘L, h. F m. s.’)
  26. –Result: ‘Von Finzer, Rev. Gregory R. Jr.’
  27.  
  28. –Test variables
  29. – declare    @NameString varchar(50)
  30. – declare    @NameFormat varchar(20)
  31. – set    @NameFormat = ‘L, h. F m. s.’
  32. – set    @NameString = ‘Reverend Gregory Robert Von Finzer Junior’
  33.  
  34. Declare    @Honorific varchar(20)
  35. Declare    @FirstName varchar(20)
  36. Declare    @MiddleName varchar(30)
  37. Declare    @LastName varchar(30)
  38. Declare    @Suffix varchar(20)
  39. Declare    @TempString varchar(100)
  40. Declare    @IgnorePeriod char(1)
  41.  
  42. –Prepare the string
  43. –Make sure each period is followed by a space character.
  44. SET    @NameString = rtrim(ltrim(REPLACE(@NameString, ‘.’, ‘. ‘)))
  45. –Eliminate double-spaces.
  46. while  charindex(‘  ‘, @NameString) > 0 SET @NameString = REPLACE(@NameString, ‘  ‘, ‘ ‘)
  47. –Eliminate periods
  48. while  charindex(‘.’, @NameString) > 0 SET @NameString = REPLACE(@NameString, ‘.’, )
  49.  
  50. –If the lastname is listed first, strip it off.
  51. SET    @TempString = rtrim(LEFT(@NameString, charindex(‘ ‘, @NameString)))
  52. IF    @TempString IN (‘VAN’, ‘VON’, ‘MC’, ‘Mac’, ‘DE’) SET @TempString = rtrim(LEFT(@NameString, charindex(‘ ‘, @NameString, len(@TempString)+2)))
  53. IF    RIGHT(@TempString, 1) = ‘,’ SET @LastName = LEFT(@TempString, len(@TempString)-1)
  54. IF    len(@LastName) > 0 SET    @NameString = ltrim(RIGHT(@NameString, len(@NameString) - len(@TempString)))
  55.  
  56. –Get rid of any remaining commas
  57. while  charindex(‘,’, @NameString) > 0 SET @NameString = REPLACE(@NameString, ‘,’, )
  58.  
  59. –Get Honorific and strip it out of the string
  60. SET    @TempString = rtrim(LEFT(@NameString, charindex(‘ ‘, @NameString + ‘ ‘)))
  61. IF    @TempString IN (‘MR’, ‘MRS’, ‘MS’, ‘DR’, ‘Doctor’, ‘REV’, ‘Reverend’, ‘SIR’, ‘HON’, ‘Honorable’, ‘CPL’, ‘Corporal’, ‘SGT’, ‘Sergeant’, ‘GEN’, ‘General’, ‘CMD’, ‘Commander’,  ‘CPT’, ‘CAPT’, ‘Captain’, ‘MAJ’, ‘Major’, ‘PVT’, ‘Private’, ‘LT’, ‘Lieutenant’, ‘FATHER’, ‘SISTER’) SET @Honorific = @TempString
  62. IF    len(@Honorific) > 0 SET    @NameString = ltrim(RIGHT(@NameString, len(@NameString) - len(@TempString)))
  63.  
  64. –Get Suffix and strip it out of the string
  65. SET    @TempString = ltrim(RIGHT(@NameString, charindex(‘ ‘, Reverse(@NameString) + ‘ ‘)))
  66. IF    @TempString IN (‘Jr’, ‘Sr’,  ‘II’, ‘III’, ‘Esq’, ‘Junior’, ‘Senior’) SET @Suffix = @TempString
  67. IF    len(@Suffix) > 0 SET @NameString = rtrim(LEFT(@NameString, len(@NameString) - len(@TempString)))
  68.  
  69. IF @LastName IS NULL
  70. begin
  71. –Get LastName and strip it out of the string
  72. SET    @LastName = ltrim(RIGHT(@NameString, charindex(‘ ‘, Reverse(@NameString) + ‘ ‘)))
  73. SET    @NameString = rtrim(LEFT(@NameString, len(@NameString) - len(@LastName)))
  74. –Check to see if the last name has two parts
  75. SET    @TempString = ltrim(RIGHT(@NameString, charindex(‘ ‘, Reverse(@NameString) + ‘ ‘)))
  76. IF    @TempString IN (‘VAN’, ‘VON’, ‘MC’, ‘Mac’, ‘DE’)
  77. begin
  78. SET @LastName = @TempString + ‘ ‘ + @LastName
  79. SET @NameString = rtrim(LEFT(@NameString, len(@NameString) - len(@TempString)))
  80. end
  81. end
  82.  
  83. –Get FirstName and strip it out of the string
  84. SET    @FirstName = rtrim(LEFT(@NameString, charindex(‘ ‘, @NameString + ‘ ‘)))
  85. SET    @NameString = ltrim(RIGHT(@NameString, len(@NameString) - len(@FirstName)))
  86.  
  87. –Anything remaining is MiddleName
  88. SET    @MiddleName = @NameString
  89.  
  90. –Create the output string
  91. SET    @TempString =
  92. while len(@NameFormat) > 0
  93. begin
  94. IF @IgnorePeriod = ‘F’ OR LEFT(@NameFormat, 1) <> ‘.’
  95. begin
  96. SET @IgnorePeriod = ‘F’
  97. SET @TempString = @TempString +
  98. case ascii(LEFT(@NameFormat, 1))
  99. when ’72′ then case @Honorific
  100. when ‘Dr’ then ‘Doctor’
  101. when ‘Rev’ then ‘Reverend’
  102. when ‘Hon’ then ‘Honorable’
  103. when ‘Maj’ then ‘Major’
  104. when ‘Pvt’ then ‘Private’
  105. when ‘Lt’ then ‘Lieutenant’
  106. when ‘Capt’ then ‘Captain’
  107. when ‘Cpt’ then ‘Captain’
  108. when ‘Cmd’ then ‘Commander’
  109. when ‘Gen’ then ‘General’
  110. when ‘Sgt’ then ‘Sergeant’
  111. when ‘Cpl’ then ‘Corporal’
  112. else isnull(@Honorific, )
  113. end
  114. when ’70′ then isnull(@FirstName, )
  115. when ’77′ then isnull(@MiddleName, )
  116. when ’76′ then isnull(@LastName, )
  117. when ’83′ then case @Suffix
  118. when ‘Jr’ then ‘Junior’
  119. when ‘Sr’ then ‘Senior’
  120. when ‘Esq’ then ‘Esquire’
  121. else isnull(@Suffix, )
  122. end
  123. when ’104′ then case @Honorific
  124. when ‘Doctor’ then ‘Dr’
  125. when ‘Reverend’ then ‘Rev’
  126. when ‘Honorable’ then ‘Hon’
  127. when ‘Major’ then ‘Maj’
  128. when ‘Private’ then ‘Pvt’
  129. when ‘Lieutenant’ then ‘Lt’
  130. when ‘Captain’ then ‘Capt’
  131. when ‘Cpt’ then ‘Capt’
  132. when ‘Commander’ then ‘Cmd’
  133. when ‘General’ then ‘Gen’
  134. when ‘Sergeant’ then ‘Sgt’
  135. when ‘Corporal’ then ‘Cpl’
  136. else isnull(@Honorific, )
  137. end
  138. when ’102′ then isnull(LEFT(@FirstName, 1), )
  139. when ’109′ then isnull(LEFT(@MiddleName, 1), )
  140. when ’108′ then isnull(LEFT(@LastName, 1), )
  141. when ’115′ then case @Suffix
  142. when ‘Junior’ then ‘Jr’
  143. when ‘Senior’ then ‘Sr’
  144. when ‘Esquire’ then ‘Esq’
  145. else isnull(@Suffix, )
  146. end
  147. when ’46′ then case RIGHT(@TempString, 1)
  148. when ‘ ‘ then
  149. else ‘.’
  150. end
  151. when ’44′ then case RIGHT(@TempString, 1)
  152. when ‘ ‘ then
  153. else ‘,’
  154. end
  155. when ’32′ then case RIGHT(@TempString, 1)
  156. when ‘ ‘ then
  157. else ‘ ‘
  158. end
  159. else
  160. end
  161. IF ((ascii(LEFT(@NameFormat, 1)) = 72 AND @Honorific IN (‘FATHER’, ‘SISTER’))
  162. OR (ascii(LEFT(@NameFormat, 1)) = 115 AND @Suffix IN (‘II’, ‘III’)))
  163. SET @IgnorePeriod = ‘T’
  164. end
  165. SET @NameFormat = RIGHT(@NameFormat, len(@NameFormat) - 1)
  166. end
  167.  
  168. RETURN @TempString
  169. end
 
 
 

Optimized by SEO Ultimate