0

How do you convert varchar to int in SQL? My code is as such but I've tried casting and converting but they don't work. Declare/set doesn't work either.

select 
    Branch.BranchNo, 
    COUNT(Member.MemberID) as 'Number of Members' 
from 
    Branch
where 
    'Number of Members' > 2

Error:

Conversion failed when converting the varchar value 'Number of Members' to data type int.

3
  • 2
    You don't really want to convert varchar to int -- the problem you're having is that it's trying to compare the words 'Number of Members' to the integer 2, so it's trying to convert. What you really want is where [Number of Members] > 2, I think, or just where COUNT(Member.MemberID) > 2. Commented Jun 3, 2014 at 20:08
  • 2
    SELECT Branch.BranchNo, COUNT(Member.MemberID) as 'Number of Members' FROM Branch ........ GROUP BY Branch.BranchNo HAVING COUNT(Member.MemberID) > 2 Commented Jun 3, 2014 at 20:09
  • 'Number of Members' is saved as a varchar ,specifically the number 9 right now, but I can't compare it to an int number since it's saved as a varchar. Thank you for the suggestion but I got error" Invalid column name" instead Commented Jun 3, 2014 at 20:12

4 Answers 4

4

Use:

select Branch.BranchNo, COUNT(Member.MemberID) as 'Number of Members' from Branch
......
HAVING COUNT(Member.MemberID) > 2

Sql runs in the following order: from where group by having select order by.

You can't use the column alias in any clause except the order by.

You need to use the count function again instead of the alias you gave. Also, you can't use an aggregate function in the where clause. Where runs before any aggregation happens in the group by clause. Even if you don't explicitly have a group by clause sql will consider the entire result set as a group.

If you want to filter on an aggregate like sum/count/max it must happen in the having clause.

Sign up to request clarification or add additional context in comments.

Comments

1

You need group by and having

select Branch.BranchNo, COUNT(Member.MemberID) as "Number of Members" 
from Branch
group by Branch.BranchNo
having COUNT(Member.MemberID) > 2

Comments

0
SELECT
    BRAND.BRANCHNO,
    COUNT{MEMEBER.MEMBERID) AS [NUMBER OF MEMBERS]
FROM 
    BRANCH
GROUP BY 
    BRAND.BRANCHNO
HAVING 
    COUNT(MEMBER.MEMBERID) > 2 

Comments

0

You cannot use grouping functions as count in "where" statements. Try to use HAVING instead:

select Branch.BranchNo, COUNT(Member.MemberID) as 'Number of Members' from Branch HAVING COUNT(Member.MemberID) > 2

Anyway, to convert VARCHAR to INT will depends of your DB vendor:

Oracle: TO_NUMBER(varcharValue)

SQL Server: CAST(varcharValue AS INT)

Postgres: CAST(varcharValue AS INTEGER)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.