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

Capitalizing only first letter of each Word in a Column using Query Expand / Collapse
Author
Message
Posted Thursday, October 10, 2013 2:00 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 3:41 AM
Points: 95, Visits: 121
Hi ,


I am Having a Column full of Capital Letter words like "ABC DEF GHI " , i want to convert that into "Abc Def Ghi"

Please help me out its very Urgent !! I have googled it but i am getting all Stored procedures i dont want Stored procedures .

I just want to use update command and finish it . Please help me out .

Thank you
Post #1503400
Posted Thursday, October 10, 2013 2:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 11:26 AM
Points: 13,622, Visits: 10,514
Guitar_player (10/10/2013)
Hi ,


I am Having a Column full of Capital Letter words like "ABC DEF GHI " , i want to convert that into "Abc Def Ghi"

Please help me out its very Urgent !! I have googled it but i am getting all Stored procedures i dont want Stored procedures .

I just want to use update command and finish it . Please help me out .

Thank you


Get the stored procedure.
Remove CREATE PROC from the first line.
Run the statement.

This solution (which uses a loop unfortunately) uses a function:
Function to Convert Text String to Title Case – Proper Case
Are functions acceptable?




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1503403
Posted Thursday, October 10, 2013 2:19 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 3:41 AM
Points: 95, Visits: 121
The reason is like i dont know how to use the functions here what i know is update command but in my DB there thousands of Records then how can i convert them with the above mentioned Function . Please guide me what to do ?
Post #1503405
Posted Thursday, October 10, 2013 2:26 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 11:26 AM
Points: 13,622, Visits: 10,514
Google for a tuturial on SQL Server functions and read it.
You are responsible for your data and before you let any update statement loose on it, you should understand what you are doing.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1503408
Posted Thursday, October 10, 2013 2:47 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 3:41 AM
Points: 95, Visits: 121
Koen Verbeeck (10/10/2013)
Google for a tuturial on SQL Server functions and read it.
You are responsible for your data and before you let any update statement loose on it, you should understand what you are doing.


Yes i Agree , that Data is getting updated in the Capital format itself , so i came here to know what can be done
Post #1503415
Posted Thursday, October 10, 2013 3:02 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, August 29, 2014 3:17 AM
Points: 2,434, Visits: 7,513
-- CREATE SOME SAMPLE DATA TO USE TO TEST THE SOLUTION
IF object_id('tempdb..#temp') IS NOT NULL
BEGIN;
DROP TABLE #temp;
END;
SELECT ID, VAL
INTO #temp
FROM (VALUES(1,'ABC DEF GHI JKL'),
(2,'DEF DEF GHI JKL'),
(3,'D DEF GHI JKL'))a(ID,VAL);
GO

-- BEGINNING OF SOLUTION
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DelimitedSplit8K]') AND xtype IN (N'FN', N'IF', N'TF'))
BEGIN;
DROP FUNCTION [dbo].[DelimitedSplit8K];
END;
GO
CREATE FUNCTION [dbo].[DelimitedSplit8K]
-- See http://www.sqlservercentral.com/articles/Tally+Table/72993/
--===== Define I/O parameters
(@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
-- enough to cover VARCHAR(8000)
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
SELECT 1 UNION ALL
SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
SELECT s.N1,
ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
FROM cteStart s
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
Item = SUBSTRING(@pString, l.N1, l.L1)
FROM cteLen l
;
GO

-- HOW DO WE USE THE SOLUTION ?
SELECT a.ID, a.VAL, ca.VAL
FROM #temp a
CROSS APPLY (SELECT STUFF((SELECT ' '+UPPER(SUBSTRING(c.Item,1,1))+LOWER(SUBSTRING(c.Item,2,LEN(c.Item)-1))
FROM #temp b
CROSS APPLY [dbo].[DelimitedSplit8K](b.VAL,' ') c
WHERE a.ID = b.ID
ORDER BY c.ItemNumber
FOR XML PATH(''), TYPE
).value('.','NVARCHAR(MAX)'),1,1,''
)
)ca(VAL);

-- RESULTS
ID VAL VAL
----------- --------------- -----------------
1 ABC DEF GHI JKL Abc Def Ghi Jkl
2 DEF DEF GHI JKL Def Def Ghi Jkl
3 D DEF GHI JKL D Def Ghi Jkl



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1503421
Posted Thursday, October 24, 2013 3:32 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 3:41 AM
Points: 95, Visits: 121
Hi,

I have used the function to get the Result and i was able to get the answer and Sorry for the delay response .

Thank you.
Post #1507937
Posted Thursday, October 24, 2013 2:13 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
The question now is.... do you understand how it works?

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1508263
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse