Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my sample table, and my expected output from totalchar:

+--------+--------------+-----------+
+---No---+-----letter---+-totalcha--+
+--------+--------------+-----------+
+---111--+--a,a,b,b,c,c-+-----3-----+  -> count distinct values only
+---222--+--e,e,f,g,h,i-+-----5-----+  -> count distinct values only
+--------+--------------+-----------+


Im using this query:

SQL
GROUP_CONCAT(distinct(letter) SEPARATOR ', ') as letter, sum(CHAR_LENGTH(letter) - CHAR_LENGTH(REPLACE((letter), ',', '')) + 1) as totalchar


And my output is these, which is wrong, can any body can help? thanks in advance:

+--------+--------------+-----------+
+---No---+-----letter---+-totalcha--+
+--------+--------------+-----------+
+---111--+--a,a,b,b,c,c-+-----6-----+  -> count distinct values only
+---222--+--e,e,f,g,h,i-+-----6-----+  -> count distinct values only
+--------+--------------+-----------+
Posted
Updated 23-Nov-14 16:42pm
v2
Comments
Shweta N Mishra 24-Nov-14 3:15am    
What are the result you expect against each row, Add that also i your question to make it more clear.
Jörgen Andersson 24-Nov-14 5:56am    
Do you have any specific reasons to break the First Normal Form?
http://en.wikipedia.org/wiki/First_normal_form

try with CHAR_LENGTH(REPLACE(GROUP_CONCAT(distinct(letter) SEPARATOR ', '), ',', ''))
 
Share this answer
 
v2
Comments
Member 11088078 24-Nov-14 0:14am    
Invalid use of group function
Member 11088078 24-Nov-14 1:51am    
could you complete the answer? because it still Invalid use of group function, I think a have some error with my code Im using this combined with your code.....sum(CHAR_LENGTH(letter) - CHAR_LENGTH(REPLACE(GROUP_CONCAT(distinct(letter) SEPARATOR ', '), ',', '')) + 1)
DamithSL 24-Nov-14 2:16am    
Shweta N Mishra 24-Nov-14 3:15am    
this wouldnt work for 222
Hi
Dude

Try this Solution

SQL
Declare @Temp as table (Sno int,Letter nvarchar(50),TotalChar int)
insert INTO @Temp VALUES(1,'a,a,b,b,c,c',0)
insert INTO @Temp VALUES(2,'e,e,f,g,h,i',0)

SELECT sno,Letter,
(SELECT count(DISTINCT Data) from dbo.Split((Letter),','))as Count
from @Temp



Before Execution (Split Function)

Create this function in your DB

CREATE FUNCTION [dbo].[Split]
(
@Line nvarchar(MAX),
@SplitOn nvarchar(5) = ','
)
RETURNS @RtnValue table
(
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED,
Data nvarchar(100) NOT NULL
)
AS
BEGIN
IF @Line IS NULL RETURN

DECLARE @split_on_len INT = LEN(@SplitOn)
DECLARE @start_at INT = 1
DECLARE @end_at INT
DECLARE @data_len INT

WHILE 1=1
BEGIN
SET @end_at = CHARINDEX(@SplitOn,@Line,@start_at)
SET @data_len = CASE @end_at WHEN 0 THEN LEN(@Line) ELSE @end_at-@start_at END
INSERT INTO @RtnValue (data) VALUES( SUBSTRING(@Line,@start_at,@data_len) );
IF @end_at = 0 BREAK;
SET @start_at = @end_at + @split_on_len
END

RETURN
END
 
Share this answer
 
v2
Comments
Member 11088078 24-Nov-14 1:42am    
I appreciate your suggestion but, im just a beginner to do that procedure, I just need a simple query so that i can finish my project. thanks
lokesh lokesh 24-Nov-14 1:57am    
That’s a simple process
first execute the below function

and then
Execute this code

Declare @Temp as table (Sno int,Letter nvarchar(50),TotalChar int)
insert INTO @Temp VALUES(1,'a,a,b,b,c,c',0)
insert INTO @Temp VALUES(2,'e,e,f,g,h,i',0)

SELECT sno,Letter,
(SELECT count(DISTINCT Data) from dbo.Split((Letter),','))as Count
from @Temp

I am showing u how to retrieve the column of the table
like that above solution u can modified your query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900