Click here to Skip to main content
11,490,801 members (78,260 online)

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, embarrasing 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 4:41
memberGary Huck17-Jan-13 4:41 
GeneralRe: Happy new year... kind of... Pin
Dave Kreskowiak17-Jan-13 6:26
mvpDave Kreskowiak17-Jan-13 6:26 
GeneralRe: Happy new year... kind of... Pin
Jörgen Andersson17-Jan-13 10:04
memberJörgen Andersson17-Jan-13 10:04 
GeneralRe: Happy new year... kind of... Pin
peterchen28-Jan-13 23:05
memberpeterchen28-Jan-13 23:05 
GeneralRe: Happy new year... kind of... Pin
hoernchenmeister28-Jan-13 23:49
memberhoernchenmeister28-Jan-13 23:49 
JokeRe: Happy new year... kind of... Pin
VUnreal8-Feb-13 11:27
memberVUnreal8-Feb-13 11:27 
GeneralJust got an error from VS2012 Pin
Brisingr Aerowing15-Jan-13 9:48
memberBrisingr Aerowing15-Jan-13 9:48 
GeneralGetting it wrong badly Pin
PIEBALDconsult9-Jan-13 15:12
memberPIEBALDconsult9-Jan-13 15: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 7:31
memberSentenryu10-Jan-13 7:31 
GeneralRe: Getting it wrong badly Pin
PIEBALDconsult10-Jan-13 8:42
memberPIEBALDconsult10-Jan-13 8:42 
GeneralRe: Getting it wrong badly Pin
Sentenryu10-Jan-13 23:31
memberSentenryu10-Jan-13 23:31 
GeneralRe: Getting it wrong badly Pin
AspDotNetDev10-Jan-13 8:37
protectorAspDotNetDev10-Jan-13 8:37 
GeneralRe: Getting it wrong badly Pin
PIEBALDconsult10-Jan-13 8:52
memberPIEBALDconsult10-Jan-13 8:52 
GeneralRe: Getting it wrong badly Pin
AspDotNetDev10-Jan-13 9:11
protectorAspDotNetDev10-Jan-13 9:11 
GeneralFacepalm Moment Pin
Zac Greve7-Jan-13 9:47
memberZac Greve7-Jan-13 9:47 
GeneralRe: Facepalm Moment Pin
PJ Arends7-Jan-13 10:03
memberPJ Arends7-Jan-13 10:03 
GeneralRe: Facepalm Moment Pin
Zac Greve7-Jan-13 10:06
memberZac Greve7-Jan-13 10:06 
GeneralRe: Facepalm Moment Pin
peterchen7-Jan-13 11:03
memberpeterchen7-Jan-13 11:03 
GeneralRe: Facepalm Moment Pin
Zac Greve7-Jan-13 11:12
memberZac Greve7-Jan-13 11:12 
GeneralRe: Facepalm Moment Pin
Dave Kreskowiak7-Jan-13 10:15
mvpDave Kreskowiak7-Jan-13 10:15 
GeneralRe: Facepalm Moment Pin
Zac Greve7-Jan-13 10:30
memberZac Greve7-Jan-13 10:30 
GeneralRe: Facepalm Moment Pin
BobJanova8-Jan-13 0:29
memberBobJanova8-Jan-13 0:29 
GeneralRe: Facepalm Moment Pin
PIEBALDconsult7-Jan-13 10:17
memberPIEBALDconsult7-Jan-13 10:17 
GeneralRe: Facepalm Moment Pin
lewax007-Jan-13 11:52
memberlewax007-Jan-13 11:52 
GeneralRe: Facepalm Moment Pin
Jörgen Andersson7-Jan-13 11:05
memberJörgen Andersson7-Jan-13 11:05 

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

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


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 28 May 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid