Convert comma separated numbers in tabular form in SQL Server

Scenerio : we have coma sperated data and want it in tabular form

Input: @strinput Varchar=’1,2,3,4′

Output :

download

Solution

Create a function as below

CREATE FUNCTION [dbo].[SplitString]
(
@Input NVARCHAR(MAX),
@Character CHAR(1)
)
RETURNS @Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE @StartIndex INT, @EndIndex INT

SET @StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) – 1, LEN(@Input)) <> @Character
BEGIN
SET @Input = @Input + @Character
END

WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Character, @Input)

INSERT INTO @Output(Item)
SELECT SUBSTRING(@Input, @StartIndex, @EndIndex – 1)

SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END

RETURN
END

Now use below statement to get desired output –

Select * from [dbo].[SplitString](‘1,2,3′,’,’)

 

Null-Conditional Operator in c# 6 and above

By using C#6 we can make better visibility and reduce code line.one of the feature to handle null is shown below …

Old way to handle null
public static string getEmpName(Employee emp)
{
String result;
if (emp != null)
{
result = emp.name;
}
return result;
}

New way to handle null

public static string getEmpName(Employee emp)
{
String result;
if (emp != null)
{
result = emp?.name;
}
return result;
}

If we want to return a default value in case of null we have to use ?? Operator as below

public static string getEmpName(Employee emp)
{
String result;
if (emp != null)
{
result = emp?.name ?? “NA”;
}
return result;
}

 

Difference in Interface and Abstract class in c#

Interface :  An interface is like a contract which is followed by the class that implemented interface, it can have property,method,delegate or event. An interface just have declaration has no definition. Interface members are public by default,

Abstract Class : A class declared as abstract keyword.

It can not be instantiated because it is incompleted.

Abstract Class can only be used as a base class.

Any class inheriting an Abstract class must provide implementation for abstract members. Note– If child class do not want implementation for all abstract method mark child class as abstract.

Differences :

1) Abstract class can have definition and interface can’t have.

2)Abstract class can have access modifier and interface can’t have.

3)Abstract class can have fields and interface can’t have.

4) Abstract class can inherit an interface or another abstract class but interface can inherit in an interface.

Use of Tuple in c#

Scenario : I have a table want to get only n number of column(2 or 3 or so on) and I don’t want create a model i.e class to store that only data,Here I need tuple

 

List<Tuple<int,string>> data =db.Resource.Select(x => new { x.Resource_ID, x.Name }).AsEnumerable().Select(x => Tuple.Create(x.Resource_ID, x.Name)).ToList();

Ajax Call for Every Minute to Server

Scenerio : Here we needs to call a a function to server on every minute,we can use use below code.

$(document).ready(function () {
snooze();
})
function snooze() {
$.ajax({
type: “GET”,
url: ‘URL’,
success: function (data) {
},
complete: function () {
// Schedule the next request when the current one’s complete
setTimeout(snooze, 10000);
}, error: function (xhr) {
},
});

TypeError: $.browser is undefined

Just put below the $.browser code in your js

var matched, browser;

jQuery.uaMatch = function (ua) {
ua = ua.toLowerCase();

var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
ua.indexOf(“compatible”) < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
[];

return {
browser: match[1] || “”,
version: match[2] || “0”
};
};

matched = jQuery.uaMatch(navigator.userAgent);
browser = {};

if (matched.browser) {
browser[matched.browser] = true;
browser.version = matched.version;
}

// Chrome is Webkit, but Webkit is also Safari.
if (browser.chrome) {
browser.webkit = true;
} else if (browser.webkit) {
browser.safari = true;
}

jQuery.browser = browser;