Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count and Like

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2012-10-24 : 11:39:34
I have a table called Users and want to count how many lastname of Smith are in the table.

Is this correct?

Count * from Users where lastname like ‘University%

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-24 : 11:47:26
nope

it should be

SELECT COUNT(*) FROM Users WHERE lastname like ‘University%


lookup books online for syntax help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2012-10-24 : 11:59:25
Thanks!

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2012-10-24 : 12:00:54
One more...

I want to get all users whose ages are between 15 - 20

SELECT COUNT(*) from Users WHERE age between 15 and 20

Want to make sure between is correct.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-10-24 : 12:12:06
SELECT COUNT(*) from Users WHERE age between 15 and 20
The end-points are included, so this will give you anybody where age >= 15 and age <= 20, if that's what you want.


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2012-10-24 : 12:31:20
If I have a query that says how many last names begin with S should I count them our list them?
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-10-24 : 12:44:53
quote:
Originally posted by JJ297

I have a table called Users and want to count how many lastname of Smith are in the table.

Is this correct?

Count * from Users where lastname like ‘University%


Count * from Users where lastname = 'Smith'



quote:
Originally posted by JJ297

If I have a query that says how many last names begin with S should I count them our list them?

Umm, it's your query. Do you not know if you need a list or a count?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-24 : 12:46:08
quote:
Originally posted by JJ297

If I have a query that says how many last names begin with S should I count them our list them?


does that mean you dont know what you want?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2012-10-24 : 12:49:59
Yes I know I want them both. I'm OK now. Thanks!
Go to Top of Page
   

- Advertisement -