Error converting data type varchar to bigint.

Hello,

I try to format the following query

DECLARE @total_buffer INT;

SELECT @total_buffer = cntr_value
    FROM sys.dm_os_performance_counters
    WHERE RTRIM([object_name]) LIKE '%Buffer Manager'
    AND counter_name = 'Target Pages';

;WITH src AS
(
    SELECT
        database_id, db_buffer_pages = COUNT_BIG(*)
        FROM sys.dm_os_buffer_descriptors
        --WHERE database_id BETWEEN 5 AND 32766
        GROUP BY database_id
)
SELECT
    [db_name] = CASE [database_id] WHEN 32767
        THEN 'Resource DB'
        ELSE DB_NAME([database_id]) END +'|'+
    db_buffer_pages +'|'+
     db_buffer_pages / 128 +'|'+
     CONVERT(DECIMAL(6,3),
        db_buffer_pages * 100.0 / @total_buffer) FROM src;

Open in new window


The error Error converting data type varchar to bigint. returned. How can I resolve it?
Thanks
bibi92Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
What is the name of field you want to convert?

If it is Field1, try:

database_id, db_buffer_pagesX = CONVERT(COUNT_BIG, Field1)

 Also I notice you have:

WHERE RTRIM([object_name]) LIKE '%Buffer Manager' ...
   
Which is the same as

WHERE [object_name] LIKE '%Buffer Manager%' ...

but much slower. Loose RTRIM()

Mike
0
bibi92Author Commented:
Hello,

I need to add  +'|'+ between each field and I think this generated the error. I have to convert  CONVERT(DECIMAL(6,3),
        db_buffer_pages * 100.0 / @total_buffer)
Thanks
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Just for kicks and giggles, let's identify the exact line that throws the error...
Execute the function, and when it errors again double-click on that error message, and note where the cursor jumps.  That is the line that caused the error.
Post that line into this question.
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

bibi92Author Commented:
Hello,

The line that caused the error :
+'|'+
     CONVERT(DECIMAL(6,3),
        db_buffer_pages * 100.0 / @total_buffer) FROM src

Thanks
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
+'|'+   is a string.
 CONVERT(DECIMAL(6,3), anything) is a decimal.

You can't concatenante a string with a non-string, so in order to pull this off you have to convert the decimal value to a string, something like this...

+'|'+
     CAST(CONVERT(DECIMAL(6,3),
        db_buffer_pages * 100.0 / @total_buffer) as varchar(100)) FROM src
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
bibi92, do you still need help with this question?
0
bibi92Author Commented:
thanks regards
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade, good luck with your project.  -Jim
0
bibi92Author Commented:
Thanks a lot for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.