This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
SQL date format convert? [dd.mm.yy to YYYY-MM-DD]
(3 answers)
Closed 5 years ago.
Am trying to query data from two joined tables with a date as criteria using the query below but it brings nothing, can someone please show me what I did wrong.
SELECT Journal_T.GL_ID as Akaunti
,coa_t.gl_name_vc
,SUM(Amount_NU) as YTD
FROM Journal_T
JOIN coa_t
ON journal_t.gl_id = coa_t.gl_id
WHERE CAST(date_dt AS DATE) BETWEEN '25/08/2017' AND '25/08/2017'
AND bs_category_vc='Rev'
GROUP BY coa_t.gl_name_vc
,Journal_T.GL_ID
ISO 8601 Date format would be '2017-08-26'. Try this query to see default Date format on your server:
select STR_TO_DATE("26/08/2017", '%d/%m/%Y')
Then you can convert it, for example to '%Y-%m-%d' format:
DATE_FORMAT(STR_TO_DATE("26/08/2017", '%d/%m/%Y'), '%Y-%m-%d')
I think you can try out this query :
SELECT Journal_T.GL_ID as Akaunti,coa_t.gl_name_vc,sum(Amount_NU) as YTD
FROM Journal_T
LEFT JOIN coa_t on journal_t.gl_id = coa_t.gl_id
WHERE DATE_FORMAT(date_dt,'%d-%m-%Y') BETWEEN '25/08/2017' AND '25/08/2017'
AND bs_category_vc='Rev'
GROUP by coa_t.gl_name_vc,Journal_T.GL_ID
Related
This question already has answers here:
Can you use an alias in the WHERE clause in mysql?
(5 answers)
Closed 4 years ago.
So i am trying to use Between Timestamp as follows , i have date stored as Unix time stamp and i am 1st trying to convert it, and then get all users between the said time.
SELECT
users.*,
DATE_FORMAT(
FROM_UNIXTIME(users.created),
'%Y %b %e '
) AS date_formatted
FROM
node
LEFT JOIN users
ON node.uid = users.uid
WHERE node.uid != 0
AND
(date_formatted BETWEEN '2017-06-30 00:00:00' AND '2018-06-30 00:00:00')
GROUP BY uid
But i am getting this error
Unknown column 'date_formatted' in 'where clause'
the issue, as mentioned in the comments, is that "date_formatted" is an alias of a result. it is not in the tables itself and therefore you can't call it within the WHERE statement. You can call it at the end of a query using a HAVING statement, but that would mean that the query has to first pull up a whole lot more data than you need. MYSQL does a whole lot of implicit casting when it comes to dates, so you can probably just use:
users.created BETWEEN '2017-06-30 00:00:00' AND '2018-06-30 00:00:00'
but if you don't want to risk it, you can apply the formatting/casting within the WHERE statement.
This question already has answers here:
Select current months records mysql from timestamp column
(12 answers)
How do I select between the 1st day of the current month and current day in MySQL?
(17 answers)
Closed 9 years ago.
How can I select Current Month records from a table of MySql database??
Like now current month is January. I would like to get records of January Month, Where data type of my table column is timestamp.I would like to know the sql query.
Thanks
This query should work for you:
SELECT *
FROM table
WHERE MONTH(columnName) = MONTH(CURRENT_DATE())
AND YEAR(columnName) = YEAR(CURRENT_DATE())
Check the MySQL Datetime Functions:
Try this:
SELECT *
FROM tableA
WHERE YEAR(columnName) = YEAR(CURRENT_DATE()) AND
MONTH(columnName) = MONTH(CURRENT_DATE());
Try this query:
SELECT *
FROM table
WHERE MONTH(FROM_UNIXTIME(columnName))= MONTH(CURDATE())
This question already has answers here:
Mysql to select month-wise record even if data not exist
(2 answers)
Closed 9 years ago.
I have a table with a column date and a column ID (to keep it simple). I am doing a query to count the total and group by week
SELECT MONTH(table1.Date_1st), YEAR(table1.Date_1st), COUNT(table1.Id)
FROM table1 as table1
WHERE Date_1st BETWEEN '2013-04-01' AND '2013-07-25'
GROUP BY WEEK(Date_1st)
the problem is there is no results on specific week the result is not taken into consideration. I already try ifnull(table1.Id,0)
Try ISNULL or other NULL SQL functions:
NULL functions
ISNULL returns a 0 if the value is null.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Select by Month of a field
i want to get the data from database whose month is 7. means
My Date format is
PostDate datetime
now i want to query..
select * from tabelname where PostDate = 7
If you are asking how to query MySQL for date objects based on month, then this may be your answer.
select * from tablename where month(PostDate) = '07';
Keep in mind this function (month()) is MySQL specific.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
trying to get the number of months
I have a
memberpayments table
totalamountpaid
exppayments
expmonthly payments
dueday
memberid
I want to find the datediff like this way here I have found the date diff using c#
but I want to find the date diff using mysql
double equivalentPayments = totalamountpaid /expmonthly payments;
double monthdiff = Math.Ceiling(exppayments - equivalentPayments);
monthdiff -= 1;
int dueDay = 01;
DateTime expDate = DateTime.Today.AddMonths((int)monthdiff).AddDays(DateTime.Today.Day - dueDay);
int diff = DateTime.Today.Subtract(expDate).Days;
I have tried using Date diff but it was giving the difference between two dates and it was not giving datediff like above solution(c#).
can i get these statements all in one query......using mysql....
would any one pls help me out
MySQL have a large set of date time functions. Finding differences between dates are fairly trivial using them :)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
If i understand correctly, all you are after is the difference in days between two dates. This can be done with DATEDIFF(a,b) :)