views:

59

answers:

1

I'm trying to calculate TimeSpans between dates. I have no problem with this if the date is formatted using the native sqlite3 format 'YYYY-dd-mm'

How would I do this if the date is formatted differently, such as 'dd-mm-YYYY'

I've tried the following with no success.

--Select days between two days; this works if the datetime string is formated YYYY-dd-mm

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

--I tried this for dates in the format of dd-mm-YYYY but it doens't seem to work --It seems to be that a date format cannot be specified.

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

Since you're using System.Data.SQLite I would recommend using a custom function. This will be easier to use and be consistent with MS SQL Server, making it clearer for other .NET developers to understand and maintain.

/// <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);
        }
    }
}
Sam
I ended up resolving the issue by exporting the table, changing the Date Column format to the native sqlite3 format and then reimporting the table.I do like this method however, and I think implement and test it out. I'll return with the results.
galford13x