1

I am using MySql and by standard their date format is 2019/03/31 in the database.

In my program .net, csharp i read the values into a variable as 31/03/2019, so str_date_format will not work as this only accepts - instead of /.

The strings CAN'T be hard coded in the where clause they have to be read in as parameters.

So from reading the manual i came to the conclusion that i need to use date_format with delimiters in the where clause

SELECT 
    DATE_FORMAT(date_time,'%d-%m-%Y'), 
    AVG(Total), 
    SUM(Total_ly),  
    AVG((Total + Total_ly)/2) 
FROM transaction 
WHERE 
    date_format(date_time >=  @param1 , '%d-%m-%Y') 
    AND date_format(date_time <= @param2, '%d-%m-%Y') 
GROUP BY date_time;

But its returning null for every column and it doesn't make sense because without the where clause it works fine.

I edited with more detail hope this helps, Appreciate any advice in the right direction !

1
  • It’s a bit confusing to say you need to use a format with slashes and then use one with dashes... Commented Mar 31, 2019 at 12:37

3 Answers 3

3

I guess that you actually mean:

SELECT 
    DATE_FORMAT(date_time,'%d-%m-%Y'), 
    AVG(Total), 
    SUM(Total_ly),  
    AVG((Total + Total_ly)/2) 
FROM transaction 
WHERE 
    date_time >=  STR_TO_DATE('22/03/2019' , '%d/%m/%Y') 
    AND date_time <= STR_TO_DATE('24/03/2019', '%d/%m/%Y') 
GROUP BY date_time;

Details:

  • DATE_FORMAT() can indeed be used to turn a datetime to a string in the expected format

  • STR_TO_DATE() converts a string to a date, given a format spec; you can use it to turn your input strings to dates before comparing them with column date_time. Or you can simply provide the input dates in the correct format, which is yyyy-mm-dd

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

4 Comments

This works in theory but taking values for where clause from variables in csharp and it doesnt support - it uses / to seperate dd/mm/yyyy, ill edit question for full scope i think
@juniordev101... ok so basically you want : STR_TO_DATE('22-03-2019' , '%d/%m/%Y')
So yeah thats correct, im very new to all this so sorry for all the questions how to i pass parameters in istead of having dates hardcoded in where clause, do you know what i mean ?
@juniordev101: you can easily find the answer to this question on StackOverflow (or Google). Basically it is not a good practice on Stack Overflow to substantially modify the question after answers were posted. If my answer properly responded to your (original) question, please upvote it and accept it... If you have additional queries that you cannot find answers to by yourself, you may also ask another question. Cheers!
3

I think you intend the where clause to be:

where date_time >= '2019-03-22' and
      date_time <= '2019-03-24'

Don't convert dates to strings for comparisons.

2 Comments

They have to be converted because my program passes the vairables into the sql string
@juniordev101 . . . You should be passing values into the query using parameters not strings. And, parameters can be typed so you can have dates.
1

The format that you use:

'%d-%m-%Y'

is not a comparable format like %Y-%m-%d.
But even if it was, this:

date_format(date_time >=  "22-03-2019" , '%d-%m-%Y')

is wrong, it should be written as

date_format(date_time, '%d-%m-%Y') >=  "22-03-2019"  -- again not comparable.

You can do what you need by using STR_TO_DATE() to convert a string to date:

SET @start = '22/03/2019';
SET @end = '24/03/2019';
select 
  date_format(date_time,'%d/%m/%Y'), 
  AVG(Total), sum(Total_ly),  
  AVG((Total + Total_ly)/2) 
from Transaction 
WHERE date_time between STR_TO_DATE(@start, '%d/%m/%Y') AND STR_TO_DATE(@end, '%d/%m/%Y') 
Group By date_time;

6 Comments

This works perfect, i was looking through the mysql documentation and it seems STR_TO_DATE only takes a string, but could this be passed as a param?
You use STR_TO_DATE when you have a string to convert to date although mysql could do also this: WHERE date_time between '2019-03-22' AND '2019-03-24', but for clarity I use conversion. Of course you can pass a string as a parameter.
Im sort of confused now so i want the where clasued checked as 31/03/2019 NOT 2019/03/31
I edited, now you can use 31/03/2019 in the where clause
YES, this works now, so my last question i researched it im still confused say i have param1 = 22/03/2019, how do i put this in STR_TO_DATE cuase it wont work if i just replace with hardcoded date
|

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.