0

I have made a problem here. I have stored date into the database in a string format but now I am finding records on the basis of dates. and it is not working. What is the solution to this problem

datatype : varchar | data : eg. 01-10-2002

$current_month_balance =
      tbl_wallet_detail::
      whereMonth('start_date', date('m'))
      ->whereYear('start_date', date('Y'))
      ->get();
     
2
  • Add proper date columns, update the new column(s) from the old columns using STR_TO_DATE(), remove the old columns, reame the new columns to be the old column names, check all the code that loaded these string dates and make sure it is now loading a date properly Commented May 31, 2022 at 9:05
  • Sir, without changing the datatype is it possible? Commented May 31, 2022 at 9:09

1 Answer 1

3

One solution is to change database type of that column to date, and i would say that if possible that would be the correct solution.

If this is not possible, you could try something like this (based on the date format that you provided):

$current_month_balance =
  tbl_wallet_detail::
  whereMonth(DB::raw("STR_TO_DATE(start_date, '%d-%m-%Y')"), date('m'))
  ->whereYear(DB::raw("STR_TO_DATE(start_date, '%d-%m-%Y')"), date('Y'))
  ->get();

This will search a raw DB statement instead of an attribute. Tested on MySQL

Sign up to request clarification or add additional context in comments.

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.