What I want is this.
Sometimes a date field in my databse is filled with 0000-00-00 which is perfectly fine. I have no problems putting my data into my table. So I am not looking for solutions aming the alternation of my tabel from 0000-00-00 to NULL.
I want to replace/convert 0000-00-00 to CURDATE or NOW, but I haven't found any solution yet.
The query I currently use looks like this:
$sql = "(SELECT
date_format (dat_start, '%d-%m-%Y') AS dat_start,
date_format (dat_eind, '%d-%m-%Y') AS dat_eind,
o.nm,
o.acroniem,
o.orgid,
r.naam,
r.rolid,
(SELECT COUNT(*)
FROM
relatie re2
WHERE
re2.organ_orgid = relatie.organ_orgid
AND
re2.rolid_rol = relatie.rolid_rol
AND re2.dat_start
BETWEEN dat_start AND dat_eind
AND re2.dat_eind
BETWEEN dat_start AND dat_eind
) AS aantal
FROM
relatie
LEFT JOIN
organ o
ON
o.orgid = relatie.organ_orgid
LEFT JOIN
rollen r
ON
r.rolid = relatie.rolid_rol
WHERE
relatie.pers_persid = '$persid2'
ORDER BY dat_start DESC
)
How can I do that? I hope my question is clear enough. I can find lots of info about getting the data into the table but almost nothing about the situation discribed above.
Thanks in advance!
You can use IF to check if the field is '0000-00-00' or not.
DATE_FORMAT(IF(dat_start = '0000-00-00', NOW(), dat_start), '%d-%m-%Y') AS dat_start
You could use an expression like this in place of date_column (which you should replace with the name of your column):
CASE
WHEN date_column = '0000-00-00' THEN CURDATE
ELSE date_column
END
For example, in a query:
SELECT
field_a,
field_b,
CASE
WHEN date_column = '0000-00-00' THEN CURDATE
ELSE date_column
END AS adjusted_date_column
FROM a_table
WHERE ...;
Related
I want to make a query where I get people birthday's in a current month, I have problems with MONTH() it does not work for me, the date format in the database is: 04/18/1990, and I want to compare if the month current date('m') is equal to the month of the database.
Any suggestions?
you can try this one
SELECT * FROM table_name where extract(month from BirthDate) = date("m");
where date("m") return current month and BirthDate is column name in which you have stored birthdate.
Try this
"select month(str_to_date(birthdate), '%Y/%m/%d')
from users
where month(str_to_date(birthdate), '%Y/%m/%d') = '$month'"
Here, i m assuming your delimeter is '/', Please set your delimeter according to your string date i.e 2013/05/04 or 2013,05,04 or 2013-05-04
could be you have some convertion issue try using
select month(str_to_date(my_col, '%m/%d/%Y'))
from my_table
where month(str_to_date(my_col, '%m/%d/%Y')) = month(now())
I have this query:
$sqlquery="SELECT * FROM tableusers WHERE status = 'off' IN (SELECT DATE(mydate) AS mydate FROM tableusers where mydate < CURDATE()) ";
This return:
off
off
on
This should return pure off:
off
off
off
How can I just return user status according to old day with condition less that today?
Sorry but I didn't get your question clearly, and I don't have enough reputation for comment. Below is one solution which you can try:
SELECT * FROM tableusers WHERE status = 'off' AND mydate < CURDATE()
This will return all the rows, where status is off and mydate is less than CURDATE() that is today.
Please elaborate your question so that we can understand it clearly. What is the purpose of your query? What output are you expecting?
This is your query:
SELECT *
FROM tableusers
WHERE status = 'off' IN (SELECT DATE(mydate) AS mydate
FROM tableusers
WHERE mydate < CURDATE()
);
Do you see anything wrong with it? Most databases would fail with a syntax error somewhere near the IN.
It is a very unusual construct, so I'm not sure if it is parsed as:
WHERE (status = 'off') IN (SELECT DATE(mydate) FROM tableusers WHERE mydate < CURDATE());
or:
WHERE status = ( 'off' IN (SELECT DATE(mydate) FROM tableusers WHERE mydate < CURDATE()) )
I am guessing the latter (based on the fact that some rows are returned). The "off IN" portions returns a boolean, which gets treated as 0. Both 'on' and 'off' would be converted to numbers for the comparison -- and hence 0 = 0 and either value matches.
I would suggest that you compare dates to dates. I don't, however, know the logic that you really intend.
What could be wrong with my sql query here , I'd like to retrieve data from both tables meeting a WHERE condition
SELECT *, UNIX_TIMESTAMP(i.sent_date) AS udate
FROM ibc_sent_history as i INNER JOIN
ibc_messages as u
ON i.msg_ids = u.id
WHERE (i.sent_date >= '02-02-2013' AND i.sent_date <= '02-02-2014')
ORDER BY i.sent_date
LIMIT 200
Assuming your ibc_sent_history.sent_date datatype is DATETIME, here's a way to refactor this query. (This will work even if the datatype is DATE). You need to change your date input string format from 02-02-2013 to the more standard '2014-02-02` (YYYY-MM-DD).
SELECT whatever, whatever
FROM ibc_sent_history AS i
INNER JOIN ibc_messages AS u ON i.msg_ids = u.id
WHERE i.sent_date >= '2013-02-02'
AND i.sent_date < '2014-02-02' + INTERVAL 1 DAY
ORDER BY i.sent_date DESC
LIMIT 200
I changed the ORDER BY to include DESC. This is to return the most recent items, not the oldest. If that's not what you need, take off the DESC.
I changed the date formatting.
I changed the end of your selection range to
i.sent_date < '2014-02-02` + INTERVAL 1 DAY
That's because
i.sent_date <= '2014-02-02`
will include items that occur precisely at midnight on 2-Feb-2014, but won't include any other items on that day. What you probably want are items that occurred up to but NOT including midnight on the next day.
I don't know MySQL very well, but in SQL Fiddle when I run:
CAST('2014-02-02' AS DATE)
I get a date, when I run
CAST('02-02-2014' AS DATE)
I get NULL, so seems like your date format is wrong.
Demo: SQL Fiddle
i trying to do a sql query which i combine de compare operators with substring.
in my column date i have the following value inside : 09-01-2014 12:02:55
what i try to now is to select all rows which is >= 09-01-2014 and for example <=22-01-2014
how can i do it?
i have trying for example with this code:
SELECT * From table Where Name= 'Something'
AND SUBSTRING(date,1,10) = '09-01-2014'
AND SUBSTRING(date,1,10) < '22-01-2014'
You can use the BETWEEN operator
SELECT * FROM table
WHERE Name = 'Something'
AND SUBSTRING(date, 1, 10) BETWEEN '09-01-2014' AND '22-01-2014'
EDIT: I'm still leaving this here, but it is not an error proof solution (as pointed out by oerkelens down in the comments)
The BETWEEN operator will work, like this:
SELECT *
From table
Where Name= 'Something'
AND `date` BETWEEN '2014-01-09' AND '2014-01-23'
Working Demo: http://sqlfiddle.com/#!2/b4d7e
Try this:
SELECT *
FROM tableA a
WHERE a.nme= 'Something' AND
DATE(STR_TO_DATE(a.date, '%d-%m-%Y %H:%i:%s')) >= '2014-01-09' AND
DATE(STR_TO_DATE(a.date, '%d-%m-%Y %H:%i:%s')) <= '2014-01-22';
OR
SELECT *
FROM tableA a
WHERE a.nme= 'Something' AND
DATE(STR_TO_DATE(a.date, '%d-%m-%Y %H:%i:%s')) BETWEEN '2014-01-09' AND '2014-01-22';
Using the following syntax makes your query sargable. It allows query to use any Indexes defined on the date column. for more information SARGable Queries with Datetime Datatype
SELECT * From table
Where Name= 'Something'
AND [DateColumn] >= '20140109'
AND [DateColumn] <= '20140122'
You are converting the date from the table row into a string before comparing to the bookend dates. You need to do the opposite. Convert the bookend dates from strings to dates, then compare each test date.
Some form of the CONVERT or CAST function should do that for you.
The reason your approach won't work is that when SQL server compares strings, it uses alphabetical order. You want ascending date order, which is a different order.
Which Database do you use? Oracle:
SELECT *
FROM table tbl
WHERE 1=1
AND name = 'Something'
AND trim(tbl.column) >= to_date('2014-01-09','DD-MM-YYYY')
AND trim(tbl.column) <= to_date('2014-01-22','DD-MM-YYYY')
or you just convert it into a number/integer like YYYYMMDD then the >= =< operators will work too.
In this question, I have 2 query
1) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '12/31/2013'
This query give proper data from the table.
But in 2 query
2) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '01/10/2014'
This query not display any date from table, how this not display any data, there is some year change problem in mysql server.
Please help me.
You have to convert string to date for comparing two dates otherwise it consider as string. For that you have to use STR_TO_DATE() function
Try this:
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2013-12-31'
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2014-01-10'