Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Query help

$
0
0

Hi All,

I have a situation where I need to select first name from a name column of this format and its count.

Schwerdtfeger, Susan C
Dodson, Susan I
Hubbard, Susan A
Hyde, Susan C
Mccue, Susan

Jim, Haley

Jill,Haley A

my output:

Fname Cnt

Susan 5

Haley 2

Here is my version of the script but it doesnot return quite what i want. 

Select Fname, Count(*) as cnt from (
SELECT   case when charindex(',', Name) > 0 
                  Then SUBSTRING(Name, CHARINDEX(',', Name) + 2, charindex(' ', Name) -2)
                  end   as Fname
  FROM [namestable] dp (nolock)
) T GROUP BY Fname 
Any help is highly appreciated. Thanks a ton.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>