... my $search = "joe"; # for testing my $sql = qq{ ;with data_count as ( select sum(case when FIRST = 'joe' then 1 else 0 end) as a_count, sum(case when MIDDLE = 'joe' then 1 else 0 end) as b_count, sum(case when LAST = 'joe' then 1 else 0 end) as c_count from my_table where FIRST like 'joe%' or MIDDLE like 'joe%' or LAST like 'joe%' ) select 'Search by: ' + 'joe' union all select 'Found ' + convert(varchar, a_count) + ' ' + 'joe' + ' for First' from data_count union all select 'Found ' + convert(varchar, b_count) + ' ' + 'joe' + ' for Middle' from data_count union all select 'Found ' + convert(varchar, c_count) + ' ' + 'joe' + ' for Last' from data_count }; my $sth = $dbh->prepare($sql); $sth->execute() or die "SQL Error: $DBI::errstr\n"; my $data = $sth->fetchall_arrayref({}); warn Dumper $data; ...