2

what is the wrong in this Query ?

string command_get_pay = "select Credit 
                          from Update_Company_Credit 
                          where (Update_Date LIKE '%" +
    System.DateTime.Today.ToShortDateString() + "%')";
7
  • 3
    why do you think it is wrong? Commented Aug 18, 2010 at 0:30
  • 1
    For what database? What is the data type for the UPDATE_COMPANY_CREDIT.update_date column? LIKE is for strings, which doesn't make much sense for a date... Commented Aug 18, 2010 at 0:45
  • 1
    I promise you when I know why I will tell you Commented Aug 18, 2010 at 0:50
  • the update_date type is datetime so I think string type is wrong i try int and it worked good Commented Aug 18, 2010 at 0:53
  • does SO have a "how to ask a meaningful question" page? could be a good time to link it. Commented Aug 18, 2010 at 0:54

4 Answers 4

2

I believe that you are trying to check that Update_date is the current date, regardless of time, and that your problem is that you are not receiving any results even though there are some Update_date values for the current date.

This is because System.DateTime.Today.ToShortDateString() converts the system date into a different format than the format used by the implicit conversion of a datetime into a string produced by the LIKE comparison.

Given that SQLServer has its own date comparison functions, I recommend that you use those, like so:

string command_get_pay = "select Credit 
                      from Update_Company_Credit 
                      where (datediff(d,Update_Date, getdate())=0)";
Sign up to request clarification or add additional context in comments.

Comments

1

Some more details would be helpful. Are you expecting to see results from this query, but when it runs you get nothing? If so, chances are you need to make sure that the filter value (ie System.DateTime.Today.ToShortDateString() ) is formatted in a way that is acceptable to the T-SQL engine. For example, ToShortDateString() might return a string value that is not in a compatible format for the SQL query.

This may also depend on which relational database you are using.

Comments

1

I believe the SQL LIKE operator only works on text fields. If Update_Date is a date field, that could be a problem.

1 Comment

SQL Server 2005 will actually let you use LIKE on datetime fields, but also on int fields and even bit fields.
0

Check the date format in SQl Database and the one you assign though System.DateTime.Today.ToShortDateString()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.