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).
Related
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 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')
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
This must be simple to answer, but how do you expand in Oracle a year to a full date, e.g.
1996 to 1996-01-01 00:00:00 ?
EDIT
The data type of the year is char, and I want to end up by comparing this year to a string-date, e.g.
1996 <= '1998-31-12 12:04:35'
It is important that the expanded data is expanded in the same data Format (since I get the dates preformatted)
At the end I need something like this
WHERE ( to_date(table.year_char ,'YYYY') <= '1996-12-31 00:00:00')
or sth like this
WHERE ( to_char(to_date(table.year_char ,'YYYY')) <= '1996-12-31 00:00:00')
or anything which works
If you're starting with the year as a string and you want to end up with a DATE object, you use the TO_DATE() function; but you need to supply a dummy month or it'll default to the first day of the current month in the specified year:
select to_date('1996', 'YYYY') from dual;
May, 01 1996 00:00:00+0000
SQL Fiddle
With the month, and to make it clearer the day too, appended and a suitable format model:
select to_date('1996' ||'-01-01', 'YYYY-MM-DD') from dual
January, 01 1996 00:00:00+0000
SQL Fiddle. I've left the year and the '-01-01' literal separate and concatenated on the assumption that you'll be using a variable really...
In a WHERE clause, using the sample date you initially showed:
select * from dual
where to_date('1996' ||'-01-01', 'YYYY-MM-DD')
<= to_date('1998-31-12 12:04:35', 'YYYY-DD-MM HH24:MI:SS')
Or if you're actually comparing to a string as your second example suggests, just leave it as a string, as you want both sides of the comparison to be the same data type without any implicit conversion that might cause you problems later. The string you have fortunately has the data in a format that is comparable:
WHERE (table.year_char || '-01-01 00:00:00' <= '1996-12-31 00:00:00')
You could convert it to and back from a DATE but there isn't any benefit in doing so.
I have time, select cast(SYSDATETIME() AS time)
14:59:09.2834595
What is the way to truncate seconds?
14:59
Description
You can use the T-SQL function convert.
Sample
PRINT convert(varchar(5), SYSDATETIME(), 108)
will give you hh:mm
More Information
MSDN - CAST and CONVERT
If you want to truncate seconds and still have a T-SQL Date datatype, first convert the date into minutes from the date '0' and then add the minutes back to '0'. This answer doesn't require any additional parsing/converting. This method works to truncate other parts just change MINUTE.
Example:
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, '2016-01-01 23:22:56.997'), 0)
If you need to drop seconds off entirely, you can use the DATEPART() function (SQL Server) to strip out the hour and minute, then append it back together. (I like dknaack's solution more, if that works.)
SELECT CAST(DATEPART(hour, SYSDATETIME()) + ':' + DATEPART(minute, SYSDATETIME()) AS DATETIME)
select cast(left(cast(SYSDATETIME() AS time), 5) as time)