The efficient way to generate 4 or 8 characters long random alphanumeric string in SQL

SELECT LEFT(CONVERT(VARCHAR(36),NEWID()),4)+RIGHT(CONVERT(VARCHAR(36),NEWID()),4)

SELECT RIGHT(REPLACE(CONVERT(VARCHAR(36),NEWID()),’-‘,”),8)

DECLARE @chars NCHAR(36)
SET @chars = N’0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ’
DECLARE @result NCHAR(5)
SET @result = SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
SELECT @result

 

In order to ensure uniqueness for each varchar you can store the results in a table and compare with result in that table. You can also make the varchar longer and just hope for the best.

Happy Coding :)

Retrieve First Name and Last Name from string in asp.net

Retrieve First Name and Last Name from string in asp.net

VB.Net:

Dim name As String = “Joseph Van Andrews”

‘ Split string based on spaces
Dim names As String() = name.Split(New Char() {” “c})

‘ Seperate the first name from the rest of the string
Dim lastName as string = name.substring(names(0).length())

Dim nameString as string = “the First Name is: ” + names(0) + ” and the Last Name is: ” + lastName

response.write(nameString)

C#.Net:

string name = “ABC DEF XYZ”;

// Split string based on spaces
string[] names = name.Split(new char[] { ‘ ‘ });

// Seperate the first name from the rest of the string
string lastName = name.Substring(names[0].Length());

string nameString = “the First Name is: ” + names[0] + ” and the Last Name is: ” + lastName;

response.write(nameString);

Happy Coding :)

Jquery Redirect to Another Page After few seconds or Some Time Delay in Asp.net

Introduction:

Here I will explain how to redirect to another page after 5 seconds or some time delay in jQuery.

Description
   
In previous articles I explained calculate age with years months and days using jqueryexpanding collapsing sliding div jquery and many articles relating to JQuery and JavaScript. Now I will explain how to redirect to another page after 5 seconds or some time delay injQuery.

To implement this functionality we need to write the following code:
 

<head runat=”server”>

<title>JavaScript redirect to another page after 3 seconds or Sometime</title>
<script type=”text/javascript”
src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js“>
</script>
<script type=”text/javascript”>
var i = 0;
$(document).ready(function () {
ShowRedirectTime();
})
function ShowRedirectTime() {
var dt = new Date();
var redirecturl = ‘http://www.nipulparikh.com‘;

document.getElementById(“lblRemaingTime”).innerHTML = 3 – i + ”
Seconds”;
i++;
if (i == 3) {
setTimeout(“location.href=redirecturl”, 0);
}
window.setTimeout(“ShowRedirectTime()”, 1000);
}
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div style=”text-align: center;”>
This Page will Redirect to Another Page within:
<label id=”lblRemaingTime” style=”font-weight: bold; font-size: 12pt”>
</label>
</div>
</form>
</body>
</html>

 

Happy Coding..!!! :)

Calculate age with years, months, and days using jquery

In this article we take birth date and using the “GetDate” function to find the years, months and days. So let’s have a look at a practical example of how to calculate age on client side using Javascript.

Date Format: MM/DD/YYYY

Write down below function in head tag.

Code:

function getAge(dateString) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());

var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate();

var dob = new Date(dateString.substring(6,10),
dateString.substring(0,2)-1,
dateString.substring(3,5)
);

var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var age = {};
var ageString = “”;
var yearString = “”;
var monthString = “”;
var dayString = “”;
yearAge = yearNow – yearDob;

if (monthNow >= monthDob)
var monthAge = monthNow – monthDob;
else {
yearAge–;
var monthAge = 12 + monthNow -monthDob;
}

if (dateNow >= dateDob)
var dateAge = dateNow – dateDob;
else {
monthAge–;
var dateAge = 31 + dateNow – dateDob;

if (monthAge < 0) {
monthAge = 11;
yearAge–;
}
}

age = {
years: yearAge,
months: monthAge,
days: dateAge
};

if ( age.years > 1 ) yearString = ” years”;
else yearString = ” year”;
if ( age.months> 1 ) monthString = ” months”;
else monthString = ” month”;
if ( age.days > 1 ) dayString = ” days”;
else dayString = ” day”;

if ( (age.years > 0) && (age.months > 0) && (age.days > 0) )
ageString = age.years + yearString + “, ” + age.months + monthString + “, and ” + age.days + dayString + ” old.”;
else if ( (age.years == 0) && (age.months == 0) && (age.days > 0) )
ageString = “Only ” + age.days + dayString + ” old!”;
else if ( (age.years > 0) && (age.months == 0) && (age.days == 0) )
ageString = age.years + yearString + ” old. Happy Birthday!!”;
else if ( (age.years > 0) && (age.months > 0) && (age.days == 0) )
ageString = age.years + yearString + ” and ” + age.months + monthString + ” old.”;
else if ( (age.years == 0) && (age.months > 0) && (age.days > 0) )
ageString = age.months + monthString + ” and ” + age.days + dayString + ” old.”;
else if ( (age.years > 0) && (age.months == 0) && (age.days > 0) )
ageString = age.years + yearString + ” and ” + age.days + dayString + ” old.”;
else if ( (age.years == 0) && (age.months > 0) && (age.days == 0) )
ageString = age.months + monthString + ” old.”;
else ageString = “Oops! Could not calculate age!”;

return ageString;
}

Example:

alert(getAge(’04/06/1988′));

 

Happy Coding :)