2015年7月24日 星期五

[T-SQL] 互相轉換TimeStamp yyyyMMddhh24mmss與datetime做日期操作

說明:
1. 如何將yyyyMMddhh24mmss的字串在T-SQL中轉型為DateTime格式?
2. 如何將DateTime格式轉為yyyyMMddhh24mmss?

做法:
1. SELECT CAST(SUBSTRING(timestamp, 1, 8)+' '+SUBSTRING(timestamp, 9, 2)+':'+SUBSTRING(timestamp, 11, 2)+':'+SUBSTRING(timestamp, 13, 2) AS datetime)

轉型為DateTime後,即可使用DateAdd函式做處理,例:
DateAdd(hour, -2, datetime)

2. SELECT CONVERT(varchar(20), GETDATE(), 112) + REPLACE(CONVERT(varchar(8), GETDATE(), 108),':','');


參考資料:
https://jerry2yang.wordpress.com/2012/07/04/sql-%E5%AD%97%E4%B8%B2%E6%A8%A3%E5%BC%8F%E8%BD%89%E6%8F%9B%E7%82%BA%E6%97%A5%E6%9C%9F%E6%A0%BC%E5%BC%8F/
https://msdn.microsoft.com/zh-tw/library/ms186819(v=sql.120).aspx

[C#] 使用LINQ操作DataTable撈出Top N資料

說明:
使用LINQ找出DataTable內依欄位值最大的N筆資料


做法:
var TopN = (from row in dt.AsEnumerable() select row).OrderByDescending(x => x.Field<string>("Column")).Take(N);



參考資料:
http://stackoverflow.com/questions/5344805/linq-orderby-descending-query
http://www.dotblogs.com.tw/yc421206/archive/2014/07/14/145944.aspx
https://msdn.microsoft.com/zh-tw/library/bb552415(v=vs.110).aspx
http://stackoverflow.com/questions/10855/linq-query-on-a-datatable

[T-SQL] 如何產出0~0.9之間的亂數

說明:
如何產出0~0.9(小數位數1位)之間的亂數。


語法:
SELECT CAST(CAST(NEWID() AS binary(1))%10 AS float)/10

其中NEWID()會產生GUID(32個16進位),將之轉型為1 byte,因此值會在0-255之間,
mod 10後,值會在0-9之間,再除以10,值就會跑在0-0.9之間。


參考資料:
https://msdn.microsoft.com/zh-tw/library/ms190348(v=sql.120).aspx
http://caryhsu.blogspot.tw/2012/08/rand.html

2015年6月4日 星期四

[IOS/Swift] how to post data to RESTful WebService?

說明:
如何透過RESTful WebService來更新後台的資料?


語法:
var strURL = WS URL;
var request = NSMutableURLRequest(URL: NSURL(string: strURL));
request.HTTPMethod = "POST";
        
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
            
    if (response != nil)
    {
        var strResponse = NSString(data: data, encoding: NSUTF8StringEncoding);
        //replace the '\' of strResponse
        print(strResponse.stringByReplacingOccurrencesOfString("\"", withString: ""));
    }
}

2015年6月3日 星期三

[C#/Excel PIA] Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a The server process could not be started because the configured identity is incorrect. Check the username and password.

說明:
使用Excel PIA寫schedule job產出Excel,程式放在64 bits server上,執行出現該錯誤。


解法:
出現此錯誤,主要是COM元件的存取帳戶被設為Interactive,
因此程式執行時,需要有帳戶登入該server。看過網路上的解法,
打開Administrative Tools下的component service -> Computers -> DCOM Config,
找到Microsoft Excel Application或{00024500-0000-0000-C000-000000000046},
右鍵Properties,找到Security tab或identity tab,將帳戶加進去,
但我找不到Excel Application,試過在command line下mmc comexp.msc /32等方法,還是沒顯示,最後是在登錄檔,HKEY_CLASSES_ROOT\AppID\{00020812-0000-0000-C000-000000000046},將某個屬性值的Interactive改掉即可。


參考資料:
https://ovaismehboob.wordpress.com/2013/07/06/production-deployment-issues-when-asp-net-web-application-access-com-component/
http://blogs.technet.com/b/the_microsoft_excel_support_team_blog/archive/2012/11/12/microsoft-excel-does-not-appear-in-dcom-configuration-snap-in.aspx
https://msdn.microsoft.com/en-us/library/ms678426(VS.85).aspx
http://www.cnblogs.com/fengjunkuan/

2015年4月11日 星期六

[IOS/Swift] how to parse json from the webservice

說明:
json可以拆解成Array,以[]表示,例:[{"id": "#123", "name": "Apple"},{"id": "#321", "name": "Google"}]。
另一種是Object,以{"key": "value"}表示,如http://date.jsontest.com/提供的:
{
   "time": "03:53:25 AM",
   "milliseconds_since_epoch": 1362196405309,
   "date": "03-02-2013"
}

這兩種格式可分別使用Swift內建NSArray跟NSDictionary來Parse,那Array內又有Array該如何Parse呢?


做法:
假設有一json如下:
{
"Parents": [ {"id": "#123",
                       "name": "Daddy1",
                       "Children": [ {"c_id": "#124",
                                                "c_name": "kid1"},
                                           {"c_id": "#125",
                                                "c_name": "kid2"}]
                  },
                     {"id": "#234",
                       "name": "Daddy2",
                       "Children": [ {"c_id": "#235",
                                                "c_name": "kid1"},
                                           {"c_id": "#236",
                                                "c_name": "kid2"}]
                     },
                     {"id": "#345",
                       "name": "Daddy3",
                       "Children": [ {"c_id": "#346",
                                                "c_name": "kid1"},
                                           {"c_id": "#347",
                                                "c_name": "kid2"}]
                     }
                ]
}

var arrResult = json.objectForKey("Parents") as NSArray;
var row: NSDictionary;
var rowSub: NSDictionary;
var arrSubResult : NSArray;
var strID: String;
var strName: String;
var strC_ID: String;
var strC_Name: String;
var arrTotalResult = [DataModel]();


for var i=0 ; i < arrResult.count ; i++ {
 
    row = arrResult[i] as NSDictionary;
    arrSubResult = row.objectForKey("Children") as NSArray;

    strID = row["ID"] as String;
    strName = row["NAME"] as String;
 
    for var j=0 ; j < arrSubResult.count ; j++ {

        rowSub = arrSubResult[j] as NSDictionary;
        strC_ID = row["C_ID"] as String;
        strC_Name = row["C_NAME"] as String;

        //使用data model
        let data = DataModel(ID: self.strID, Name: self.strName, C_ID: self.strC_ID, C_Name: self.C_Name);
        arrTotalResult.append(data);
    }
}






2015年3月11日 星期三

[C#] Performance: 泛型 List V.S. ArrayList V.S. DataTable

說明:數值型別使用ArrayList(型別轉換為Object)會有裝箱與拆箱的問題,參考型別也需進行型別轉換,因此使用List<T>應有較佳的執行效率,而DataTable在擷取DB資料方便使用,但效能似乎較差? 使用StopWatch來簡單驗證(皆跑1000000次)。


Case1:
使用數值型別int做比較,List > ArrayList >> DataTable。








Case2:
使用參考型別string做比較,List ≒ ArrayList >> DataTable。