Categorized | Programming

5 Extremely Useful ASP Functions

Posted on 04 April 2006

In this article I describe my 5 favorite functions in ASP. Examples included.


1. Left()

Why is Left() useful? Let’s say that you like to give credit to a certain website by showing the URL. What if the URL is 200 characters long? Left() solves this problem. Here’s an example:

Left(myRecordSet(”URL”),30)

myRecordSet is the recordset you define for your database. URL is one of the fields in your database. Next is the important part, 30. 30 is the number of characters that display. Left() essentially eliminates any character after that. The ‘…’ is just for a professional look and to let the user know that the URL is pretty long.

2. Now()

I know what you’re thinking. Now() isn’t that useful. Now() is extremely useful. Maybe not by itself but it is when you manipulate it. What if you want to display a list of blog entries over the last month? A useful example:
SELECT entries FROM myBlog WHERE date BETWEEN Now() AND (Now() - 30)

3. Request.ServerVariables(”HTTP_REFERER”)

What’s funny about this one is that referrer is spelled wrong in the function! And it works! Anyways, let’s say someone leaves a comment on your blog but it’s 5 pages deep. After they leave a comment, do you want to redirect them back to your main page? No, you want to redirect them to the page they were just viewing. It’s done like this:

refurl = Request.ServerVariables(”HTTP_REFERER”)
Response.Redirect refurl

Your form has to add the comment to the database so be sure to put the redirect at the end of the page in your ASP code. Response.Redirect is also really useful, too. A two for one!

4. Trim()

Suppose you want to add user registration to your site. Without Trim() anyone could create a username like ” “. Blank space. You don’t want a user called ” “. Trim() solves this problem. Here’s an example:

user = Trim(request.form(”username”))

This function gets rid of all the whitespace. This way if a user tries to register “john ” it’ll become “john”.

5. DateAdd()

This function is a little more complicated. I won’t go into all the different options with this one because it could fill another entry as large as this one. When it comes to ASP, you use this function with cookies. An example:

Response.cookies (Logged_In).Expires = DateAdd(”N”, 30 ,Now)

This function basically says “the cookie will expire in 30 minutes”. Why is Now() in there? Because it adds 30 minutes onto Now(). “N” means minutes. “Active” is just the name of your cookie.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

This post was authored by:

Brian - who has written 3 posts on Tech Pedia.

I am the webmaster of fantacular.com

Contact the author

4 Comments For This Post

  1. Nick says:

    left(), trim(), datedd(), and now() are not ASP functions per se, they are vbscript functions. Conversely, the request and response objects are unique only to ASP.

  2. Brian says:

    I thought about giving the article the title “5 Extremely Useful VBScript Functions” but you use VBScript with ASP. And in all the examples I wrote I made sure they had something to do with a database. ASP deals with databases.

  3. Nick says:

    True. Nice site btw.

  4. Brian says:

    Thanks!

Leave a Reply

Site Sponsors

  • programming freak: And by the way thanks for those useful codes, i am surprised a site like this has C codes? and...
  • programming freak: Keep posting some codes man, i would be glad to find some c/c++/java/html/unix codes, sql queries...
  • programming freak: Hey thanks, good one!
  • Nathaniel Baker: its all good information.. but you forgot one very very important thing…. FAT32 can not...
  • om: Yeah sure it is.. :)

Older Posts

Ads

  • Advertise Here

Topics