locked
saving/Comparing Date from C# to Sql RRS feed

  • Question

  • User-349240026 posted

    when date as  String and pass it to StoredProcedure. 

    Convert.ToDateTime(model.ToDate, cultureinfo).ToString("yyyy-MM-dd HH:mm:ss.fff")

    where ToDate is string in Code and DateTime in SP

    For Comparison

    where FromDate is Datetime in code

    obj.FromDate = DateTime.ParseExact(dates1, "dd/MM/yyyy", cultureinfo);

    In SP

    Compre Dates

    BETWEEN CAST(CONVERT(varchar, ISNULL(@FromDate, GETDATE() - 365), 112) AS int) AND CAST(CONVERT(varchar, ISNULL(@ToDate, GETDATE()), 112) AS int)

    Monday, September 10, 2018 8:57 AM

All replies

  • User753101303 posted

    Hi,

    Never do that. The purpose of using strings is to show data to users according to their country convention and read user input. When working on data (transmitting values to the db, selection, comparison, calculation etc...) you should always work on "native" data.

    Monday, September 10, 2018 9:37 AM