I have a requirement to retrieve result rows (from yesterday 3 pm to today 3 pm) each row has the DateTime column (timestamp).
how can I query this?
I prefer codeigniter active records
Try something like this:
$this->db->where('`DateTime` BETWEEN "'. strtotime("yesterday 15:00"). '" and "'. strtotime("today 15:00").'"');
Use the PHP function strtotime() to convert a textual date into a UNIX timestamp, and then use it to compare. Something like strtotime("yesterday 3pm") and strtotime("today 3pm") should also work. Be aware that unexpected results can depend on different time settings and locale in your database compared to the locale and installation setting on your server and PHP configuration.
If your timestamp is not a Unix Timestamp, you could try the following two alternatives:
$this->db->where("`DateTime` BETWEEN '". date('Y-m-d H:i:s', strtotime("yesterday 3pm")) ."'::timestamp AND '". date('Y-m-d H:i:s', strtotime("today 3pm"))."'::timestamp");
OR:
$this->db->where("`DateTime` BETWEEN '". date('Y-m-d H:i:s', strtotime("yesterday 3pm")) ."' AND '". date('Y-m-d H:i:s', strtotime("today 3pm"))."'");
Thanks to #Oto and #pozs discussion in this answer, an even simpler and better approach would be to use native postgresql syntax to compare timestamps:
$this->db->where("`DateTime` BETWEEN (current_date - 1) + time '15:00:00' AND current_date + time '15:00:00'");
EDIT: Note that if your column are named datetimeyou need to put backticks to not confuse with PHP:s datetime-function. Updated the example.
UPDATE: Added alternative syntax if you are not using a Unix Timestamp. And a third example using native postgresql syntax for comparing timestamps.
I don't know about codeigniter active records, for "pure" postgres, you can use this one:
SELECT * FROM yourtable
WHERE datetime BETWEEN
to_timestamp ((CURRENT_DATE - interval '1 day')::date||' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND
to_timestamp (CURRENT_DATE ||' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')
Related
Lets do the following:
SELECT to_timestamp(1453336500)::date
Then i get a date 2016-01-21
How does the function work backwards. With the date as input and the number (i guess seconds from 1970) as result?
You use extract:
SELECT extract(epoch FROM current_timestamp);
This is the date column I am using to get date
(CAST(substr(CAST(q2.hedte AS VARCHAR(8)),1,4) || '-' || substr(CAST(q2.hedte AS VARCHAR(8)),5,2) || '-' ||
substr(CAST(q2.hedte AS VARCHAR(8)),7,2) AS date)) As FLD5
However, this is also getting me time. How do I get rid of time?
When using Db2 in Oracle compatibility mode, DATEs are actually TIMESTAMPs. That is how Oracle works, and so is how Db2 has to work to. It does mean that you can't then create proper DATE only columns.
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.apdv.porting.doc/doc/r0053667.html
So short of converting your DATE that is really a TIMESTAMP into e.g. CHAR(10), you can't "get rid of the time".
E.g. try this
SUBSTR(TO_DATE(q2.hedte,'YYYYMMDD'),1,10)
I want to know what other MONTH formats exist except MM , MONTH or MON.
When the query below runs it gives me an error ORA-01843: not a valid month and I can understand why, because the server removes the "0" from the month "07" and leaves only the number "7", so the MM format is not the right one.
But which one is it?
select to_char(to_date(START_DATE,'MM/DD/YYYY '), 'DD-MM-YYYY')
from PER_ALL_PEOPLE_F
WHERE person_id=12345
The START_DATE column is DATE TYPE and it provides results like: 7/17/2012 .
Your assumption that the single-digit 7 for the month is a problem is not correct; Oracle is generally quite flexible and will happily parse a single digit month with the MM model:
select to_date('7/17/2012', 'MM/DD/YYYY') from dual;
TO_DATE('7/17/2012'
-------------------
2012-07-17 00:00:00
If start_date is already a DATE type then you should not be calling to_date() for it. You're doing an implicit conversion to a string using your NLS_DATE_FORMAT moodel, and then back to a date with your specified format. So really you're doing:
select to_char(to_date(to_char(START_DATE, <NLS_DATE_FORMAT>),
'MM/DD/YYYY '), 'DD-MM-YYYY')
If your NLS_DATE_FORMAT is something other than MM/DD/YYYY, e.g. DD-MON-YYYY, then you'll get an ORA-1843 error for at least some values.
You can see this with:
select to_date(date '2014-01-16', 'MM/DD/YYYY') from dual;
or the expanded:
select to_date(to_char(date '2014-01-16', 'DD-MON-YYYY'),
'MM/DD/YYYY') from dual;
Dates do not have any specific format, they're stored in an internal representation and then converted to a formatted string for display. You said your dates display like 7/12/2012, but given the error you're seeing your client seems to be doign that formatting, and it isn't related to the session NLS_DATE_FORMAT.
You only need to do:
select to_char(START_DATE, 'DD-MM-YYYY')
How do I convert the following format to UNIX timestamps?
A value like: 01-02-2015 10:20 PM should be converted to: 1418273999000
I did try to_timestamp function but its not working for me.
If your data is stored in a column called ts, in a table called data, do this:
select extract(epoch from ts) from data
To add Joe's answer, you can use date_part, i think it's syntax is clearer than 'extract'.
select date_part('epoch', ts) from data;
Adding to haoming answer,
for UNIX epoch this was my approach.
I also added a 180 day interval which can be changed/removed upon requirements.
date_part('epoch', (column_name + INTERVAL '180 day')) * 1000
The year is given as int: 2009, 2010 etc.
I want to convert this information to DATE (first January).
My solutions (I prefer the first one):
(year::char || '-01-01')::DATE
'0001-01-01' + ((year-1)::char || ' year')::interval
Is there a better (build in) or more elegant and faster solution?
(I'm currently working with PostgreSQL 8.4 but are also interested in more recent versions.)
I think this is the simplest way:
to_date(year::varchar, 'yyyy')
SELECT to_date(2011::text, 'YYYY');
Attention: any code based on default casting from text to date is bad. Somebody can change a default format datestyle to some unexpected value, and this code fails. Using to_date or to_timestamp is very preferable. to_date or to_timestamp is relative expensive, but it is rock robust.
to_date('01 Jan ' || year, 'DD Mon YYYY')
OR
SELECT (DATE (year || '-01-01'))
ref: http://www.postgresql.org/docs/current/interactive/functions-formatting.html
Note: I haven't worked with PostgreSQL
One possibility:
select year * '1 year'::interval + '0000-01-01'::date;
I like this way because it avoids conversion between text and integer (once all the constants are parsed).