Tuesday, February 22, 2011

Generating random hex strings using MS SQL Server

In the post Generating random numbers in a specific range using MS SQL Server we saw how it is possible to generate random integer number for a given range. We will use the function dbo.random_range from this post in order to create a function that generates a random string of hex numbers of any length.
The idea is very simple. Suppose we would like to generate a string of length N, then all we have to do is loop N times and generate a random number POSITION in the range of 1 to 16. For each of the random values we take a substring of one character in the generated POSITION from a predefined string containing all Hex digits (“0” to “9” and “a” to “f”). After we done we will have a string containing N Hex digits.
Let’s have a look at the SQL SERVER code:

CREATE FUNCTION random_hex(@length int)
RETURNS varchar(2000)
  DECLARE @index int
  DECLARE @seq varchar(16) 
  DECLARE @result varchar(2000)
  DECLARE @digit char(1)
  SET @seq = '0123456789abcdef'
  SET @index = 0
  SET @result = ''
  WHILE @index < @length
    SET @digit = SUBSTRING(@seq, dbo.random_range(1, 16), 1)
    SET @result = @result + @digit
    SET @index = @index + 1
  RETURN @result

Monday, February 21, 2011

Generating random numbers in a specific range using MS SQL Server

In the post Generating Random numbers in a SELECT statement using MS SQL Server we saw how random numbers can be generated using Microsoft MS SQL Server. We will use the dbo.random function from this blog in order to create a new function called dbo.random_range.
This function will generate a random number for a specific range. For example: we might need to generate a random number between 100 and 1000.
The dbo.random_range function will return a random number between @start and @end parameters. The dbo.random function generates a random number between 0 and 1. In order to create a random number for a given range we will use a small mathematical calculation which is pretty easy and straightforward. Therefore we will simply understand it by looking at the dbo.random_range function:

CREATE FUNCTION [dbo].[random_range](@start int, @end int)
  return @start + dbo.random() * (@end - @start + 1)

Sunday, February 13, 2011

Create Click to Call link on Mobile Phones

Mobile web browsers are also phones. Creating links that lead directly to a call can be a very easy and continent way.
Most mobile web browser support click to call links. For obvious reason, all of them will confirm with the user before making the call.
Creating click to call links is very easy. In general there are 2 main types of click to call links:
* Apple devices
* All the other phones, especially feature phones (old generation mobile phones).

Apple use a simple way of doing call links. All you have to do is use the "tel" protocol instead of "http" when creating a link. For example, if you would like to make a call to the phone number: 1234567 when use clicks your link, all you have to do is:

<a href="tel:1234567">Call me now!</a>

This will also work:

<a href="tel://1234567">Call me now!</a>
Feature phones do not support Apple method, but most of them do allow call links, by using a similar way:
<a href="wtai://wp/mc;1234567">Call me now!</a>

Of course, if you are advertising a service or a product worldwide and you are using click to call link, it is always good to put a full phone number (country code prefix) and make am end to end test in order to make sure the number really makes it to its destination.