2010-03-12 68 views
0

我在計算日期之間的TimeSpans。我有這個沒有問題,如果日期是使用原生的sqlite3格式「YYYY-MM-DD」格式用sqlite3計算日期

我怎麼會做這個,如果日期的格式不同,如「DD-MM-YYYY」

我試過以下沒有成功。

- 選擇兩天之間的天數;這個工程如果時間字符串格式化爲YYYY-MM-DD

SELECT julianday(date1) - julianday(date2) AS Span from myTable; 

- 我在DD-MM-YYYY的格式試過這種的日期,但它好好嘗試似乎工作 --IT似乎無法指定日期格式。

SELECT julianday(strftime('%d-%m-%Y', date1)) - julianday(strftime('%d-%m-%Y', date2)) AS Span from myTable; 

回答

2

由於您使用System.Data.SQLite我建議使用自定義函數。這將更易於使用並與MS SQL Server保持一致,從而使其他.NET開發人員更易於理解和維護。

/// <summary> 
/// MS SQL 2005 Compatible DateDiff() function. 
/// </summary> 
/// <remarks> 
/// ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.SQL.v2005.en/tsqlref9/html/eba979f2-1a8d-4cce-9d75-b74f9b519b37.htm 
/// 
/// 
/// </remarks> 
[SQLiteFunction(Name = "DateDiff", Arguments = 3, FuncType = FunctionType.Scalar)] 
public class DateDiff : SQLiteFunction 
{ 
    public override object Invoke(object[] args) 
    { 
     if (args[0] == DBNull.Value || 
      args[1] == DBNull.Value || 
      args[2] == DBNull.Value) 
     { 
      return null; 
     } 
     string part = Convert.ToString(args[0]); 
     DateTime startTime = ToDateTime(args[1]); 
     DateTime endTime = ToDateTime(args[2]); 

     switch(part) 
     { 
      case "year": 
      case "yy": 
      case "yyyy": 
       return endTime.Year - startTime.Year; 

      case "quarter": 
      case "qq": 
      case "q": 
       return (endTime.Year - startTime.Year) * 4 + ((endTime.Month - 1)/3) - ((startTime.Month - 1)/3); 

      case "month": 
      case "mm": 
      case "m": 
       return (endTime.Year - startTime.Year) * 12 + endTime.Month - startTime.Month; 

      case "dayofyear": 
      case "dy": 
      case "y": 
      case "day": 
      case "dd": 
      case "d": 
       return (endTime - startTime).TotalDays; 

      case "week": 
      case "wk": 
      case "ww": 
       return (endTime - startTime).TotalDays/7.0; 

      case "Hour": 
      case "hh": 
      case "h": 
       return (endTime - startTime).TotalHours; 

      case "minute": 
      case "mi": 
      case "n": 
       return (endTime - startTime).TotalMinutes; 

      case "second": 
      case "ss": 
      case "s": 
       return (endTime - startTime).TotalSeconds; 

      case "millisecond": 
      case "ms": 
       return (endTime - startTime).TotalMilliseconds; 

      default: 
       throw new ArgumentException(String.Format("Date part '{0}' is not recognized.", part)); 
     } 

    } 

    private static DateTime ToDateTime(object source) 
    { 
     try 
     { 
      return Convert.ToDateTime(source);    
     } 
     catch (Exception ex) 
     { 
      throw new ArgumentException(String.Format("DateDiff Input value '{0}' can not be converted to a DateTime.", source), ex); 
     } 
    } 
} 
+0

我最終通過導出表來解決問題,將日期列格式更改爲本機sqlite3格式,然後重新導入表。 但是,我喜歡這種方法,我認爲實施並測試它。我會返回結果。 – galford13x 2010-03-15 20:27:03

+0

不幸的是,爲了這個工作,你需要將第一個參數作爲字符串傳遞。例如:datediff('mi',date2,date1),但是在SQL Server中,你需要傳遞它沒有引號,就像這個datediff(mi,date2,date1) – NYCChris 2011-12-20 23:34:29

+0

@NYCChris,好點,這個版本用於SQLite使用字符串和MSSQL函數使用datepart常量。我不知道有什麼方法在SQLite中複製常量。 – 2011-12-21 02:36:32