Is there any way in BigQuery to get the current UTC timezone offset from a timezone name? For example using the input:
`Australia/Victoria`
How could I currently return:
+10:00
Below example for BigQuery STandard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'Australia/Victoria' tz_string
)
SELECT tz_string, DATETIME_DIFF(CURRENT_DATETIME(tz_string), CURRENT_DATETIME(), hour) tz_hours
FROM `project.dataset.table`
with result
Row tz_string tz_hours
1 Australia/Victoria 10
Another way to do this is to use the (at least, now) built in FORMAT_TIMESTAMP() function and the %Ez format element.
SELECT FORMAT_TIMESTAMP('%Ez', CURRENT_TIMESTAMP(), 'Australia/Victoria');
Result:
+11:00
Related
pdt.startTime is datetime
s_first.FromTimeOfDay is a time
I want to subtract the time drom the datetime. When i run the code below, Snowflake gives me this error invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
select (pdt.StartTime - (SELECT s_first.FromTimeOfDay::datetime FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
When i try this:
select (pdt.StartTime::TIMESTAMP_NTZ(9) - (SELECT s_first.FromTimeOfDay::TIMESTAMP_NTZ(9) FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
I get more or less the same error: invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
How do I convert the time into a datetime format so that I can subtract the two. It doesnt seem to me that there is a clear way to convert time into datetime in snowflake.
Is this what you're after?
select current_timestamp() as sample_timestamp
, time(sample_timestamp) as sample_time
, date(sample_timestamp) as sample_date;
A user pointed me in the right direction. i didnt realize i could use "dateadd" to also subtract time.
dateadd(HOUR, - (HOUR(current_timestamp())), temp.DateTime)
there are multiple timestamps column in oracle Sql , Now in want to convert 12 hour format time into 24 hour format time like 01-FEB-18 01.00.21.645000000 PM should be 05-01-0018 13:12:44. but I need to convert hours into 24 hours time format , I am using the below statement.
SELECT TO_CHAR(TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM'),'DD-MM-YYYY hh24:mi:ss')
FROM FACT_WORK_ITEM_ACTION
WHERE TRUNC(PERFORMED_TIMESTAMP)>= '05-JUN-18'
But still i am getting the below error .
Error Code....
ORA-01855: AM/A.M. or PM/P.M. required
01855. 00000 - "AM/A.M. or PM/P.M. required"
*Cause:
*Action:
SELECT to_char( TO_timestamp('01-FEB-18 01.21.01.645','dd-mon-yy hh12.mi.ss.ff'),'dd-mm-yyyy hh24:mi:ss')
from dual;
Your PERFORMED_TIMESTAMP is already a timestamp, which doesn't have any intrinsic human-readable format - Oracle uses its own internal representation when storing the values.
You are just seeing that timestamp displayed by your client with formatting taken from the session NLS settings (or, possibly, overridden by the client itself).
The error is because you are doing unnecessary data type conversions and relying on implicit conversion and NLS session settings. When you do
TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM')
you are first implicitly converting the PERFORMED_TIMESTAMP to a string, again using your session NLS settings, so you're effectively doing:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP),'DD-MON-YYYY hh:mi:ss AM')
which given the string values in your question is actually:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-RR HH.MI.SS.FF AM'),'DD-MON-YYYY hh:mi:ss AM')
The inner part of that will give a string like "01-FEB-18 01.00.21.645000000 PM" which is what you see when you query the table and the client does its own formatting. Passing that string back into to_date() gives the error you see, because the fractional seconds are appearing in the string where it's expecting to see the AM/PM marker:
SELECT TO_DATE('01-FEB-18 01.00.21.645000000 PM','DD-MON-YYYY hh:mi:ss AM')
FROM DUAL;
Error report -
ORA-01855: AM/A.M. or PM/P.M. required
You could replace the implcit conversion and session values with explicit conversion and format masks:
TO_CHAR(TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-YYYY hh:mi:ss AM'),'DD-MON-YYYY hh:mi:ss AM'), 'DD-MM-YYYY hh24:mi:ss')
But hopefully you can tell that is doing a lot more work than it needs to -and having to use the same format mask twice is also a sign that you're doing something wrong.
The real solution is just to simplify it. You don't need to convert to a string and back to a date at all. Just do:
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
Notice that I've also removed the trunc() and the comparison with a string; this now compares your timestamp column as a timestamp, which will make any index on that happier too.
Quick demo with a CTE to provide dummy data:
with FACT_WORK_ITEM_ACTION(PERFORMED_TIMESTAMP) as (
select timestamp '2018-06-01 13:00:21.645000000' from dual
union all select timestamp '2018-06-06 13:00:21.645000000' from dual
)
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
TO_CHAR(PERFORMED_T
-------------------
06-06-2018 13:00:21
I'm trying to query what happened between today and yesterday. To example on the 17th of June 2016 it would look like:
SELECT * FROM Inspection_Log WHERE date_time BETWEEN '2016-6-16' AND '2016-6-17'
But these days are relative, and this won't work say tomorrow, or really every again. So I've encountered this page where tells me now to use DATE as it's just a polite wrapper around strftime.
But here is my current issue:
This query works:
>SELECT COUNT(*) FROM Inspection_Log WHERE date_time BETWEEN '2016-6-16' AND '2016-6-17'
535
But when I use date('yada', '+1 day')
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND DATE('2016-6-16','+1 day')
0
So I try with strftime
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND strftime('%Y-%M-%D','2016-6-16','+1 day')
0
So I try with datetime
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND datetime('2016-6-16','+1 day')
0
Digging into this here is what i see
SELECT time('now')
'2016-06-24'
SELECT date('now')
'2016-06-24'
SELECT date('now','-1 day')
'2016-06-23'
SELECT date('2016-6-24','-1 day')
NONE
What am I doing wrong?
You need to change: AND strftime('%Y-%M-%D','2016-6-16','+1 day') for AND strftime('%Y-%m-%d','2016-06-16','+1 day').
1 - You should use '%Y-%m-%d' for the first parameter 'YYYY-MM-DD'.
The format string supports the most common substitutions found in the strftime() function from the standard C library plus two new substitutions, %f and %J. The complete list link
2 - A time string must be follow the format: YYYY-MM-DD, then you need to use '2016-06-16'.
There is a question and answer : SQL Select between dates
Okay so I was totally and completely wrong.
My scheme looks like this:
CREATE TABLE InspectionLog(
date_time DATE,
station_name TEXT,
inspection TEXT,
barcode_part_number TEXT,
bus_part_number TEXT,
barcode_serial_number TEXT,
bus_serial_number TEXT,
rework_operation TEXT,
status TEXT,
ng_description TEXT
)
DATE is not a valid data type. It is actually a high level wrapper around INTEGER and TEXT depending on the data placed into it. Sqlite3 defaults to TEXT.
What this means is when I perform an insert/update which does something similar to:
date_time = '2016-6-16'
This is valid as date_time is really TEXT not DATE. And when I preform a search that uses the DATE data type, it will skip any row which isn't a DATE.
The long version is. I inserted ~250MB incorrectly formatted into this table. After fixing my tests and functions so my inserts always have 2 day/month digits the majority of the OP's time queries work correctly.
Select distinct Format(DateAdd(""s""," & columnname & ",""1/1/1980 12:00:00 AM""), 'dd-MMM-yyyy') as A
I have assumed that the seconds to add and the original date are hard coded values below whilst awaiting clarifications requested in the comments.
To add a number of seconds to a date you can use:
select datetime('1980-01-01 00:00:00', "345000 seconds");
This gives the result: 1980-01-04 23:50:00
The example above is just under 4 days in seconds, if you want to truncate the result to just the date as implied by the query in your questions then you can wrap this inside a date function. However, this would give the result in the format "YYYY-MM-DD" rather than "DD-MMM-YYYY" as your access query does.
Unfortunately I cannot find any native SQLite function to convert a numeric month value to mmm format. You can do this manually with replace (similar to the answer to this question), but this is a bit messy.
If you are happy to live with the numeric months then you can simply use:
select strftime('%d-%m-%Y', '1980-01-01 00:00:00', "345000 seconds");
This gives the result: 04-01-1980
More information on the SQLite date / time functions can be found here.
Hi am trying to convert column defined as date to date with format DD-MON-YYYY but can't get it working
select
to_date(to_char
(DESIGN_COMPLETION_DATE,'DD-MON-YYYY'),'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
Also Tried
Select to_date(DESIGN_COMPLETION_DATE,'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
What works is, but I can't do any calculations on it as it is char
Select to_char(DESIGN_COMPLETION_DATE,'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
Thanks
If you have a DATE that includes a time portion but are only interested in the actual date part, you can use the trunc function:
select trunc(design_completion_date) as assessment_completion_date from nbi_dates
An example of the difference using sysdate; notice the time on the trunc'd version has been set to midnight:
SQL> alter session set nls_date_format = 'DD/MM/YYYY HH24:MI:SS';
Session altered.
SQL> select sysdate, trunc(sysdate) from dual;
SYSDATE TRUNC(SYSDATE)
------------------- -------------------
11/04/2013 15:14:31 11/04/2013 00:00:00
A DATE has no inherent format. DD-MON-YYYY is a format mask applied to display the date, or to convert it to a string representation, which is usually only necessary for display anyway. What you have as your third option is right for that purpose, but not if you want to do any further date calculations with the result.
Select to_date(to_char(DESIGN_COMPLETION_DATE,'DD-MON-YYYY'),'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
or simply (in case you want date object for calculations but not for rendereing)
Select DESIGN_COMPLETION_DATE as Assessment_Completion_Date
from nbi_dates