Search This Blog

Wednesday, July 25, 2012

CHARINDEX (SQL Server)

SQL Server > Built-in Functions > CHARINDEX

CHARINDEX SQL Server function searches an expression for another expression and returns its starting position if found.

Example

declare @t nvarchar(max)
set @t = 'blue; red; green'

SELECT CHARINDEX(';', @t);

Result
(No column name)

5

SELECT CHARINDEX(':', @t);
Result
(No column name)
0








Tuesday, July 24, 2012

timestamp (Transact-SQL)

SQL Server > Data types > TimeStamp

Timestamp in SQL Server is a data type that exposes automatically generated, unique binary numbers within a database.

  • is generally used as a mechanism for version-stamping table rows. 
  • the storage size is 8 bytes. 
  • the timestamp data type is just an increment number and does not preserve a date or a time. 
  • to record a date or time, use a datetime data type.


Example

IF OBJECT_ID(N'tbl1', N'U') IS NOT NULL
       DROP TABLE tbl1;
GO

CREATE TABLE tbl1
(
       id     int,
       code   timestamp
);
GO

INSERT INTO tbl1(id) values  (1)
SELECT
       *
FROM
       tbl1

Result:

id code
1  0x00000000005C0E69







Friday, July 20, 2012

Update table join SQL Server

SQL Server > Update > Using Join


UPDATE
  tbl1 AS A
  INNER JOIN tbl2 AS B ON A.F1=B.F1
SET

  A.X = 1,
  A.Y = 2
WHERE
  A.id = 100





Wednesday, July 18, 2012

nvarchar data type SQL Server

SQL Server > Data Types > nvarchar


  • variable length 
  • Unicode string data.
  • length can be a value from 1 through 4,000.
  • maximum storage size is 2 GB
Example


CREATE TABLE dbo.Person
(
   Name nvarchar(100)
);








Monday, July 16, 2012

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

ASP.NET > Errors

This error normally occurs when we use Response.Redirect or Server.Transfer or Response.End in your code before completing the actual process the page was doing.

In this case what you have to do is

In place of Response.End you have to use HttpContext.Current.ApplicationInstance.CompleteRequest

in place of Response.Redirect, use the Response.Redirect ("home.aspx", false)
in place of Server.Transfer, use the Server.Execute method





Monday, July 9, 2012

DATEADD SQL Server Example

SQL Server > Built-in Functions > DATEADD

Add a number to a specified datepart of that date.

DATEADD (datepart , number , date )

datepart: is the part of date to which an integer number is added.

Example:

SELECT DATEADD(month,1, GetDate()) -- add 1 month to current day
SELECT DATEADD(day,1, GetDate()) -- add 1 month to current day
SELECT DATEADD(year,1, GetDate()) -- -- add 1 year to current day


Find last day of previous month








Monday, June 25, 2012

ref c#

C# Keywords > ref

The ref method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method.


using System;
public class MyClass
{
   public static void Ref(ref char x)
   {
      // The value of i will be changed in the calling method
      x = 'a';
   }
   public static void NoRef(char x)
   {
      // The value of i will be unchanged in the calling method
     x = 'b';
   }
   public static void Main()
   {
      char i = '';    // variable must be initialized
      Ref(ref i);  // the arg must be passed as ref
      Console.WriteLine(i);
      NoRef(i);
      Console.WriteLine(i);
   }
}