Registered member login:
Register Now
Altirigos » Altiris Administrators » SQL » SQL Basics Part II

» Current Poll
Do you leave the Aclient enabled?
YES - 82.93%
102 Votes
NO - 17.07%
21 Votes
Total Votes: 123
You may not vote on this poll.
» Stats
Members: 9,465
Threads: 11,733
Posts: 55,226
Top Poster: Nick (4,981)
Welcome our newest member, Serendipity
» Online Users: 33
0 members and 33 guests
No Members online
Most users online at once 294, 06-30-2007 at 01:24 PM.
» March 2010
S M T W T F S
28 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 123
Reply
Old 10-17-2005, 05:15 PM   #1 (permalink)
 
Nick's Avatar
 
Status: Altiris Architect (Site Founder)
Join Date: 01-01-2005
Location: RDU, North Carolina, USA
Posts: 4,981


SQL Basics Part II

SQL Basics Part II

The WHERE clause is an optional parameter that can be used to filter information from the database. Following is the basic SQL query structure with a WHERE clause.

SELECT <column name> FROM <table name>
WHERE <column name operator value>

Odds are, if you’re new to SQL, you’re saying.. “Great Nick… but what the heck is a <column name operator value>?” Fair enough question that is important to understand for using the WHERE clause.

The WHERE clause can be used with the following operators or conditions.



Operator Description
= Equal
<> Not Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern



With this information lets run a query against the vcomputer table that we used in Part I.



Name Domain User OS Name OS Version IP Address
Computer1 Altiris Jim Windows XP 5.1 192.168.0.1
Computer2 Altiris Andrew Windows 2000 5.0 192.168.15.4



SELECT * FROM vcomputer
WHERE Name = ‘Computer1’


This would return the following results from our table.

Name Domain User OS Name OS Version IP Address
Computer1 Altiris Jim Windows XP 5.1 192.168.0.1



You will notice that I used single quote marks around the value. You will want to do this for the value you are searching for. I have read that this is only needed for text values and can be left out for numerical values. I haven’t been successful doing that but wanted to mention it.

You use the LIKE operator if you want to get results from a field whose value contains specific data. When using the LIKE operator, you will most likely want to use the percentage (%) symbol within the value. The percentage (%) symbol tells the database to return the defined value plus anything that would be present in the same location as the percentage symbol. Lets look at an example.

SELECT * FROM vcomputer
WHERE Name LIKE ‘Computer%’

This query is telling the database to return all results from the vcomputer table where the Name column contains any value of Computer + any value after it. Ie… Computer1, Computer2, Computer3

This would return:

Name Domain User OS Name OS Version IP Address
Computer1 Altiris Jim Windows XP 5.1 192.168.0.1
Computer2 Altiris Andrew Windows 2000 5.0 192.168.15.4

SELECT * FROM vcomputer
WHERE Name LIKE ‘%2’

This query is telling the database to return all results from the vcomputer table where the Name column contains any value of 2 + any value before it. Ie… Computer2, Computer22, Computer32, Computer2Test

This would return:

Name Domain User OS Name OS Version IP Address
Computer2 Altiris Andrew Windows 2000 5.0 192.168.15.4

One bit of information that will be good to keep in mind. When dealing with column names that have spaces in them you will need to enclose them with brackets [] as shown in the following example.

SELECT * FROM vcomputer
WHERE [OS Name] LIKE ‘Windows%’

This concludes part II of the SQL Basics. I have attached a copy of this in Word document format for those that may want it.
Attached Files
File Type: doc SQL Basics Part II.doc (43.0 KB, 151 views)
__________________
Scire potentia est (knowledge is power)
Nick is offline   Reply With Quote
Old 10-17-2005, 10:50 PM   #2 (permalink)
 
JAustgen's Avatar
 
Status: Site Administrator (Ex-SQL Savant)
Join Date: 02-16-2005
Location: Colorado Springs, Colorado, USA
Posts: 1,853


Nice work, Nick.
__________________
Jim Austgen
JAustgen at hotmail.com
http://www.ns-experts.com
JAustgen is offline   Reply With Quote
Old 10-19-2005, 12:14 PM   #3 (permalink)
 
Status: Altiris Admin
Join Date: 09-17-2005
Location: ottawa
Posts: 42


SQL Basics Part II

Great lesson!

I'll just add one more tidbit that bit me the other day.
Some of the column names that the db uses are reserved words in SQL.
The most obvious one being User.
So, whenever you use this column in a query you also have to use the [];

WHERE [User] = 'artusod'

[Name] would be another one.

Dan
artusod is offline   Reply With Quote
Old 10-19-2005, 01:31 PM   #4 (permalink)
 
Nick's Avatar
 
Status: Altiris Architect (Site Founder)
Join Date: 01-01-2005
Location: RDU, North Carolina, USA
Posts: 4,981


Great point Dan!

Thanks for pointing that out!
__________________
Scire potentia est (knowledge is power)
Nick is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -4. The time now is 07:03 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
vB.Sponsors
Altirigos