Minimum of Timestamp BigQuery Standard SQL - sql

I get the following error when running this simple query in Bigquery with standard SQL:
SELECT MIN(created) as mm FROM `projectId.ds.User`
Column created has type cloud.helix.Timestamp, which differs from the expected type of INT64 Dismiss
COMPOSE QUERY
The created field has datatype Timestamp, is nullable, but contains no null values.
This query works however in Legacy SQL:
SELECT MIN(created) as mm FROM [projectId:ds.User]
Any advice?

A fix has been rolled out by Google addressing this error.
https://code.google.com/p/google-bigquery/issues/detail?id=841

Related

To_chars in JPA

I have native query added in JPA and want to format the date as yyyy-MM-dd
select to_char(date,'yyyy-MM-dd') from table1 where date > sysdate;
This date data is stored as either varchar or timestamp in different tables. So I converted into to_char with this format to make it generic. When ran on Oracle DB it worked but when executed on JPA it throws invalid number.
I tried using Operator instead it threw Operator is invalid.
Thanks

Updating invalid timestamps in Google BigQuery table

My table has a few invalid timestamps that I think are too precise (maybe beyond microseconds) for BigQuery.
When I tried updating the table with the following query using Standard SQL:
UPDATE mytable
SET event_time = TIMESTAMP(DATETIME(TIMESTAMP_MILLIS(CAST(SUBSTR(SAFE_CAST(UNIX_MILLIS(event_time) AS string),1,13) AS int64))))
WHERE DATE(logtime) BETWEEN "2018-03-21" AND "2018-03-23"
AND event_time IS NOT NULL
I get the invalid timestamp error:
Cannot return an invalid timestamp value of 1521738691071000064 microseconds relative to the Unix epoch. The range of valid timestamp values is [0001-01-1 00:00:00, 9999-12-31 23:59:59.999999]; error in writing field event_time
I think the problem is the SET event_time = part, but I don't know how to get around setting the values in the event_time column without referring to it.
Anyone have any ideas on how to resolve this?
Necessity is the mother of invention. For anyone who has a similar issue, I've figured out a workaround.
Create a new table of the affected rows (include this in the WHERE clause: LENGTH(CAST(UNIX_MILLIS(event_time) as string)) > 13, while transforming the
invalid timestamp into a valid format using TIMESTAMP(DATETIME(TIMESTAMP_MILLIS(CAST(SUBSTR(SAFE_CAST(UNIX_MILLIS(event_time) AS string),1,13) AS int64))))
Delete the affected rows from the original table using the WHERE clause mentioned above.
Insert all rows from the new table back into the original table.
A lot more work, but it should do the trick.

Error in MS SQl 11.0 given code: 'unix_timestamp' is not a recognized built-in function name

I got the SQL query from another developer and there was unix_timestamp function, because this query returns a list of objects from database in the exact period of time. Query doesn't work because of the SQL error 195: 'unix_timestamp' is not a recognized built-in function.
n.created>unix_timestamp('2019-02-28') and n.created<unix_timestamp('2019-09-01')
"unix_timestamp" is a built in function on MySQL - it isn't on SQLserver.
Here's the code to get the unix timestamp now:
SELECT DATEDIFF(SECOND,'1970-01-01', GETUTCDATE()) AS unix_timestamp
You can either create a user defined function to do this and use the query as-is or you can just modify the query like so:
n.created>(DATEDIFF(SECOND,'1970-01-01', '2019-02-28')) and n.created<(DATEDIFF(SECOND,'1970-01-01','2019-09-01'))
Unix timestamps are not particularly useful in SQL Server. You can add a computed column to convert this to a datetime data type:
alter table t add created_dt as (dateadd(second, created, '1970-01-01')) persisted;
create index t_created_dt on t(created_dt);
This then allows you to write:
n.created_dt > '2019-02-28' and
n.created_dt < '2019-09-01';
Otherwise, the solution by motobatsu is a good solution.

Date in where clause

I'm having to reverse engineer an Oracle query. I'm familiar with SQL but not Oracle and I'm using SQL OPENQUERY to the linked Oracle server. I've gone through the larger portions of the query and figured most of the syntax and getting results but when I get to the following in the where clause I get "missing expression" error. (simplified for clarity)
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= {1}
I can query the table and see that the column USER_DATE is indeed a date field so I don't understand the meaning of >= {1}. This query came to me as "this is how the other dept uses this query, make it work for us" and I don't have access to this other dept. Can someone explain how this supposedly works?
The {1} is just a placeholder for an actual value. Once you've populated this with a proper date value it will run just fine like this:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= '01/01/2019'
You can also use the to_date function for a specific format:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= to_date('01/01/2019', 'mm/dd/yyyy')
Most likely it is a matter of default format of date in your remote Oracle database is different from the "other" database. Most databases allow you to select date into string and also.conpare date to string. The comparison implicitly converts string to date.
Most likely that implicit conversion is failing here. Correct way would be to see if the parameter is a really a string and in what format is date presented there. Then use to_date on right side around {1}.
To make it not error temporarily you can put to_char on left side. Ofcourse your output will be incorrect but since SQL would run, you may be able to troubleshoot

Redshift - Issue displaying time difference in table stored in table

I am trying to find the time difference between two time stamps and store it in a column. When I check the output in the table, I see the value to be a huge number and not the difference in day/hours. I am using amazon redshift as the database.
Data_type of time_duration column : varchar
Given below is the sample:
order_no,order_date,complain_date,time_duration
1001,2018-03-10 04:00:00,2018-03-11 07:00:00,97200000000
But I am expecting time_duration column to show 1 day,3 hours
This issue happens when I store the time_duration in a table and then query to view the output.
Could anyone assist.
Thanks.
Do it following way, it will give hours difference
select datediff(hour,order_date,complain_date) as diff_in_hour from your_table ;
If you want to do it day, do it following way
select datediff(day,order_date,complain_date) as diff_in_day from your_table;
You could use datediff function to update your table column time_duration.
Refer Redshift documentation for more details.