How to split comma separate string in Sql server

This is common requirement for almost every developer to extract data from comma separated string. In Sql Server 2016, we have STRING_SPLIT  inbuilt function to extract data from string. But for people those are using prior versions of Sql Server like me 😥 , still have to strugle with this issue. If you Google about this function, you’ll find out more than 10 ways of extrating data from comma separted string. Here, I present my own version 🙂

--SELECT * FROM DBO.StringSplitter('Sunil,Sanjay,Neha,Aradhaya,Nikhil',',')
--SELECT * FROM DBO.StringSplitter('Sunil',',')
CREATE FUNCTION StringSplitter
(
	@STR VARCHAR(100), -- Input string 
	@SEPARATOR VARCHAR(1) -- Separator character 
)
RETURNS @TEMP TABLE (NAME VARCHAR(100)) -- Return data in tabular format 
AS
BEGIN
DECLARE @START INT= 1, @STRLENGTH INT = LEN(@STR);
-- First check whether input string is comma separater string or not.
-- If NOT then insert the whole string into our table variable and 
-- return. 
IF(CHARINDEX(',', @STR, @START) = 0)
 BEGIN
  INSERT INTO @TEMP SELECT @STR
 END
-- If input string is a comma separated string then go in and extract data 
ELSE
 BEGIN
  -- Before loop over the input string first add a ',' (comma)
  -- at the end of the input string because if we don't add
  -- then last comma separated word will be missed.
  SET @STR += ',';
  -- loop till @START variable is smaller than length of the input string
  WHILE(@START < @STRLENGTH)
   BEGIN
        -- get the substring from begining of the input string till it finds a
	-- comma. Once you find a comma set this value to your @START variable
	-- by increasing it's value by 1 because next search should start from
	-- next character after found comma 
	INSERT INTO @TEMP 
	SELECT SUBSTRING(@STR, @START, CHARINDEX(',', @STR, @START) - @START)
	SET @START = CHARINDEX(',', @STR, @START) + 1
   END
END
RETURN;
END

Alert once Page Load is completed in Asp.Net

My friend asked me to alert user once page load is done. There is a simple way to do so, just write a JavaScript function and call it on “onload” of body tag. “onload” will be triggered after all the images and associated resources are loaded completely, that means after whole page load and that’s what we want to do. See below code, how to do it:

xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<title>Test Page</title>
<script type=”text/javascript”>
function onLoadClicked() {
alert(‘Page load is done.’);
}
</script>
</head>
onLoadClicked()”>
<!– HTML CODE –>
</body>
</html>

lastDigit

Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % “mod” operator computes remainders, so 17 % 10 is 7.

lastDigit(7, 17) ? true
lastDigit(6, 17) ? false
lastDigit(3, 113) ? true

public boolean lastDigit(int a, int b) {
if(a%10 == b%10) return true;
else return false;
}

intMax

Given three int values, A B C, return the largest.

intMax(1, 2, 3) ? 3
intMax(1, 3, 2) ? 3
intMax(3, 2, 1) ? 3

public int intMax(int a, int b, int c) {
if(a>b)
if(a>c) return a;
else return c;
else if(b>c) return b;
else return c;
}

front22

Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so “kitten” yields”kikittenki”. If the string length is less than 2, use whatever chars are there.

front22(“kitten”) ? “kikittenki”
front22(“Ha”) ? “HaHaHa”
front22(“abc”) ? “ababcab”

public String front22(String str) {
int n=str.length();
if(n<2) return(str+str+str);
else return( str.substring(0,2)+str+str.substring(0,2));
}

Write a program for sleepIn problem.

The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we’re on vacation. Return true if we sleep in.

sleepIn(false, false) ->true
sleepIn(true, false) ->false
sleepIn(false, true) ->true

public boolean sleepIn(boolean weekday, boolean vacation) {
if((weekday != true)||(vacation ==true)) return (true);
else return (false);

}

Write a program that replace two or more blank spaces by single blank.

e.g. ” i    hate   my          books”
output:: “i hate my books”

#include<stdio.h>

void blank(char *str)
{
int i=0;
char *temp,timepass[100];
temp=timepass;

while(*str!='')
{ if(*str !=' ')
{ *temp=*str;
temp++;
}
else
{ if(*(str+1) != ' ')
{ *temp=*str;
temp++;
}
}

str++;
}

*temp='';
puts(" ");
printf("\n%s",timepass);
puts(" ");
}

main()
{
char str[100];
puts("Enter a string:: ");
gets(str);
blank(str);
}

Write a program to find total number of +ve, -ve, odd and even numbes in an array.

#include<stdio.h>
numbers(int *arr, int n)
{ int i,a=0,b=0,c=0,d=0;
while(n>0)
{ if(*arr < 0) a++;
if(*arr > 0) b++;
if((*arr % 2)==0) c++;
if((*arr % 2)!=0) d++;
n--;
arr++;
}

printf("\nNumber of +ve numbers:: %d\n",b);
printf("\nNumber of -ve numbers:: %d\n",a);
printf("\nNumber of even numbers:: %d\n",c);
printf("\nNumber of odd numbers:: %d\n",d);
}

main()
{
int n, a[100],i=0;
printf("\nEnter the dimension of array::");
scanf("%d",&n);
printf("\nEnter the elments of array:: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
numbers(a,n);
}

Write a program that search a number in an array and also display number of times it appears.

#include<stdio.h>
search(int * arr, int n, int m)
{

int l=0;
while(n>0)
{ if(m== *arr)
l++;
n--;
arr++;

}
return l;
}

main()
{ int n, m,i=0,a[100],l;
printf("\nEnter the dimension of array:: ");
scanf("%d",&n);
printf("\nEnter the elments of array:: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnte the number you want to search:: ");
scanf("%d",&m);
l=search(a,n,m);
printf("\nArray:: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
if(l!=0)
printf("\nThe number is found and %d times it appears in array\n", l);
else printf("\n number not found!!!\n");
}