Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple sql syntax/statement 1

Status
Not open for further replies.

thepower1986

Programmer
Joined
Jun 25, 2010
Messages
2
Location
GB
Hi guys im extremely new to SQL but im learining.

I would really appreciate if someone can check my sql statment and syntax just to check if it is correct.

its only a few lines

Assuimng "users" as the table and "age" as a column holding numeric data.

I'm trying to view all persons 18 and over in descending order,

SELECT age
FROM users
WHERE age >=18
ORDER BY age DESC

Regards

jon

thanks in advance.
 
I don't see anything wrong with the syntax.

Usually, it is a bad idea to store things like age in a database because age is constantly changing (man, do I feel old). Instead, it is better to store a birth date and then calculate the age or filter a different way.

For example:

[tt]
Select Name, BirthDate
From Users
Where Birthdate < DateAdd(Year, -18, GetDate())
Order By BirthDate
[/tt]

This query will only return users that are at least 18 years old and will sort them from oldest to youngest.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Ok I think I got it.
DateAdd(Year, -18, GetDate()) im assuming the DateAdd gets the current year subtracts by 18 and checks against the birthdate. Sorry if im stating the obvious but I am a novice. Do you have any links where I can learn these kinds of things from.

Thanks alot for your help
 
GetDate() returns the current date and time.

The DateAdd function takes 3 parameters. The first parameter is the interval and can be year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, or millisecond. The 2nd parameter is the quantity to add, and the third parameter is the date you are adding to.

So, in this example, we are adding -18 years to the current date and time.

There are TONS of resources on the internet for learning about SQL Server.

I've written a few blogs about SQL server. You can find them here:
I also strongly recommend you learn about database normalization. In fact, you should do this first. Rudy has a nice blog that is easy to read and understand. That can be found here:
And, of course, if you have specific questions, please feel free to post them here. You should start a new thread for each different question.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top