SQL using sysdate - mysql

I have updated the value of the SAD_PB_CAS_STATDT field equal to sysdate which is the date today. The problem now is when i query something with that SAD_PB_CAS_STATDT = sysdate, it does not return anything.
see my sql code below:
SELECT EMPLID, ACAD_CAREER, STDNT_CAR_NBR, ADM_APPL_NBR, APPL_PROG_NBR,
SAD_PB_CAS_STATUS, SAD_PB_CAS_STATDT
FROM PS_SAD_PB_CAS
WHERE SAD_PB_CAS_STATUS IN ('ASG', 'USD')
AND SAD_PB_CAS_STATDT = sysdate;
I'm guessing it has something to do with the return type of sysdate? does it return something with date and time? any help would be appreciated for me to be able to pull the query with sysdate in the conditions.

As others have mentioned, always check the documentation (including Stack Overflow) before you post a question. There are several ways to make your query work, but I would recommend the following WHERE clause:
WHERE
SAD_PB_CAS_STATUS IN ('ASG', 'USD') AND
SAD_PB_CAS_STATDT >= CURDATE() AND -- today at midnight
SAD_PB_CAS_STATDT < CURDATE() + INTERVAL 1 DAY; -- tomorrow at midnight
The reason why this approach is favored over casting your SAD_PB_CAS_STADT column to a date only, is that the above allows an index to be used on the column, if it exists.

Related

How to search data for a date value after than the specified one?

I have a basic SQL knowledge and I am trying to retrieve data from my database based on the date a user chooses. So, if the user selects 2018-03-01 I want to display the data for the date value 2018-03-02.
I can search data for a specified date in the following way:
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01';
Now I want to search the data for the date next to the specified so for '2018-03-02' in this case. I cant directly search for 2nd March because I don't know what date the user will choose
So is there any way to query data for the date next to the one specified?
I have already tried looking up everywhere but couldn't find anything that makes sense to me.
Thx
I think this works well. By the documentation for date/time functions, you should be able to do something like,
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01' + INTERVAL 1 DAY
What sort of element are you using for the user to be able to get their date and what is your codebase? We have mysql for the DB but we need to know if your codebase is c#, PHP etc.
Most languages generally have a function to allow you to add days to a datetime, if for instance you used C# and had a datetime selector posting its value you can do:
DateTime dateField = postedDateTime.AddDays(1);
from there you can easily escape the value and push it through your db call.
or PHP:
$dateField = date('Y-m-d', strtotime($postedDateTime. ' + 1 days'));
If you would like a date range (date selected and the next day) then you would adjust your php code to have:
$dateFrom = $postedDateTime;
$dateTo = date('Y-m-d', strtotime($postedDateTime. ' + 2 days'));
$dateTo = date('Y-m-d', strtotime($dateTo. ' - 1 seconds'));
//DB Query
$query = "select * from FLIGHT where DEPARTURE_DATE >= :dateFrom AND DEPARTURE_DATE <= :dateTo";
The above assuming you're using pdo, but you can see how to adjust for your situation. I am also assuming the datetime is based on midnight for each day so you may need to adjust your dateTo to take from 1 second before midnight the day after just so you can get the entire days data
You can make use of interval as day as mentioned in the answer. Along with day you can also have day_minute, day_hour,day_second but be sure when you are adding decimals on these.

DATE type comparison in MySQL

Had a bit unintuitive case right now with MySQL:
the query contains where clause with comparison: WHERE t.date = '2016-12-31' (t.date-s datatype is DATE(!)).. And it returns no records on execution. But the query: WHERE t.date > '2016-12-31' - returns the records with date equals '2016-12-31' among other records! The record for 2016-12-31 also showed up in case I've used BETWEEN '20161231' AND '20170101'. Tried formattings, type changes - nothing helped. After some time spent on searching for cause I did the following: updated the record's date column manually, SETting it to '2016-12-31'. After this action WHERE t.date = '2016-12-31' started to work as expected.
Probably I'm missing something, wondering what can cause such behavior.
Update
date is DATE, not DATETIME
After doing manual update I can't reproduce the mentioned behavior again: now any type of comparison(=, DATE(..)=, STRCMP) - works as it should!
Update 2
For 2016-11-30 and 2016-09-30(end of months!) found the same behavior! Won't update the record manually for now to test the suggestions I get here.
Update 3
I've also run OPTIMIZE TABLE on the table with that date column to rebuild indexes for elimination any problems with corruption.
Update 4
Here is more:
if I check HEX values for the date field for incorrect fields(end of month) I get wrong values!
SELECT HEX(t.date) FROM table t WHERE t.date BETWEEN DATE('20160930') AND DATE('20161001');
Returns:
323031362D31302D3030
323031362D31302D3031
SELECT HEX(DATE('20160930'));
Returns:
323031362D30392D3330
And 323031362D30392D3330 != 323031362D31302D3030
SELECT X'323031362D31302D3030';
And it returns:
2016-10-00, NOT 2016-09-30!
For the value that I've updated manually - HEX is same.
But what can cause such difference?
Try forcing the format using
WHERE date(t.date) = '2016-12-31'
or
WHERE date(t.date) = str_to_date( '2016-12-31', '%Y-%m-%d')
or based on your test
WHERE date(t.date) = str_to_date( '20161231', '%Y%m%d')
After some investigation I've found the problem and its not related directly to the date comparison in MySQL. I'll post it here in case anyone is stuck at such case.
I've found that the problem was with selecting results in IDE (in my case DataGrip): the value for date field in database was 2016-10-00 and select was returning 2016-09-30! That was confusing.. But after the 00 DAY was found - it was relatively easy to find the cause of it: CURDATE() - 1 (in my case there should have been: CURDATE() - INTERVAL 1 DAY). Don't ever use date related functionality without specific functions like INTERVAL!!
Thanks to everyone who supported the question, sorry for confusion, I was confused too and found the answer only after several steps.

Mysql Select with Dates and maybe Case when

im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.

How to use where clause in separate datetime(year,month,day)

http://upic.me/i/hq/capture.png
http://upic.me/i/3g/capture.png
I have the table that divide datetime to single field and set these field to index.
i would to use where clause in date range ex. between 2010/06/21 to 2011/05/15
I try to use
where concat_ws('-',year,month,day) between '2010/06/21' and '2011/05/15'
it's work because I use concat function to adjust these field like ordinary datetime
but it not use index and query slowly.This table has 3 million record
if would to use index I try to this query
where
year = '2011'
and month between 05 and 06
and day between 21 and 15
It almost work but in last line
day between 21 and 15
I can't use this condition
I try to solve this problem but I can't find it and change structer table
I'm looking for answer
thank you
Now I can OR operation for query thank for your answer
In another case if would to find 2009/08/20 to 2011/04/15 It's use longer query and make confusion.Has someone got idea?
If it's a datestamp type, you can just use the where/between clause directly. I would consider switching to that, it's quite faster than a varchar with a custom date format.
WHERE yourdate BETWEEN "2011-05-01" AND "2011-06-15"
Although checking ranges may work for single months, you will find if you're querying between several months to have some margin of error because, if you think about it, you're selecting more than you may necessarily want. Using Datestamp will fix performance and usability issues arising from storing the date in a custom varchar.
Here are the two queries to convert your times around if you're interested:
ALTER TABLE `yourtable` ADD `newdate` DATE NOT NULL;
UPDATE `yourtable` SET `newdate` = STR_TO_DATE(`olddate`, '%Y/%m/%d');
Just change "yourtable", "newdate", and "olddate" to your table's name, the new date column name, and the old datestamp column names respectively.
If you can't change the table structure, you could use something like the following:
WHERE year = '2011'
AND ((month = '05' AND day >= 21) OR (month = '06' AND day <= '15'))
(At least, I think that query does what you want in your specific case. But for e.g. a longer span of time, you'd have to think about the query again, and I suspect queries like this could become a pain to maintain)
UPDATE for the updated requirement
The principle remains the same, only the query becomes more complex. For the range of 2009/08/20 to 2011/04/15 it might look like this:
WHERE year = '2009' AND (month = '08' AND day >= '20' OR month BETWEEN '09' AND '12')
OR year = '2010'
OR year = '2011' AND (month BETWEEN '01' AND '03' OR month = '04' AND day <= '15')
where year = 2011
and (month between 5 and 6) and (day > 20 or day < 16)
You where seperating days and month whereas you must keep them together
parentheses must be set ...
Mike
It is important that you use OR otherwise it is nonsense

Help me to revise mySQL query

I am trying to check if a regulation's date reminder is today and regulation's date end not yet passed then I do not want it to display. The problem is that the query that I made isn't working in mysql. Can anyone help me to revise my query?
Here is my query:
$query="select * from t_regulation where dt_reminder >= '$today' and dt_ended ='$today'"
This is assuming that your dt_reminder columns type is DATETIME, and not some sort of timestap.
SELECT * FROM t_regulation WHERE DATE(dt_reminder) >= CURDATE() AND DATE(dt_ended) = CURDATE()
You can do many funky things with date functions;
Mysql date/time functions
Very frequently I run into the problem that my date variable is a string that is not properly formatted for the default date time stamp in Mysql.
Remember it should be 'yyyy-mm-dd' for this comparison.
Also, for since the 'date ended' has not yet passed, shouldn't it be:
$query="select * from t_regulation where dt_reminder >= '$today' and (dt_ended > '$today' or dt_ended is null)"