2013年11月21日 星期四

[T-SQL] DATEDIFF函式

說明:想在T-SQL求date欄位與系統日的相差天數。

作法:
DATEDIFF ( datepart , startdate , enddate ),其中datepart表回傳相差的時間格式。

例:
SELECT DATEDIFF(d, Test_date, GETDATE())
FROM Test_Table
WHERE Test_No = '1';


參考資料:
http://technet.microsoft.com/en-us/library/ms189794.aspx

2013年11月20日 星期三

[Oracle] 使用SPOOL匯出script


SET PAGESIZE 1000 --設定每頁行數
SET LINESIZE 1000 --設定每行長度
SET HEADING ON --顯示欄位表頭
SET ECHO ON --顯示SQL Command
SPOOL C:\test.log;

--SQL Command
SELECT sysdate FROM dual;

SPOOL OFF;

2013年11月17日 星期日

[C#] 兩個日期算相差月數與日數

說明:兩個日期字串算相差幾個月與幾天

作法:
public static int GetTimeDiff(string strFrom, string strTo, string strType)
    {
        DateTime dtStart = DateTime.Parse(strFrom);
        DateTime dtEnd = DateTime.Parse(strTo);

        if (strType == "D")
        {
            //使用TimeSpan提供的Days屬性
            TimeSpan ts = (dtEnd - dtStart);
            int iDays = ts.Days + 1;
            return iDays;
        }
        else if (strType == "M")
        {
            int iMonths = dtEnd.Year * 12 + dtEnd.Month - (dtStart.Year * 12 + dtStart.Month) + 1;
            return iMonths;
        }
        else return 0;
    }


參考資料:
http://www.haogongju.net/art/1125129

2013年11月13日 星期三

[ASP.NET] CheckBoxList取得多選之Value

說明:使用CheckBoxList,該如何取得多選之Value?使用SelectedValue只抓得到index最小的Value。

作法1-使用迴圈找Selected的Item:
code behind
StringBuilder sbTest = new StringBuilder();

 for (int i = 0; i < cblTest.Items.Count; i++)
{
    if (cblTest.Items[i].Selected)
    {
        sbTest.Append(cblTest.Items[i].Value.Trim() + ", ");
    }
}
sbTest = sbTest.Remove(sbTest.Length - 2, 2);


作法2-覆寫CheckBoxList控制項的SelectedValue方法:
請參考IN91大文章:
http://www.dotblogs.com.tw/hatelove/archive/2009/01/13/6785.aspx


參考資料:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20100319135604VSG
http://www.dotblogs.com.tw/hatelove/archive/2009/01/13/6785.aspx

2013年11月4日 星期一

[ASP.NET] 解決DropDownList內ListItem Value重複的問題

說明:在設計DropDownList的清單項目時,一般是不會有Value相同的情況,如果有重複,DropDownList會傳回Index最前面的那筆ItemList,例:

<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="True">
        <asp:ListItem Value="0">請選擇</asp:ListItem>
        <asp:ListItem Value="1">台北</asp:ListItem>
        <asp:ListItem Value="1">新北</asp:ListItem>
        <asp:ListItem Value="2">桃園</asp:ListItem>
        <asp:ListItem Value="2">新竹</asp:ListItem>
</asp:DropDownList>

點選新北,此時DropDownList卻顯示台北,而且無法觸發 SelectedIndexChanged 事件。


作法:參考ASP.NET魔法學院的作法,override DropDownList的方法,將VB版本改為C#:
1. 新增一專案,在類別內加入System.Web.UI.WebControls參考

namespace WebControls
{
    public class CDropDownList: DropDownList
    {
        //將DropDownList的selectedIndex也PostBack回Server端
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            bool blAutoPostBack = false;
            string strScript = string.Empty;

            blAutoPostBack = this.AutoPostBack;
            this.AutoPostBack = false;
            base.AddAttributesToRender(writer);

            if (blAutoPostBack)
            {
                base.Attributes.Remove("onchange");
                strScript = String.Format("__doPostBack('{0}',{1})", this.ClientID, "this.selectedIndex");
                writer.AddAttribute(HtmlTextWriterAttribute.Onchange, strScript);
                this.AutoPostBack = true;
            }
        }

 //判斷PostBack的selectedIndex與原selectedIndex是否相同,不同則觸發SelectedIndexChanged 事件
        protected override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            string[] strValues = new string[35]; //可自己定義陣列大小
            int iSelectedIndex = 0;

            this.EnsureDataBound();
            strValues = postCollection.GetValues(postDataKey);

            if (strValues != null && Page.Request.Form["__EVENTARGUMENT"].ToString() != "")
            {
                iSelectedIndex = Convert.ToInt32(this.Page.Request.Form["__EVENTARGUMENT"]);
            }

            if (this.SelectedIndex != iSelectedIndex)
            {
                base.SetPostDataSelection(iSelectedIndex);
                return true;
            }
            return false;
        }
    }
}

2. 在web專案加入此專案建置後的dll,在工具箱->選擇項目->加入該元件即可,可自訂前置詞(TagPrefix),定義控制項所在命名空間的別名,預設為cc1。


參考資料:

2013年11月3日 星期日

[Excel] VLOOKUP說明與example

說明:做資料比對時,有時會出現兩個工作表需要比對或合併的情形,例:
sheet 1(全員工)
工號 部門代號
001   A01
002   B01
...
999   Z01

sheet 2(組織調整某些員工)
工號 新部門代號
002   U01
008   Z01
....
999   C01

要在sheet 1合併全公司員工的新部門代號,由於非全部員工都有異動,因此sheet 2只有部門調整的員工資料,資料少時還可以一筆一筆對,但資料一多,該如何處理?


作法:有對應的key值,可利用VLOOKUP函數來處理,此函數有四個引數,語法:
VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])

lookup_value:表示要主頁面參照的key值,如以工號為準尋找該員工的資料。注意:該值需在table_array最前面,或第一欄,否則顯示#N/A。
table_array:表示要參照的一系列資料,如sheet 2(有工號與新部門代號)
col_index_num:表Excel要傳回參照的那一欄資料(從1開始),如sheet 2有兩欄,要新部門代號所以填2。
[range_lookup]:非必要,bool值,填0 (false)表完全符合,不填或非0則為true,表大約符合,一般填0。

因此,在sheet 1拉一個新欄位打上VLOOKUP(sheet1A:A, sheet2!A:B, 2, 0)即可顯示新工號的欄位。

P.S. HLOOKUP可用來尋找列資料。


參考資料:
http://felin0630.pixnet.net/blog/post/24888627-%E2%96%8Cexcel%E3%80%82%E5%87%BD%E6%95%B8-%E2%96%8Cexcel%E8%B3%87%E6%96%99%E6%AF%94%E5%B0%8D%E5%B0%8F%E6%8A%80%E5%B7%A7(excel%E5%87%BD%E6%95%B8-
Excel說明