Steven
Steven

Reputation: 155

Neither cast nor convert is working for me using a select statement

Below is my cast attempt:

cast((select count(distinct tm.MEMBER_ID) from #tempmemid as tm) as int(64))

After that didn't work I attempted to convert:

convert(int, (select count(distinct tm.MEMBER_ID) from #tempmemid as tm)

Is the syntax incorrect? Can someone explain the difference between cast and convert more finitely than the Google results I obtained?

The error messages I'm receiving for each are as follows:

cast: incorrect syntax near the keyword 'as'

&

convert: incorrect syntax near the keyword 'convert'

Upvotes: 0

Views: 77

Answers (2)

cloudsafe
cloudsafe

Reputation: 2504

The first will work if you remove '(64)':

SELECT cast((select count(distinct tm.MEMBER_ID) from #tempmemid as tm) as int)

but as Sean points out below, there is no need to CAST a COUNT as INT.

The second has incorrect brackets:

SELECT convert(int, (select count(distinct tm.MEMBER_ID) from #tempmemid as tm))

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269753

There is no need to cast() the return value of count() to an integer. That is what the value already is.

I assume you want a bigint. Don't use a cast(), use count_big():

(select count_big(distinct tm.MEMBER_ID) from #tempmemid as tm) 

This returns a bigint.

Upvotes: 2

Related Questions