Trunc date field in mysql like Oracle - mysql

I am to not able to use the 'trunc(in oracle)' function in 'mysql' database. I have a table called dlb_cc_purchase and date field called due_date in my 'mysql' database. The data displaying in the date field like 20-11-2014 00:00:00 (20-nov-2014). in oracle we are using query
select * from dlbcc_purchase where trunc(due_date) = '20-nov-2014'
Oracle DB will fetch the row with due date 20-11-2014 00:00:00. How can I use this function in 'mysql'?
I know this is a basic question, but i was trying to do this for long time with truncate, str_to_date... but not able to fetch value. Please help.

Use DATE(expr) function. Query example:
SELECT *
FROM dlbcc_purchase
WHERE DATE(due_date) = '2014-11-20'

You can use DATE_FORMAT().
example:
select * from dlbcc_purchase where DATE_FORMAT(due_date,'%d-%b-%Y') = '20-nov-2014'

Related

MySQL returns a date format different that it has been setted

i have a MySQL's db with 2 DATE columns and when i add a registry the date is saved like this 2018/07/30. The problem is that when i make a get call to the db the value comes like this"2018-07-30T03:00:00.000Z"
I do not need the time part, and i think that the DATE type i have set to the column should not return this kind of value.
I have tried this but it does not work:
"SELECT * FROM patrones WHERE DATE_FORMAT(bd_fRecepcion, '%Y-%m-%d') WHERE bd_SI = 'Si' "
How could i do to get retuned the date part only?
Thanks for your help.

Mysql - How to compare date (mm/dd/yyyy) with another another date from database?

This is my SELECT query
season_from_date >= "06/09/2015" and season_to_date <=
"06/11/2015"
The same date format(mm/dd/yyyy), am saving in my database. I am not getting error but its not showing any result? Is that correct method or do we have any other method?
You should never store in those format and using varchar field, you should always store date data with mysql native datatypes this makes life easy. However in this case you may use str_to_date function
select * from table_name
where
str_to_date(season_from_date,'%m/%d/%Y') between
str_to_date('06/09/2015','%m/%d/%Y')
and
str_to_date('06/11/2015','%m/%d/%Y')

Date functions trim off timestamp in DB2

Is there a way to trim off the timestamp in a DB2 date function?
Have DB2 select statement where I'mm selecting a date form the databease and saving it to a variable. I then use that variable as a parameter for another db2 select by adding 30days to it but I don't think it agrees with the timestamp that it is adding to the end.
Select business_date From DB2INST1.BusDate Where key = 0
There is no timestamp in the database for this date but its adding '12:00:00AM' to the end
it saves this select into a variable and I use it in another select here
where expirdate > (DATE(#BusDate) + 30 DAYS)
I get this error:
{"ERROR [428F5] [IBM][DB2/AIX64] SQL0245N The invocation of routine \"DATE\" is ambiguous. The argument in position \"1\" does not have a best fit."} System.Exception {IBM.Data.DB2.DB2Exception}
Select business date as a varchar/string
Select varchar_format(business_date,'YYYY-MM-DD')
then do this to use it later, convert it to a date then use a date function to add 30days to it:
(DATE(to_date(#BusDate, 'YYYY-MM-DD') + 30 DAYS))
Try
where expirdate > DATE(#BusDate + 30 DAYS)
Is the first SELECT statement and the second SELECT Statement part of same stored procedure? Are you storing the resulting date in any .Net variable? If you are storing it in a .Net DateTime variable, my suggestion is to do the date addition operation in .Net code itself. And then remove the time part from the variable before passing to the database.
Take a look at this too: Datetime field overflow with IBM Data Server Client v9.7fp5
If both the SELECT statements were part of a stored procedure and there is no .Net DateTime variable invloved, then it is a different story.

Float to date in mysql

We have a script that saves some rows in a table and one of them is filled with Time.now.to_f. So we have values like:
1330017921.1065
1330018520.80347
Is there a way to convert this to a date directly in MySQL I now I can use Time.at but I need a row mysql query.
i guess this is a microtime.
if you don't need milliseconds, you could query something like
SELECT * FROM table WHERE datefield = FROM_UNIXTIME(rounded_unixtime)
where rounded_unixtime would be in your case 1330017921

Turning Off Timestamp Formatting in MySQL

I have many timestamp columns that I need to use with mixed timezones. The users timezone is set in the language I'm using for the front-end so I need MySQL to return a unix timestamp from a select *. Is there a way to turn off the auto formatting when fetching data from MySQL timestamp columns?
YYYY-MM-DD HH:MM:SS is the default representation for timestamp columns in MySQL. I don't believe you can change that on a global level.
Two options:
Instead of doing a SELECT *, do SELECT *, UNIX_TIMESTAMP(your_timestamp_column) AS your_timestamp, which will add a Unix-formatted your_timestamp column to the results.
Make a view (CREATE VIEW) for each table that does the same thing, e.g.
CREATE VIEW your_view AS
SELECT *, UNIX_TIMESTAMP(your_timestamp_column) AS your_unix_timestamp
FROM your_table;
Then you can do SELECT * FROM your_view; and get your Unix timestamp without adding anything to your queries.
Reference: UNIX_TIMESTAMP
Yes.
You can wrap the filed in the UNIX_TIMESTAMP function like this:
select UNIX_TIMESTAMP(your_timestamp_column) from your_table;
For more information about this and other mysql data and time functions see:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp