Wednesday, December 28, 2005

Paging in SQL

Many times, it is required to implement paging in SQL. Well you can find many ways to accomplish it but recently I came across to a very simple way of doind this.

Consider you want to find the records from 21-30 in order of their IDs, then you can do this using the folllowing query:

SELECT TOP pagesize *FROM (SELECT TOP (pagesize*pagenumber) * FROM tableName ORDER BY ID)
ORDER BY ID DESC.

Yup it is a very easy and handy way to implement paging.

Monday, December 19, 2005

Validating numeric data type

Many times we face the situation where we want to validate whether user input is numeric or not. In .Net it is reallly esy to validate the data type of user input using Regular Expressions. An example of how to validate that the data type of user input is numeric is:

bool Validate(string text)
{
Regex reg=new Regex(@"\D");
return !reg.Match(text).Success;
}

Here if text contains only digits then the resull of reg.Match is false, so Validate will return true and vice versa. Here decimal point is also considered as non digit and will cause the result of the function to be false.

Now the following code snippet will recognize the period "." as the part of the digits also but more than one periods will not cause error i.e 123.45.67 will also return true.

bool Validate(string text)
{
Regex reg=new Regex(@"[^0-9.]");
return !reg.Match(text).Success;
}