The following function returns dates in this format, "2021-01-01T00:00:00.000Z" and all I need is just the date portion of "2021-01-01".
DATE_TRUNC(‘day’, timestamp)
The return value from your current call to DATE_TRUNC already functionally is the date 2021-01-01, which in timestamp form is at midnight. That being said, if you want to view as a date only, then maybe you want this:
SELECT FORMAT_DATETIME("%Y-%m-%d", DATE_TRUNC('day', timestamp))
FROM yourTable;
Another trick which might work on BigQuery is to cast the timestamp to a VARCHAR of the right length:
SELECT CAST(DATE_TRUNC('day', timestamp) AS VARCHAR(10))
FROM yourTable;
Related
I have Oracle SQL query where it has been used TRUNC(04-Aug-2017 15:35:32)
What will be parameter in Hive to replace TRUNC?
Assuming you have a date/time, you can use the to_date() function:
select to_date(col)
If you have a timestamp, say ts, you can use trunc():
trunc(ts, 'day')
This returns a timestamp, with the time portion stripped off - which is similar to what trunc() does in Oracle when given one argument only.
On the other hand, you can also convert the timestamp to a date:
to_date(ts)
This returns a date rather than a timestamp: that's a different datatype, that has no time component (Oracle does not have such a datatype: both date and timestamp store the date and time).
As per Oracle docs, The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day.
Similar is the function of to_date function in Hive.
It returns the date part of a timestamp string (pre-Hive 2.1.0): to_date("1970-01-01 00:00:00") = "1970-01-01".
If what you want is the timestamp(midnight timestamp : 00:00:00) along with the truncated date, you need to use some conversions as shown below:
cast(from_unixtime(unix_timestamp(to_date(<YOU_DATE_COL>), 'yyyy-MM-dd')) as timestamp)
The below works:
SELECT DATE_TRUNC('day', TIMESTAMP '2017-03-17 02:09:30')
But if I remove the "TIMESTAMP" part (as below) it doesn't. Why is this?
SELECT DATE_TRUNC('day', '2017-03-17 02:09:30')
From what I understand, the format should just be:
DATE_TRUNC('datepart', timestamp)
This simple format works in other situations..
The timestamp '2017-03-17' is a so-called timestamp-literal. The prefix timestamp is what makes this a timestamp and not a char. If you just have '2017-03-17', then it is a char-literal, which is not a timestamp, and DATE_TRUNC requires a datetime value.
Try this
select date_trunc('week', to_date('28/10/2020','dd/mm/yyyy'))
It will return date of monday for current week.
I have one table with two columns, id (string) and myTs (bigint).
I want to create a View with this two columns, but myTs must be date type:
CREATE myView AS SELECT id, myToDate(myTs) FROM myTable;
myTs is a timestamp, I want to transform it into a date with timezone Europe/Madrid and format yyyy-MM-dd HH:mm:ss.S.
For example, with myTs = 1167529028000, if I execute:
from_utc_timestamp(myTs, 'Europe/Madrid')
--Result: '2006-12-31 02:37:08.0'
I get the correct result but the type is timestamp, If I try cast it to date I lost the format:
cast(from_utc_timestamp(myTs, 'Europe/Madrid') as date)
--Result: '2006-12-31'
Another option:
date_format(from_utc_timestamp(myTs, 'Europe/Madrid'), 'yyyy-MM-dd HH:mm:ss.S')
--Result: '2006-12-31 02:37:08.0'
The result is String, if I cast as Date, again removes the format:
cast(date_format(from_utc_timestamp(myTs, 'Europe/Madrid'), 'yyyy-MM-dd HH:mm:ss.S') as Date)
--Result: '2006-12-31'
You explained your question pretty well. But unfortunately, it's not possible. If you define any field as date, it is simply a date. The moment you add hours, mins, secs - any of them, you have to define them as a timestamp. Date format will simply have year month and the day.
I have a temp table.
It has last_update column in 2/10/2018 6:01:50 PM datetime format.
How can I write THE BEST QUERY to display all information that's updated on 02-Oct-2018 day?
You can use trunc function
select *
from tab
where trunc(last_update) = date'2018-10-02'
It is preferable to avoid TRUNC especially if you have an index on the column last_update.
A simple where condition should be better and may be better performant.
WHERE last_update >= date '2018-10-02' AND
last_update < date '2018-10-02' + 1
Use trunc function for getting the same day:
trunc(last_update) = trunc(to_date('02-Oct-2018', 'DD-MONTH-YYYY'))
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day.
You can also use format DD-MON-YYYY
I have a table with two temporal columns. First (name is DATE) is storing the date (not including the time part) and therefor the datatype is DATE. Second column (name is TIME) is for storing the time in seconds and therefor the datatype is NUMBER.
I need to compare this two dates with a timestamp from another table. How can I calculate the date of the two columns (DATE and TIME) and compare to the timestamp of the other table?
I have tried to calculate the hours out of the time column and add it to the date column, but the output seems not correct:
SELECT to_date(date + (time/3600), 'dd-mm-yy hh24:mi:ss') FROM mytable;
The output is just the date, but not the time component.
You can use the INTERVAL DAY TO SECOND type:
SELECT your_date + NUMTODSINTERVAL(your_time_in_seconds, 'SECOND') FROM dual;
Example:
SELECT TRUNC(SYSDATE) + NUMTODSINTERVAL(39687, 'SECOND') FROM dual;
The calculated date with time is: 10-11-2013 11:01:27
This is a better idea than dividing your value by 3600 in my opinion, as you have an interval in seconds, so it feels natural to use an interval to represent your time, which can then be easily added to a column of DATE datatype.
Oracle Interval in Documentation
NUMTODSINTERVAL Function in documentation
date + (time/3600) is already a DATE, so you don't need to do to_date(). It does have the time part you added though, you just aren't displaying it. If you want to output that as a string in the format you've shown, use to_char() instead:
SELECT to_char(date + (time/3600), 'dd-mm-yy hh24:mi:ss') FROM mytable;
... except that if time is actually in seconds, you need to divide by 86400 (24x60x60), not 3600. At the moment you're relying on your client's default date format, probably NLS_DATE_FORMAT, which doesn't include the time portion from what you've said. That doesn't mean the time isn't there, it just isn't displayed.
But that is just for display. Leave it as a date, by just adding the two values, when comparing against you timestamp, e.g.
WHERE date + (time/86400) < systimestamp
Try like this,
SELECT TO_DATE('11/11/2013','dd/mm/yyyy') + 3600/60/60/24 FROM DUAL;
Your query,
SELECT date + time/60/60/24 FROM mytable;
try using to_timestamp instead of to_date