I am using PostgreSQL, I am facing an issue in that I am not able to fetch the record’s date range between
actually, in my database, I am saving this type of date in column “2022-12-21 16:12:15”
My PG Query is - "SELECT * FROM table WHERE DATE_FORMAT(date_column,'%YYYY-%MM-%DD') BETWEEN '2022-12-21' AND '2023-03-20'"
SQL error:
ERROR: function date_format(text, unknown) does not exist
LINE 1: ... WHERE DATE_FORMA...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
go to http://sqlfiddle.com, and click on the hamburger at the right of the header bar so that you can choose PostgreSQL as your database, then build a schema using CREATE TABLE that exactly mirrors your date_column, and then use INSERT statements to populate a few rows, making sure you include a few value both within and outside the range you want, then share the link with us so that we can see why the SQL suggested is not working
but this query is wrong, I expect the query will return 3 rows bcz from 21 to 23 there is 3 records exist in db, it show 1 records only.
overall there is one plus date is required to supply, that is wrong logically.
Please help…
actually it’s right, just wrong for your expectation
let me show you how dates work with datetimes
when you compare a date (e.g. ‘2023-03-21’) to a datetime, it’s as though you had specified a datetime with a time of midnight (e.g. ‘2023-03-21 00:00:00’)
take this query for example –
WHERE date BETWEEN '2022-12-21' AND '2022-12-22'
this is equivalent to –
WHERE date BETWEEN '2022-12-21 00:00:00' AND '2022-12-22 00:00:00'
i will show you where your data plus your range endpoints are on this little diagram
can you see now why the query returns only 1 row of your sample data, and not the other two? it’s because the other two are beyond the upper endpoint of the range that you specified
tip: it’s better not to use BETWEEN with dates and datetimes, but instead, use a fixed lower bound and open upper bound like this –
WHERE date >= '2022-12-21'
AND date < '2022-12-23'
this will pick up all datetimes for the 21st and also the 22nd
the reason this is such a great tip is what happens in February
WHERE date >= '2023-02-01'
AND date < '2023-03-01'
but why is this function working in phpmyadmin and MySQL, there is no concern about the date range 21-22 there is just input 21-21 it works.
It might be due to DATE_FORMATE() is there an option same as we can do in PostgreSQL?
What is the problem ? actually I was working on phpmyadmin and this database is not able to handle my big database records as usually very slow speed for calculation, fetch and etc.
so I planned to switch to PostgreSQL here this DATE_FORMATE() not working, I am confused in MYSQL I used UNION, and JOINING with two different database tables the same query will run on PostgreSQL OR not…
Even if I like Postgre more then mySQL there is no increase of speed if you use the same queries on postgre instead of mySQL. Postgre is definitely not faster then MySQL if you do not know how to use the advantages
To be honest, you don’t get a better lumberjack by changing your handsaw by a chainsaw….
First step is to learn the basics and the most important is to not store dates and/or times in text or varchar fields.
After you are able to choose the right column types for the data you need, it is a good idea to even know (and respect) as a minimum the first three normalization rules
After that you can start to analyze your queries with the explain command to see what is slow and what can be improved.
I have developed applications with both databases and at the end both are running fine if you know how to store and retrieve the data correctly.