SQL Server Puzzle | Make all UPPER/LOWER case of string to Proper Case/Title Case

In this puzzle you have to given an input string. You have to make first letter of each work in upper case and all the remaining characters in lower case

Please check out the sample input and the expected output below-

Sample Input

‘7what the 789hell is 78 going on here dshf 78’

Expected Output

7what The 789hell Is 78 Going On Here Dshf 78

Rules/Restrictions

• The solution should be should use SINGLE “SELECT” statement.
• Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com

Script | use below script to create table & insert some sample data

 ``` -- DECLARE @String VARCHAR(1000) = '7what the 789hell is 78 going on here dshf 78' -- ```

Solution 1 | Number Table

 ``` -- DECLARE @InputString VARCHAR(1000) = '7What the 789hell is 78 going on here dshf 78' DECLARE @OutputString VARCHAR(1000) = '' SELECT @OutputString = @OutputString + CASE WHEN Number = 1 THEN UPPER(SUBSTRING(@InputString,Number,1)) WHEN SUBSTRING(@InputString,Number-1,1) = '' THEN UPPER(SUBSTRING(@InputString,Number,1)) ELSE LOWER(SUBSTRING(@InputString,Number,1)) END FROM ( SELECT DISTINCT Number FROM MASTER..SPT_VALUES N WHERE Number > 0 AND Number < LEN(@InputString) )u SELECT @OutputString Outputs GO -- ```

Solution 2 | George Mastros | Modified Version

 ``` -- DECLARE @String VARCHAR(1000) = '7what the 789hell is 78 going on here dshf 78' DECLARE @Position INT = -1 SELECT @String = STUFF(LOWER(@String),1,1,UPPER(LEFT(@String,1))) SELECT @Position = PATINDEX('%[^A-Za-z0-9''][a-z]%',@String COLLATE Latin1_General_Bin) WHILE @Position > 0 SELECT @String = STUFF(@String,@Position,2,UPPER(SUBSTRING(@String,@Position,2))), @Position = PATINDEX('%[^A-Za-z0-9''][a-z]%',@String COLLATE Latin1_General_Bin) SELECT @String -- ```

Solution 3 | Joe Celko

 ``` -- declare @Foobar table(data varchar(1000)) insert into @Foobar select '7this is only for testing' union all select 'SEe if This workS well' union all select 'TESTING DATA' /* Original data */ select * from @Foobar /* Modified data */ select ltrim ( 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( (LOWER (' '+data)) ,' 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') ,' x',' Z') ) AS data from @Foobar -- ```

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal

Http://MSBISkills.com

Advertisements
{"ft_lang_label":"__label__en","ft_lang_prob":0.5795399,"math_prob":0.8967056,"size":9493,"snap":"2019-13-2019-22","text_gpt3_token_len":2228,"char_repetition_ratio":0.25661293,"word_repetition_ratio":0.07064472,"special_character_ratio":0.24428526,"punctuation_ratio":0.1709354,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97915953,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-05-26T09:19:17Z\",\"WARC-Record-ID\":\"<urn:uuid:2621ef23-341c-4b67-bdc6-6997cad8ebfa>\",\"Content-Length\":\"140635\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3e568e86-fcf3-4552-b2e8-9b81d8325275>\",\"WARC-Concurrent-To\":\"<urn:uuid:754329b2-b5cc-4f61-b7e7-773bff91ebe8>\",\"WARC-IP-Address\":\"192.0.78.25\",\"WARC-Target-URI\":\"https://msbiskills.com/2016/06/26/sql-server-puzzle-make-all-upperlower-case-of-string-to-proper-casetitle-case-multiple-methods/\",\"WARC-Payload-Digest\":\"sha1:O4U6K7L2TVOCAWHYERLJ5HOJFGT6SCIH\",\"WARC-Block-Digest\":\"sha1:YXW2DHMO55B4T6TBZ7G5BF5GS3TCTSTM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-22/CC-MAIN-2019-22_segments_1558232259015.92_warc_CC-MAIN-20190526085156-20190526111156-00024.warc.gz\"}"}