Basically I am trying to convert a date field from my database into a number of seconds old. I used UNIX_TIMESTAMP on our old MySQL database and am looking for an equivalent function in PostgreSQL.
Thanks in advance
SELECT extract(epoch FROM your_datetime_column)
FROM your_table
More details in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
This:
select CURRENT_TIMESTAMP - '1970-01-01';
Will give you an interval type. You can get seconds out of that interval result if you need it.
I am trying to convince existing MySQL queries to work, this is what I have, seem to be working fine to me.
CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(date_field timestamp with time zone) RETURNS integer
AS $$
BEGIN
RETURN extract(epoch FROM date_field);
END;
$$ LANGUAGE plpgsql;
Related
I am in desperate need of some help!
I am currently working on a project for SQL in a course I am doing (Due on Friday)
One of the questions is to create a stored procedure which shows dates of sales_orders within a certain range.
I have created the following stored procedure for this question, but all I am getting is blank rows? From what I can tell it seems to be reading the dates wrong, but I have no idea why? Can anyone please help? I am a newbie at SQL.
DELIMITER //
CREATE PROCEDURE customer_order_range (start_date DATE, end_date DATE)
BEGIN
SELECT *
FROM customer_order
WHERE `date` >= start_date
AND `date` <= end_date;
END //
DELIMITER ;
CALL customer_order_range ( 'start_date' = '2020-02-01', 'end_date' = '2020-03-05');
It returns blank rows. My customer_order table has a date column, which is stored as a DATE value.
Any help would be super appreciated!
Conor
I've got it!
This piece of the code is wrong:
CALL customer_order_range ( 'start_date' = '2020-02-01', 'end_date' = '2020-03-05');
It should instead be:
CALL customer_order_range ('2020-02-01','2020-03-05');
The problem arose when I put the parameter names in with their values when calling the stored procedure. Thanks everyone! :)
Remove the delimiters and the columns names like below :
CALL customer_order_range ('2020-02-01', '2020-03-05');
I can't seem to figure this out and I've been at it for sometime now any help would be great thank you.
I have to prepare a simple procedure accepting one parameter, a date value, and using date addition functions in SQL (MySql if possible) return the parameter value minus 1 day
CREATE PROCEDURE get_date
IS
BEGIN
DATE_ADD(date,INTERVAL expr type)
END;
/
EXEC get_date
This was all I could come up with so far. Any help would be greatly appreciated.
That question is not just about select statement (ie, computing minus one day); I need help with a SQL procedure.
Maybe just create a function
CREATE FUNCTION simplefunction (s datetime)
RETURNS datetime DETERMINISTIC
RETURN DATE_SUB(s, INTERVAL 1 DAY);
SELECT simplefunction (anydate);
I have already been doing some function in postgresDB, but im completly new to MySQL. I thought that it will be almost same to create procedure in MySQL, but aparently it is not. So I am asking for some help :)
In postgres I would write this function for what I need to accomplish:
DROP PROCEDURE IF EXISTS report_incomes;
DELIMITER //
CREATE FUNCTION reportincomes(IN interval INT)
RETURN report_tab TABLE
(
year_month DATE,
total DECIMAL(30,2),
total_before DECIMAL(30,2)
)
AS
BEGIN
FOR row_d IN SELECT * FROM intervals_generator('2013-01-01 00:00:00', now(), interval) LOOP
SELECT sum(cash) AS total, DATE_FORMAT(datetime, '%b %Y') AS year_month FROM incomes WHERE datetime BETWEEN row_d.start_d AND row_d.end_d//
SELECT sum(cash) AS total_before FROM incomes WHERE datetime BETWEEN DATE_SUB(row_d.start_d, INTERVAL 1 YEAR) AND DATE_SUB(row_d.end_d, INTERVAL 1 YEAR)//
END LOOP//
END//
DELIMITER ;
As you may noticed, the big part of code is already switched to MySQL, but I am kind of stuck with FOR LOOP, as it looks so different in MySQL compared to Postgres.
Of course there will be much more code added inside the LOOP, I just did not wanted to make it too complicated for explanation.
Could please anyone help me with the FOR LOOP?
Basicly, I want to execute these SELECTs for each row returned by function that generates my custom intervals, while using data from that row inside of LOOP
I have the following trigger which inserts records into Table B whenever Table A is updated. This works fine however TableA_date is in unix time format and I want to convert it when the trigger inserts the record in Table B.
DELIMITER $$
CREATE TRIGGER MyTrigger
AFTER INSERT
ON TableA
FOR EACH ROW
BEGIN
INSERT INTO TableB SET
TableB_id = NEW.TableA_id,
TableB_date = FROM_UNIXTIME(NEW.TableA_date, '%d/%m/%y %r'),
TableB_comment = NEW.TableA_comment;
END $$
DELIMITER ;
In my results, instead of "01/01/70 03:00:05 AM" as the converted date I get "5" - I know the format string is correct as I am able to use it in a select statement. Thanks for your help
You are working too hard. Just leave out the format string entirely. Dates are stored as dates, not a strings.
In fact, don't even use the conversion at all! Just directly assign the date column, and MySQL will do any conversion it needs.
I'm assuming that TableA_date is stored using the timestamp datatype, and that TableB_date is datetime. (Although you may want timestamp for that one too.)
If you are using other datatypes like int, or char, then fix it. That's not the correct way to structure a database.
Just to answer your actual question (instead of telling you the correct way to do it) the date format you want is yyyy-mm-dd hh:mm:ss not what you have.
How can I get the current timestamp using a mysql query?
Depends on which kind you're looking for.
The current integer Unix Timestamp (1350517005) can be retrieved like so:
SELECT UNIX_TIMESTAMP();
MySQL often displays timestamps as date/time strings. To get one of those, these are your basic options (from the MySQL Date & Time reference):
SELECT CURRENT_TIMESTAMP;
SELECT CURRENT_TIMESTAMP();
SELECT NOW();
CURRENT_TIMESTAMP is standard SQL and works on SQL server, Oracle, MySQL, etc. You should try to keep to the standard as much as you can.
Select current_timestamp;
just use NOW()
Full reference: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html