Wednesday, 2 January 2013

how to get week number for a date based on month from the date in sql server

 
We have a date column in a table and I want to get week number for that particular date based on the month from that date irrespective of the day

For example:
01-dec-2012 to 07-dec-2012 should give week number as 108-dec-2012 to 14-dec-2012 should give week number as 215-dec-2012 to 21-dec-2012 should give week number as 3
 
 
You can use DAY (Transact-SQL)  
 
select ((day(ColumnName)-1) / 7) + 1 as W
 
Example :
 
select ((day('2012-11-01 00:00:00.000')-1) / 7) + 1 as W
 
Output :
 
W
--
1

Tuesday, 1 January 2013

Dictionary Class Demo in C#

The Dictionary<TKey, TValue> generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the Dictionary<TKey, TValue> class is implemented as a hash table.


As long as an object is used as a key in the Dictionary<TKey, TValue>, it must not change in any way that affects its hash value. Every key in a Dictionary<TKey, TValue> must be unique according to the dictionary's equality comparer. A key cannot be null, but a value can be, if the value type TValue is a reference type.
Dictionary<TKey, TValue> requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer<T> generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer<T>.Default is used. If type TKey implements the System.IEquatable<T> generic interface, the default equality comparer uses that implementation.


C# :

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DictionaryDemoWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        Dictionary<string, string[]> countryList = new Dictionary<string, string[]>();
        protected void Page_Load(object sender, EventArgs e)
        {
            countryList["India"] = new string[] { "Pune", "Mumbai" };
            countryList["USA"] = new string[] { "California", "Boston" };
            countryList["Germany"] = new string[] { "Hamberg", "Berlin" };
            if (!IsPostBack)
            {
               
                foreach (string CountryKey in countryList.Keys)
                {
                    ddlCountry.Items.Add(CountryKey);
                }
            }
        }
        protected void ddlUniv_SelectedIndexChanged(object sender, EventArgs e)
        {              
        }      
        protected void countrycomboBox_SelectedIndexChanged(object sender,EventArgs e)
        {
            string selectedCountry = ddlCountry.SelectedItem.Value.ToString();      
            string[] sel = countryList[ddlCountry.SelectedItem.Text];
            ddlUniv.DataSource = sel;
            ddlUniv.DataBind();      
        }
    }
}


aspx :

<asp:Label ID ="lblCountry" runat ="server"></asp:Label>
    <asp:DropDownList ID ="ddlCountry" runat="server" OnSelectedIndexChanged="countrycomboBox_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
    <br />
    <br />
    <asp:Label ID ="Label1" runat ="server"></asp:Label>
    <asp:DropDownList ID ="ddlUniv" runat="server"
            onselectedindexchanged="ddlUniv_SelectedIndexChanged"></asp:DropDownList>