Click here to Skip to main content
15,891,513 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Happy new year... kind of... Pin
Gary Huck17-Jan-13 3:41
Gary Huck17-Jan-13 3:41 
GeneralRe: Happy new year... kind of... Pin
Dave Kreskowiak17-Jan-13 5:26
mveDave Kreskowiak17-Jan-13 5:26 
GeneralRe: Happy new year... kind of... Pin
Jörgen Andersson17-Jan-13 9:04
professionalJörgen Andersson17-Jan-13 9:04 
GeneralRe: Happy new year... kind of... Pin
peterchen28-Jan-13 22:05
peterchen28-Jan-13 22:05 
GeneralRe: Happy new year... kind of... Pin
hoernchenmeister28-Jan-13 22:49
hoernchenmeister28-Jan-13 22:49 
JokeRe: Happy new year... kind of... Pin
Vladimir Svyatski8-Feb-13 10:27
professionalVladimir Svyatski8-Feb-13 10:27 
GeneralJust got an error from VS2012 Pin
Brisingr Aerowing15-Jan-13 8:48
professionalBrisingr Aerowing15-Jan-13 8:48 
GeneralGetting it wrong badly Pin
PIEBALDconsult9-Jan-13 14:12
mvePIEBALDconsult9-Jan-13 14:12 
<context>SQL Server 2008 R2</context>

Someone added a function to my database! Well OK, it's a team effort so that's fine, but it's incorrect and not well-written.

What I think the purpose is is to take a column name in PascalCase and make it look more presentable by adding a SPACE before each capital (uppercase) letter. That's what I understand from the name and it's a reasonable thing to do and it does that.
Buuut... what it really does is add a SPACE before each non-lowercase letter (e.g. digits, symbols, whitespace). D'Oh! | :doh:

Aside from producing unexpected output it's also hard to read and difficult to maintain -- I opted to rewrite it rather than try to fix it. Here's the original:

CREATE FUNCTION [dbo].[SpaceBeforeCap]
(
 @str nvarchar(max)
)
returns nvarchar(max)
as
begin

declare @i int, @j int
declare @returnval nvarchar(max)
set @returnval = ''
select @i = 1, @j = len(@str)

declare @w nvarchar(max)

while @i <= @j
begin
 if substring(@str,@i,1) = UPPER(substring(@str,@i,1)) collate Latin1_General_CS_AS
 begin
  if @w is not null
  set @returnval = @returnval + ' ' + @w
  set @w = substring(@str,@i,1)
 end
 else
  set @w = @w + substring(@str,@i,1)
 set @i = @i + 1
end
if @w is not null
 set @returnval = @returnval + ' ' + @w

return ltrim(@returnval)

end



And here's mine (I added the fn wart because that's the standard I inherited Sigh | :sigh: ):

CREATE FUNCTION [dbo].[fnSpaceBeforeCap]
( @str NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
  DECLARE @offset INTEGER
  SET @offset = PATINDEX ( '%[^ ][A-Z]%' , @str COLLATE Latin1_General_BIN )

  WHILE ( @offset > 0 )
  BEGIN
    SET @str = STUFF ( @str , @offset + 1 , 0 , ' ' )
    SET @offset = PATINDEX ( '%[^ ][A-Z]%' , @str COLLATE Latin1_General_BIN )
  END

  RETURN @str
END


It uses PATINDEX to find a non-SPACE followed by an uppercase letter then STUFFs a SPACE between them.
I'd like to say that mine is more efficient, but I don't feel like doing any testing. What I will point out is that the original uses SUBSTRING (twice!) to test each character and SUBSTRING to form the new value, whereas mine has that kind of thing hidden in black boxes. What I don't like about mine is that PATINDEX doesn't allow a parameter to tell it where to start so it always starts over from the beginning -- see also Schlemiel the painter's Algorithm[^].
GeneralRe: Getting it wrong badly Pin
Sentenryu10-Jan-13 6:31
Sentenryu10-Jan-13 6:31 
GeneralRe: Getting it wrong badly Pin
PIEBALDconsult10-Jan-13 7:42
mvePIEBALDconsult10-Jan-13 7:42 
GeneralRe: Getting it wrong badly Pin
Sentenryu10-Jan-13 22:31
Sentenryu10-Jan-13 22:31 
GeneralRe: Getting it wrong badly Pin
AspDotNetDev10-Jan-13 7:37
protectorAspDotNetDev10-Jan-13 7:37 
GeneralRe: Getting it wrong badly Pin
PIEBALDconsult10-Jan-13 7:52
mvePIEBALDconsult10-Jan-13 7:52 
GeneralRe: Getting it wrong badly Pin
AspDotNetDev10-Jan-13 8:11
protectorAspDotNetDev10-Jan-13 8:11 
GeneralFacepalm Moment Pin
Brisingr Aerowing7-Jan-13 8:47
professionalBrisingr Aerowing7-Jan-13 8:47 
GeneralRe: Facepalm Moment Pin
PJ Arends7-Jan-13 9:03
professionalPJ Arends7-Jan-13 9:03 
GeneralRe: Facepalm Moment Pin
Brisingr Aerowing7-Jan-13 9:06
professionalBrisingr Aerowing7-Jan-13 9:06 
GeneralRe: Facepalm Moment Pin
peterchen7-Jan-13 10:03
peterchen7-Jan-13 10:03 
GeneralRe: Facepalm Moment Pin
Brisingr Aerowing7-Jan-13 10:12
professionalBrisingr Aerowing7-Jan-13 10:12 
GeneralRe: Facepalm Moment Pin
Dave Kreskowiak7-Jan-13 9:15
mveDave Kreskowiak7-Jan-13 9:15 
GeneralRe: Facepalm Moment Pin
Brisingr Aerowing7-Jan-13 9:30
professionalBrisingr Aerowing7-Jan-13 9:30 
GeneralRe: Facepalm Moment Pin
BobJanova7-Jan-13 23:29
BobJanova7-Jan-13 23:29 
GeneralRe: Facepalm Moment Pin
PIEBALDconsult7-Jan-13 9:17
mvePIEBALDconsult7-Jan-13 9:17 
GeneralRe: Facepalm Moment Pin
lewax007-Jan-13 10:52
lewax007-Jan-13 10:52 
GeneralRe: Facepalm Moment Pin
Jörgen Andersson7-Jan-13 10:05
professionalJörgen Andersson7-Jan-13 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.