Display Label on Row as Count Total instead of as Column

MikeM670
MikeM670 used Ask the Experts™
on
MS SQL 2016

I need help in displaying a text Label and count on a single row.

select count(Name) as 'Count: '
from Person.....

Count: 12
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012

Commented:
Something like this perhaps:
select 'Count: ' + cast(count(*) as varchar(10))
from Person.....
Topic Advisor, Page Editor
Distinguished Expert 2018
Commented:
select 'Count : ' + ltrim(count(*)) from Person

Open in new window

Author

Commented:
Both solutions worked but Marks was more elegant.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2012

Commented:
Both solutions worked but Marks was more elegant
You may want to look up the definition for COUNT (Transact-SQL).  In particular I would focus on the Return Type and the appropriate data types for  LTRIM()
Mark WillsTopic Advisor, Page Editor
Distinguished Expert 2018

Commented:
LTRIM() will always render the result as a varchar or nvarchar. So long as it is not text,ntext or image.

https://docs.microsoft.com/en-us/sql/t-sql/functions/ltrim-transact-sql?view=sql-server-2017
Top Expert 2012

Commented:
LTRIM() will always render the result as a varchar or nvarchar.
I don't disagree.  That was not the point.

You can also do the following (try it):
Declare @Date datetime = getdate()
select ltrim(@Date)

The question is does it make sense? or more to the point, does it reach the "elegant" level?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial