I am trying to update the filed date in mysql database , this field contains unix-timestamp date.
But I do not want to convert the whole of date 1364562954 , I want to replace only the year and leave the day, month and time same.
For Example - 19-03-2015, 03:43 PM to 19-03-2016, 03:43 PM
You can use FROM_UNIXTIME and UNIX_TIMESTAMP to convert a Unix timestamp to MySQL's DATETIME format.
So for example to get a timestamp incremented by one year:
SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(1364562954) + INTERVAL 1 YEAR);
Result: 1396098954
For UPDATE it is the same:
UPDATE t SET tstamp= UNIX_TIMESTAMP(FROM_UNIXTIME(tstamp) + INTERVAL 1 YEAR);
Reference: Date and Time Functions
Related
I have a database with the following structure and I am trying to get all rows from this table based on passing both the month and year using a where on the timestamp column (this will be a unix standard timestamp)
e.g month - 3, year - 2018 // get all rows for March 2018 only.
// db structure
id, timestamp, post_title
If you want rows for a given month using a unix timestamp, I would recommend:
where timestamp >= unix_timestamp('2018-03-01') and
timestamp < unix_timestamp('2018-04-01')
If you are passing in a variable, I would recommend passing in the first day of the month and doing:
where timestamp >= unix_timestamp(?) and
timestamp < unix_timestamp(? + interval 1 month)
Use convert function
SELECT * FROM dbo.YourTable WHERE CONVERT(VARCHAR(5),DATEPART(mm,timestamp))+'-'+CONVERT(VARCHAR(5),DATEPART(yy,timestamp)) ='3-2018'
I guess this can help you.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
I'm looking to create a date time field in a MySQL script that has a specific date and time.
I've tried using
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y%m%d'), ' 13:00:00')
but it doesn't insert correctly.
How can I achieve this so that it will insert a date time with the time as above?
It inserts the record as 0000-00-00 00:00:00 with the above
mysql accepts datetime values in yyyy-mm-dd hh:mm:ss format. In your formula you do not separate the year, month, day values, hance the result is not in the date format mysql expects the dates in. Change it to:
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y-%m-%d'), ' 13:00:00')
But I do not really understand why you need to do the formatting, just use CURDATE() function instead of the NOW():
CONCAT(DATE_SUB(CURDATE(), INTERVAL 9 DAY), ' 13:00:00')
I have a table with year data in a year datatype field, now I want to insert new data with date data and I want to convert the year from the old data to date with default month and day 1.1.YearFromOldData.
I'm looking for something like the function STR_TO_DATE but for the datatype year NOT VARCHAR NOT VARCHAR and I fail to find it. How would I do this?
I want to do this
SELECT YEAR_TO_DATE(myYearField, '%1/%1/%Y')
FROM myTable
Assuming that you have a varchar field named year_dt with old years, use the following query to get a date with default day and month
SELECT DATE(CONCAT(table.year_dt, '-01-01')) as 'date' FROM table
this will return date in default format i.e. YYYY-MM-DD
If you want the first day of the year, I think the easiest way is with makedate():
select makedate(year, 1)
You can get the timestamp to insert to the database using
timestamp = (year - 1970) / 31557600
STR_TO_DATE() is exactly what you need. Try this if you have four-digit years, in a string, an integer, or a YEAR column. It works for them all.
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
If you have two-digit years try this (lower case %y)
select str_to_date(CONCAT(year_column,'-01-01'), '%y-%m-%d')
This is cool because you can do all sorts of date arithmetic: for example
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
+ INTERVAL 1 QUARTER
- INTERVAL 1 DAY
will give you the last day of the first quarter of your year.
I am trying to select everything from my table with today's date. But I found that that date column is in unix time stamp. So, how do I select everything with today's date? or for example only yesterday's ? If it was normal date instead of unix time it would be easy, but... here is what I have put for my query so far..
$ann_renewal_query = "SELECT * FROM annual_renewal WHERE due_date '%$today%' order BY due_date ASC";
You just need to convert the values to the unix timestamp version. The best way is to convert the current time to Unix timestamp:
where due_date >= UNIX_TIMESTAMP(curdate()) and
due_date < UNIX_TIMESTAMP(date_add(curdate(), interval 1 day)
This version of the query allows it to take advantage of an index on due_date.
I have date column field it's data look like 2014-05. how i get previous 6th month interval. My mysql query below
SELECT name,grade from users where data_year BETWEEN DATE_FORMAT('2014-04', '%Y-%m') - INTERVAL 12 MONTH AND DATE_FORMAT('2014-04', '%Y-%m')
I got result when i put with days like 2014-04-31
The strings you used aren't valid dates as they don't contain the day. If you want to use a date literal in a SQL statement you should use the form DATE '2014-04-01', eg
SELECT name,grade
from users
where date_column BETWEEN DATE '2014-04-01' - INTERVAL 6 MONTH
AND DATE '2014-04-01'
UPDATE
From the comments it seems the question actually is - how to parse an incomplete date string. You can use STR_TO_DATE for that, but you'll have to append -01 at the end, otherwise you'll get an invalid date with 0 for the day of month, ie 20140-04-00 instead of 2014-04-01.