ASP Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreASP Programming

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:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old July 17th, 2003, 05:06 AM
yaan yaan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 10 yaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Formatting post code string

Hi there,
I was wondering if someone could help me with a formatting problem for ASP/SQL?

Is it possible to format a variable string from a textbox?

I'll try and make it more clear what i want, sorry.

I have a page with a textbox and submit button for users to input a postcode, which then searches a database for matching or similar postcodes.

This is the query i am using:

<% Dim pcodequery1, pcodewhere1
pcodequery1="select * from tbl_basket"
pcodewhere1=" where basket_del_postcode like '%" & pcode & "%'"
pcodewhere1=pcodewhere1 & " order by basket_del_postcode desc"
pcodequery1=pcodequery1 & pcodewhere1 %>

I have already set the record sets etc. And the "pcode" variable is the name of the textbox variable, which i have set at the top of the page.

The query works fine for either exact matches or just typing a few letters.
The problem is that when the postcode has been entered it may or may not have a space in the middle, eg "hg2 7gk" or "hg27gk".
If i type a full postcode in and put a space in the middle where the original has no space i get told there is no match, and vice versa.

I'm sorry if this isn't very clear as this is my first post. If you need more from me just let me know.

Cheers loads,
Yaan out

Reply With Quote
  #2  
Old July 17th, 2003, 07:13 AM
Agarash Agarash is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: South Africa
Posts: 53 Agarash User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 35 sec
Reputation Power: 6
ok, one question, do all postcodes have the same lengh?? if so the lets say the length of all postcodes are 6 characters, then simply limit your textbox to 6 characters. else....

it sounds like youre gonna need to do some string formatting... im not to sharp on this, but in Javascript, youre gonna need to SEARCH your STRING for "_" (a empty space) and erase that.

there may be a simpler solution, but im not sure.

Reply With Quote
  #3  
Old July 17th, 2003, 08:10 AM
yaan yaan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 10 yaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for your reply, unfortunately i am working with a database which i have no control over how users input the postcodes.

The postcodes are not more than 8 characters long but could be as little as 6.

i don't think that taking the spaces out of the textbox input would work as i am having to search a database that is, unfortunately, not a set format.

Again, thanks for the help as it is greatly appreciated!

Yaan out

Reply With Quote
  #4  
Old July 17th, 2003, 10:26 AM
lordkyl lordkyl is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 27 lordkyl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It is tough working with data that many different users have input.

Especially if you have no control over validation. If you change the form and create some javascript validation, that would solve future mis-typed postal codes (if these postal codes are all from the same region and therefore in the same format), but then you still have the ones in the database.

How many records are there? If there aren't many, you could go through and format each one correctly.

This would be the best approach, I think.

If there are lots, see if you can find common mistakes (like an extra space) and using an update query remove them.

Reply With Quote
  #5  
Old July 17th, 2003, 11:03 AM
yaan yaan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 10 yaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the feedback,
looks like i'm going to have to look at re-formatting the info in the database if possible.

Cheers for the help, i'll let you know if i find another solution.

Yaan out

Reply With Quote
  #6  
Old July 17th, 2003, 02:56 PM
Shad Shad is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 Shad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Code:
SELECT
  myfield
FROM
  mytable
WHERE
  replace(myfield,' ','') LIKE '%stuff%'
That should work if you're using MS SQL (it's T-SQL compatible code) but I don't know about Access or MySQL etc. Replace 'stuff' with Replace(postcodestring," ","") in ASP.

Reply With Quote
  #7  
Old July 18th, 2003, 04:40 AM
yaan yaan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 10 yaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Looks like it's going to have to be put up with until i can find a solution as it's not feasable to re-format the database data due to other dependancies.

I was wondering if there was a way of dissecting the search string and making the query first look for a whole match and then matches for just the first 2 or 3 characters?

Again, any suggestions are welcome.

Thanks for the help once again.

Reply With Quote
  #8  
Old July 18th, 2003, 07:45 AM
lordkyl lordkyl is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 27 lordkyl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You could do this...

SELECT
myField
FROM
myTable
WHERE
otherField LIKE '%something%' OR otherField LIKE '%firstThreeChars%'


If you are using access & asp, just setup two variables for something and then use the left function to get the first three characters.

If you are using SQL Server, you could write a stored procedure to do it for you.

Reply With Quote
  #9  
Old July 21st, 2003, 03:25 AM
yaan yaan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 10 yaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It's looking like the only way i could do it without re-formatting all the data would be if the server was running the full text search option shipped with MS SQL server 2000.
If it was running this then i should, in theory, be able to use the "CONTAINS" or "SIMILAR" queries.

I'll keep looking.

Cheers again,
Yaan

Reply With Quote
  #10  
Old July 21st, 2003, 07:32 AM
Shad Shad is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 Shad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If you are running MS SQL server, then the T-SQL code I posted WILL work. There is no need to a full text search, as far as I can see.

Reply With Quote
  #11  
Old July 21st, 2003, 08:29 AM
yaan yaan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 10 yaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the "left" suggestion.

It now works, all i had to do was run the original query and then say that if there were no results, run a new query using a new variable: "pcode2=left(pcode, 3)"
I just told the new query to look for matches for this instead.

Thanks for the help, this is a fine forum!

Yaan out (again)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreASP Programming > Formatting post code string


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 6 hosted by Hostway