Monday, May 28, 2012

Pivot and Unpivot in Sql server


CREATE TABLE #tblMergeOutput
(
    ID                                BIGINT IDENTITY(1,1)
    , MergeAction                    NVARCHAR(100)
    , Source_barclay_id                BIGINT NULL
    , Source_name                    VARCHAR(200) NULL
    , Source_Address                VARCHAR(200) NULL
    , Target_barclay_id                BIGINT NULL
    , Target_name                    VARCHAR(200) NULL
    , Target_Address                VARCHAR(200) NULL
)

INSERT INTO #tblMergeOutput VALUES ('UPDATE', 1, 'Name1', 'Address1', 1, 'Name11', 'Address11')
INSERT INTO #tblMergeOutput VALUES ('UPDATE', 2, 'Name2', 'Address2', 2, 'Name22', 'Address22')

SELECT * FROM #tblMergeOutput

SELECT
*
FROM
(
    SELECT
    *
    FROM
    (
        SELECT
        *
        FROM
        (
            SELECT
                ID
                , MergeAction
                , 'OldValue' AS RecordType
                , Source_barclay_id AS barclay_id
                , CAST( Source_name AS VARCHAR(MAX) ) AS Name
                , CAST( Source_Address AS VARCHAR(MAX) ) AS Address
            FROM #tblMergeOutput
           
            UNION ALL
           
            SELECT
                ID
                , MergeAction
                , 'NewValue' AS RecordType
                , Target_barclay_id AS barclay_id
                , CAST( Target_name AS VARCHAR(MAX) ) AS Name
                , CAST( Target_Address AS VARCHAR(MAX) ) AS Address
            FROM #tblMergeOutput
        ) AS Data
        UNPIVOT( [Value] For [FieldName] IN ( [Name], [Address] ) ) AS upvt
    ) AS Data
    PIVOT( MIN ( [Value]) FOR [RecordType] IN ([OldValue], [NewValue]) ) AS pvt
) AS Data
WHERE OldValue != NewValue

DROP TABLE #tblMergeOutput

Tuesday, March 6, 2012

Convert Seconds to HH:MM:SS Format:


protected void Page_Load(object sender, EventArgs e)
    {
int seconds = 10000;
       string Time = ConvertToHHMMSS1(seconds);
       //string Time = ConvertToHHMMSS2(seconds);
lblImpCurTimePerday.Text = Time;

    }

private string ConvertToHHMMSS1(int seconds)
    {
        TimeSpan ts = new TimeSpan(0, 0, 0, seconds, 0);
        int Hours = (int)ts.TotalHours;

        return string.Format("{0}:{1}:{2}", Hours, ts.Minutes, ts.Seconds);
    }

private string ConvertToHHMMSS2(int seconds)
  {
    return string.Format("{0:00}:{1:00}:{2:00}", seconds / 3600, (seconds / 60) % 60, seconds % 60);
  }

Sunday, March 4, 2012

C# string formatting


Numeric Format Specifiers
Specifier
Description
Example
C#
c
Currency; specify the number of decimal places
 $12,345.00
string.Format
("Currency: {0:c}", iNbr)
d
Whole numbers; specifies the minimum number of digits - zeroes will be used to pad the result
 12345
string.Format
("Whole: {0:d}", iNbr)
e
Scientific notation; specifies the number of decimal places
 1.2345e+004
string.Format
("Exponential: {0:e}", iNbr)
f
Fixed-point; specifies the number of decimal places
 12345.00
string.Format
("Fixed: {0:f3}", iNbr)
n
Fixed-point with comma separators; specifies the number of decimal places
 12,345.00
string.Format
("Fixed formatted: {0:n3}", iNbr)
p
percentage; specifies the number of decimal places
 1,234,500.00%
string.Format
("Percentage: {0:p2}", iNbr)
x
Hexadecimal
 3039
string.Format
("Hexadecimal: {0:x}", iNbr)

Example :
private string ConvertToPercent(string textToConvert)
  {
    return String.Format("{0:P}", Convert.ToDecimal(textToConvert));
  }

private string CovertToInteger(string textToCovert)
  {
    return String.Format("{0:n0}", Convert.ToInt32(textToCovert));
  }

private string ConvertToDecimal(string textToCovert)
  {
    return String.Format("{0:f0}", Convert.ToDecimal(textToCovert));
  }

 private string ConvertToCurrency(string textToConvert)
  {
    return String.Format("{0:c}", Convert.ToDecimal(textToConvert));

Friday, March 2, 2012

set the color of text that is in span in HTML in jquery

var color = 'red';
function fnSetColor(loopname, color) {
                $("div[id*='lstSelectedLoop'] ul li span").each(function () {
                    if ($(this).html() == loopname) {
                        $(this).css("color", color);
                    }
                })
            }

Call javascript from server side

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "SetHiddenAdSegmentDuration", "SetHiddenAdSegmentDuration('" + SumAdSegmentDuration + "');", true);

Wednesday, February 29, 2012

Hide particular column into Grid on special condition

In Aspx :
----------

<telerik:GridBoundColumn DataField="LastName" UniqueName="LastName"
                                                                                                  HeaderText="Last Name" HeaderStyle-HorizontalAlign="Right">
                                                                                                    <ItemStyle HorizontalAlign="Right" />
                                                                                                </telerik:GridBoundColumn>



In Aspx.cs
-------------
protected void Page_Load(object sender, EventArgs e)
        {
HideColumn("LastName "); //-- Column Name
}


private void HideColumn(string uniqueName)
        {
            GridColumn gridCol = dgSiteList.Columns.FindByUniqueNameSafe(uniqueName);
            if (gridCol != null)
                gridCol.Visible = false;
        }