Results 1 to 5 of 5

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2011
    Posts
    268

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

    Hello guys, im been doing great with parameters thanks to some people on this forum suggestions but now it semms that i am finally stuck with an issue, i have into a database a table which countains a column which is datebased, the date is date and time.
    Now i am trying to search based on the day only, so it will filter all rows with the date from the date & time column, example:
    Code:
    id|    datestarted  |summary             |isdeleted|
    1 |18-10-2001 17:30|it started out great...|0          |
    2 |21-11-2011 17:30|had some issues...    |0          |
    now i tried:
    Code:
    maskedtextbox1.text="18-10-2011";
    sqlcecommand.commandtext = "SELECT * FROM TABLE1 WHERE IsDeleted=0 AND datestarted LIKE @datestarted";
    sqlcecommand.parameters.addwithvalues("@datestarted",maskedtextbox1.text +"%");
    however this doesnt work by not returning any error at all and never finding any match, how can i fix this? ive searched around the net and all i could find was non parametized commands, any help regarding this matter will be appreciated as usual, thanks for your time taken to read this thread and thanks in advance for any reply
    Last edited by Legendary_Agent; Mar 7th, 2012 at 11:49 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to select like on a parametized sqlcecommand?

    Moved From The CodeBank (which is for sharing code rather than posting questions )

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2011
    Posts
    268

    Re: How to select like on a parametized sqlcecommand?

    Quote Originally Posted by Hack View Post
    Moved From The CodeBank (which is for sharing code rather than posting questions )
    im very sorry for the mistake :\ and thank you for moving the thread ^^

  4. #4
    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

  5. #5

    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