MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 14th, 2003, 10:25 PM
aaron.martone's Avatar
aaron.martone aaron.martone is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Central Florida
Posts: 76 aaron.martone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via ICQ to aaron.martone
Exclamation Long SQL Statement

I'm relatively new to SQL statements but I have a solid computer background so I should be able to pickup easy if someone has an idea for me.

Quickly, I work with a DWMX authoring environment, on IIS 5.1 / WinXP Pro using an Access 2000 DB. I have this SQL statement that looks like (recalling from memory since I'm not on the same box that has the code)

SELECT *
FROM Employes
WHERE NAME LIKE '%MM_Name%' AND EMPLOYEENUM LIKE '%MM_EmpNum%' AND HOMEPHONE LIKE '%MM_HomePhone%' AND CELLPHONE LIKE '%MM_CellPhone%' AND PAGERNUM LIKE '%MM_PagerNumber%' AND RADIONUM LIKE '%MM_RadioNumber%'
ORDER BY NAME ASC

And since I'm using DWMX, I can setup default values and run-time values for each of the variables in that SQL statement (MM_XXX = a variable), ie:

Variable / Design Time Value / Run Time Value
---------------------------------------------------------
MM_Name / % / Request.Form("txtName")

This means that the variable MM_Name will default to % (wildcard) if nothing is supplied in the previous page's txtName input box (which is submited via a POST, thus the Request.Form)

Anyways, only 2 fields are data types of Number, and the rest are Text data types. The 2 number fields are RADIONUM and EMPLOYEENUM.

I read somewhere that if a value is numerical, you don't use quotes around it. so

AND RADIONUM LIKE '%MM_RadioNum%'

should be

AND RADIONUM LIKE %MM_RadioNum%

And THIS is where I'm getting problems. The reason why this SQL statement is so big is because it's working for a search page where the user can input search criteria from many different aspects (search by name, extension, phone numbers, etc.)

So my question (odd as it'll sound here) is, "What is the difference between saying - ... AND Variable LIKE Value compared to AND Variable = Value? When should I use LIKEs and when should I not? The nice thing about the DWMX interface is that while building the SQL statement, I can test to see the recordset returned by changing the default values to values the user may have entered, pressing TEST and seeing the results. Well all variables are setup to % as default.

I typed in "aaron" in the NAME field, pressed TEST and got nothing back. My Employee Number is 3022, and I changed NAME back to % and changed EMPLOYEENUM to default to 3022 (no quotes) and pressed TEST. No results. Obviously my SQL code isn't doing what I want, but I'm stumped as to why.

Sorry for the long explanation; hope that doesn't put people off to help me. I just wanted to make sure I wasn't leaving anyone scratching their head when they say the forum post.

My thanks in advance!
__________________
aaron martone
professional web developer

Reply With Quote
  #2  
Old November 17th, 2003, 09:18 AM
smackmeister smackmeister is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 21 smackmeister User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
where are you getting the info for cell phone, pager, etc? in your query, you are using AND which implies that the user has to input all of the neccessary data for a record to be returned. if they are not passing all of this data, try using OR instead.

you should only be using like statements when the user has the option of entering partial names or id numbers. if you are requiring them to enter the entire name or id, then i would use = instead of LIKE.

Reply With Quote
  #3  
Old November 20th, 2003, 10:49 AM
jluke-b jluke-b is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: italy
Posts: 5 jluke-b User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi AARON
You should use LIKE only to compare string fields with a a string model (with or without jolly character). If U want to compare a number field by "LIKE" you should convert its content in nvarchar by "CONVERT" .
4 example :
SELECT *
FROM Employes
WHERE convert(nvarchar,RADIONUM) LIKE '%MM_RadioNum%'

I Hope this cound help.
BYE

Reply With Quote
  #4  
Old November 20th, 2003, 01:07 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 27 m 14 sec
Reputation Power: 891
yes, you are right, you do not put quotes around numerics

i personally do not see the logic of looking for a particular digit inside a number

this is what you will get when using the string function LIKE on a numeric field (assuming the implicit conversion to string takes place)

it's fine to look for the string 'drew' inside the name field, and expect to get results for Andrew, Drew, etc.

but looking for numbers, i would expect to look for exact matches, and therefor use equality, not LIKE

also, Access 2000 uses the asterisk instead of percent sign as its wildcard character, so you would have to find a way to get dreamweaver to generate LIKE '*MM_field*' or go in after and change it yourself

rudy
http://r937.com/

Reply With Quote
  #5  
Old April 8th, 2004, 07:45 PM
maxfresh maxfresh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 maxfresh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I think i know why ...

It seems that the % does not work on number fields, only text fields, that's how it was designed.

Also, the % does not work with =, it only works with LIKE(note that it still doesn't select empty text fields though). I think that was your main question.

So MY question is, what is the wildcard character that will select all the records in a number filed, or auto number field?

I too am using DWMX with Access. I want to use this function so that when submitting a search form, the user can leave a field empty and the search will return all the records.

So, although i think i know why ... i still havn't found the solution.
maxfresh

Reply With Quote
  #6  
Old April 8th, 2004, 07:59 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 27 m 14 sec
Reputation Power: 891
the solution is, don't generate a condition into the query for that column

see http://forums.devshed.com/t133303/s...ighlight=option
__________________
r937.com | rudy.ca

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Long SQL Statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway