Results 1 to 5 of 5

Thread: [RESOLVED] How to select like on a parametized sqlcecommand?

Hybrid View

  1. #1
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to select like on a parametized sqlcecommand?

    You can't use LIKE with any data type other than text. You can only use things like =, < and > for dates. In this situation you have two choices:

    1. Use a database function to remove the time from the date/time column value.
    2. Use two date parameters a day apart.

    The first option would look something like this:
    SQL Code:
    1. WHERE DateFunction(MyDateTimeColumn) = @MyDateParameter
    There is no actual DateFunction function. You'd have to check the documentation for your database to see if a function that does that exists.

    Here's an example of the second option to get all records with yesterday's date:
    csharp Code:
    1. myCommand.CommandText = "SELECT * FROM MyTable WHERE MyDateTimeColumn >= @StartDate AND MyDateTimeColumn < @EndDate";
    2. myCommand.Parameters.AddWithValue("@StartDate", DateTime.Today.AddDays(-1))
    3. myCommand.Parameters.AddWithValue("@EndDate", DateTime.Today)
    You could probably also use BETWEEN but I can never remember which limits are inclusive and which are exclusive.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2011
    Posts
    268

    Re: How to select like on a parametized sqlcecommand?

    Quote Originally Posted by jmcilhinney View Post
    You can't use LIKE with any data type other than text. You can only use things like =, < and > for dates. In this situation you have two choices:

    1. Use a database function to remove the time from the date/time column value.
    2. Use two date parameters a day apart.

    The first option would look something like this:
    SQL Code:
    1. WHERE DateFunction(MyDateTimeColumn) = @MyDateParameter
    There is no actual DateFunction function. You'd have to check the documentation for your database to see if a function that does that exists.

    Here's an example of the second option to get all records with yesterday's date:
    csharp Code:
    1. myCommand.CommandText = "SELECT * FROM MyTable WHERE MyDateTimeColumn >= @StartDate AND MyDateTimeColumn < @EndDate";
    2. myCommand.Parameters.AddWithValue("@StartDate", DateTime.Today.AddDays(-1))
    3. myCommand.Parameters.AddWithValue("@EndDate", DateTime.Today)
    You could probably also use BETWEEN but I can never remember which limits are inclusive and which are exclusive.
    Even better than what i wanted, thank you so much, i cant semm to add rep right now but as soon as i "spread" enough rep ill add it asap.
    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width