Monday, January 10, 2011

String reversal, without using String operations

The below code will perform of the reverse of the string without using any string methods.
private void reverseString()
    {
    string str = "SANTHOSH";
    int i = 0;
    foreach (char c in str)
      {
      i++;
      }
    for (int j = i - 1; j >= 0; j--)
      {
      Response.Write(str[j]);
      }
    }
Result : HSOHTNAS

SQL Linked Server

Link server will be useful to establish the Link between to servers and retrieve the data.
creating the link server can be done in two methods.
1. Use sp_addlinkserver
2. Open Sql server Management Studio go to Server Objects (see below)
Give the ServerName, Product Name, DataSource (see below)
Go to Security settings and enter the credentials to use the Database(see below).
Then click on OK this will create the Linked server.
Using the linked server we can use the linked server using OPENQUERY:
SELECT * INTO dbo.testlinkserver
FROM OPENQUERY(LOCALSERVER, 'SELECT * FROM dbo.testlinkserver' )

In the above Query LOCALSERVER is the Linked server Name testlinkserver is Table Name.
Result of the above Query is it will create the table called testlinkserver and dump the Data from Data Source which you are linked to.

Friday, January 7, 2011

SQL Query, to get the Second Highest Salary

SELECT MAX(Salary) FROM dbo.Employee WHERE Salary <> (SELECT MAX(Salary) FROM Employee)

Wednesday, January 5, 2011

SQL scalar function example to get the projects for a specific date

CREATE FUNCTION fnGetProjects(@FromDate DATETIME)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @ProjectsList VARCHAR(MAX)
DECLARE @name VARCHAR(1000)
DECLARE db_cursor CURSOR FOR
SELECT DISTINCT PT.ProjectName FROM dbo.ProjectDetailsInfo WHERE EnteredDate = @FromDate
OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0  
BEGIN
  SET @ProjectsList = COALESCE(@ProjectsList + ', ', ' ') + @name
FETCH NEXT FROM db_cursor INTO @name  
END
CLOSE db_cursor  
DEALLOCATE db_cursor        
RETURN @ProjectsList
END

Calling the above function to get the Result:

SELECT dbo.fnGetProjects('December 06,2010')

RESULT :
CI Notes, Disruptions, Email cleanup

Note: I have used the CURSOR in the above example
to get the distinct Projects and seperate them with the comma.