Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234»»

ProperCase Function Expand / Collapse
Author
Message
Posted Friday, January 17, 2014 4:52 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:38 PM
Points: 1,816, Visits: 5,914
Jeff Moden (1/17/2014)
BWAAA-HAAA!!!! Dare I say IT? DARE I? Oh, what the heck...

"Ahhhhhhh MAGOO! You've done it again." (Sorry ol' friend... I've been holding it back for more than a month and couldn't resist).


If I am honest, I kind of missed it


MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1532338
    Posted Friday, January 17, 2014 5:22 PM


    Hall of Fame

    Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

    Group: General Forum Members
    Last Login: 2 days ago @ 9:53 PM
    Points: 3,438, Visits: 5,390
    I agree that you've done some very thorough testing here. Excellent work!

    Dohsan (1/17/2014)

    Dwain
    Ándre, Ándre, Luís And O'Brien Had A Low-Budget 3D Printer In Washington D.C.





    I wasn't sure how you wanted to handle "Low-Budget' and "3D" but either can be handled in the opposite manner by adjusting the match pattern.



    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
    Since random numbers are too important to be left to chance, let's generate some!
    Learn to understand recursive CTEs by example.
    Splitting strings based on patterns can be fast!
    Post #1532339
    Posted Friday, January 17, 2014 5:49 PM


    SSCarpal Tunnel

    SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

    Group: General Forum Members
    Last Login: Today @ 3:35 PM
    Points: 4,046, Visits: 9,202
    dwain.c (1/17/2014)
    I agree that you've done some very thorough testing here. Excellent work!

    Dohsan (1/17/2014)

    Dwain
    Ándre, Ándre, Luís And O'Brien Had A Low-Budget 3D Printer In Washington D.C.





    I wasn't sure how you wanted to handle "Low-Budget' and "3D" but either can be handled in the opposite manner by adjusting the match pattern.


    Don't make me start with contractions or possessives. You'Ll Say It'S My Fault.



    Luis C.
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    Forum Etiquette: How to post data/code on a forum to get the best help
    Post #1532342
    Posted Saturday, January 18, 2014 9:56 AM


    Old Hand

    Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

    Group: General Forum Members
    Last Login: Yesterday @ 4:15 AM
    Points: 356, Visits: 2,689
    Luis Cazares (1/17/2014)
    dwain.c (1/17/2014)
    I agree that you've done some very thorough testing here. Excellent work!

    Dohsan (1/17/2014)

    Dwain
    Ándre, Ándre, Luís And O'Brien Had A Low-Budget 3D Printer In Washington D.C.





    I wasn't sure how you wanted to handle "Low-Budget' and "3D" but either can be handled in the opposite manner by adjusting the match pattern.


    Don't make me start with contractions or possessives. You'Ll Say It'S My Fault.


    hah very true!
    Post #1532387
    Posted Sunday, January 19, 2014 5:42 AM
    Ten Centuries

    Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

    Group: General Forum Members
    Last Login: Today @ 2:07 AM
    Points: 1,037, Visits: 7,025
    This thread has been fun to watch, thanks folks! Here's an option I'm surprised nobody posted up:
    CREATE FUNCTION [dbo].[IF_ProperWithREPLACE]
    (
    @inputstring VARCHAR(8000)
    )
    RETURNS TABLE WITH SCHEMABINDING
    AS
    RETURN
    (
    SELECT ProperisedString =
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    ' '+@inputstring COLLATE Latin1_General_BIN,
    ' a',' A'), ' b',' B'), ' c',' C'), ' d',' D'), ' e',' E'), ' f',' F'), ' g',' G'), ' h',' H'), ' i',' I'),
    ' j',' J'), ' k',' K'), ' l',' L'), ' m',' M'), ' n',' N'), ' o',' O'), ' p',' P'), ' q',' Q'), ' r',' R'),
    ' s',' S'), ' t',' T'), ' u',' U'), ' v',' V'), ' w',' W'), ' x',' X'), ' y',' Y'), ' z',' Z')
    )

    It's about 8x faster than any of the solutions posted so far, leaving some scope for dealing with weird stuff like accents.



    Low-hanging fruit picker and defender of the moggies





    For better assistance in answering your questions, please read this.




    Understanding and using APPLY, (I) and (II) Paul White

    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
    Post #1532441
    Posted Wednesday, February 26, 2014 12:39 PM


    SSChampion

    SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

    Group: General Forum Members
    Last Login: Yesterday @ 2:05 PM
    Points: 13,327, Visits: 12,820
    I came back to this thread today because I was trying to proper case some stuff around here. I needed to exclude tokens that contain at least a single digit but all other tokens should be init caps.

    I made a slight modification to Dwain's that worked well for my purposes.

    CREATE FUNCTION [dbo].[ProperCaseWithNumbers]
    (
    @StrIn VARCHAR(255)
    )
    RETURNS TABLE WITH SCHEMABINDING
    AS
    RETURN

    SELECT STUFF((
    SELECT CASE
    WHEN split.Item LIKE '%[0-9]%'
    THEN ' ' + split.Item
    ELSE ' ' + Upper(LEFT(split.Item, 1)) + Lower(Substring(split.Item, 2, 255))
    END AS word
    FROM dbo.Delimitedsplit8k(@StrIn, ' ') split
    FOR XML PATH('')
    ,TYPE
    ).value('.', 'varchar(255)'), 1, 1, '') AS ProperCased

    We have some data coming from the mainframe and it is all caps but I need to make this data more "user friendly". Here is an example of this one in action.

    declare @MyVals table (SeriesName varchar(25))
    insert @MyVals
    select '9400 SERIES' union all
    select '9760 SERIES' union all
    select 'ADE360 SERIES' union all
    select 'CUSTOM' union all
    select 'M16360 SERIES' union all
    select 'N1260 SERIES' union all
    select 'N3160 SERIES' union all
    select 'PA360 SERIES' union all
    select 'S260 SERIES'

    select *
    from @MyVals v
    cross apply dbo.ProperCaseWithNumbers(v.SeriesName)



    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Moden's splitter.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
    Understanding and Using APPLY (Part 1)
    Understanding and Using APPLY (Part 2)
    Post #1545551
    Posted Wednesday, February 26, 2014 5:21 PM


    Hall of Fame

    Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

    Group: General Forum Members
    Last Login: 2 days ago @ 9:53 PM
    Points: 3,438, Visits: 5,390
    Sean Lange (2/26/2014)


    I made a slight modification to Dwain's that worked well for my purposes.



    Can't say I see that much of a resemblance but it is always good to hear success stories.



    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
    Since random numbers are too important to be left to chance, let's generate some!
    Learn to understand recursive CTEs by example.
    Splitting strings based on patterns can be fast!
    Post #1545645
    Posted Thursday, February 27, 2014 7:23 AM


    SSChampion

    SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

    Group: General Forum Members
    Last Login: Yesterday @ 2:05 PM
    Points: 13,327, Visits: 12,820
    dwain.c (2/26/2014)
    Sean Lange (2/26/2014)


    I made a slight modification to Dwain's that worked well for my purposes.



    Can't say I see that much of a resemblance but it is always good to hear success stories.


    Doh!!! That's because it was a modification of the code Luis posted.

    Thanks Luis!!!


    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Moden's splitter.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
    Understanding and Using APPLY (Part 1)
    Understanding and Using APPLY (Part 2)
    Post #1545879
    Posted Thursday, February 27, 2014 8:32 AM


    SSCarpal Tunnel

    SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

    Group: General Forum Members
    Last Login: Today @ 3:35 PM
    Points: 4,046, Visits: 9,202
    Sean Lange (2/27/2014)
    dwain.c (2/26/2014)
    Sean Lange (2/26/2014)


    I made a slight modification to Dwain's that worked well for my purposes.



    Can't say I see that much of a resemblance but it is always good to hear success stories.


    Doh!!! That's because it was a modification of the code Luis posted.

    Thanks Luis!!!


    I knew the code look familiar
    I guess that's the benefit of collective intelligence.



    Luis C.
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    Forum Etiquette: How to post data/code on a forum to get the best help
    Post #1545923
    Posted Thursday, February 27, 2014 8:43 AM
    SSC Rookie

    SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

    Group: General Forum Members
    Last Login: Thursday, November 13, 2014 6:03 AM
    Points: 30, Visits: 404
    I wonder if a CLR function would be worth considering here? .NET includes the System.Globalization.TextInfo.ToTitleCase() function which seems to do a pretty good job getting things properly capitalized. Not perfect but perhaps an extension method could handle the edge cases ?
    Post #1545928
    « Prev Topic | Next Topic »

    Add to briefcase «««1234»»

    Permissions Expand / Collapse