2018年4月23日 星期一

MSSQL 以特定格式取得日期



-- yyyy/mm/dd => 2019/02/28
SELECT CONVERT(VARCHAR, GETDATE(), 111)

-- yyyymmdd => 20190228
SELECT CONVERT(VARCHAR, GETDATE(), 112)

-- hh:mm:ss:mmm(24h) => 22:50:40:017
SELECT convert(varchar, getdate(), 114)

-- yyyy-mm-dd hh:mm:ss(24h) => 2019-02-28 22:50:40
SELECT convert(varchar, getdate(), 120)

-- yyyy-mm-dd hh:mm:ss.mmm => 2019-02-28 22:50:40.020
SELECT convert(varchar, getdate(), 121)

-- yyyy-mm-ddThh:mm:ss.mmm => 2019-02-28T22:50:40.020
SELECT convert(varchar, getdate(), 126)

-- yyyy mm dd => 2019 02 28
SELECT replace(convert(varchar, getdate(), 111), '/', ' ')

-- yyyy-mm => 2019-02
SELECT convert(varchar(7), getdate(), 126)


2018年3月26日 星期一

C# 宣告字串類型的列舉

當在 C# 中需要使用到字串類型的列舉時,你可以試試下列的做法定義 ContentTypeEnum
當定義好之後就可以在主程式中打 ContentTypeEnum. 就會跳出建議選單。


using System;
namespace CSharp_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ContentTypeEnum.Json);
Console.ReadKey();
}
}
public static class ContentTypeEnum
{
public static String X_WWW_Form_Urlencoded { get { return "application/x-www-form-urlencoded"; } }
public static String Json { get { return "application/json"; } }
}
}

2018年2月28日 星期三

C# Http Request

//// 建立連線
WebRequest request = WebRequest.Create(requestUrl);

//// 定義連線資訊
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + token);

//// FormData
StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
streamWriter.Write(parameter);
streamWriter.Flush();
streamWriter.Close();

//// 回應資訊
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();

//// Close
reader.Close();
response.Close();

2018年1月31日 星期三

C# 日期字串統一格式

//// 定義日期字串
string dateString = "2018/03/10";

//// 字串 -> DateTime
DateTime datetime = Convert.ToDateTime(dateString);

//// DateTime -> 字串(yyyy-MM-dd HH:mm:ss)
String datetimeString = datetime.ToString("yyyy-MM-dd HH:mm:ss");

//// 轉回來的字串才能統一格式
Console.WriteLine(datetimeString);

參考程式 -> C# Online

2018年1月5日 星期五

SQL Case When In Where Condition

SELECT
   column1, 
   column2
FROM
   viewWhatever
WHERE
CASE 
    WHEN @locationType = 'location' AND account_location = @locationID THEN 1
    WHEN @locationType = 'area' AND xxx_location_area = @locationID THEN 1
    WHEN @locationType = 'division' AND xxx_location_division = @locationID THEN 1
    ELSE 0
END = 1
SELECT column1, column2
FROM viewWhatever
WHERE
    (@locationType = 'location' AND account_location = @locationID)
    OR
    (@locationType = 'area' AND xxx_location_area = @locationID)
    OR
    (@locationType = 'division' AND xxx_location_division = @locationID)
By: https://stackoverflow.com/questions/206484/sql-switch-case-in-where-clause

remove last word from SQL query

SET @columnSql = SUBSTRING ( @columnSql , 1 , LEN ( @columnSql ) - 1 )