Default value for Hive script parameter - hive

I'm wanting to use a default value for a parameter if it has not been set
e.g. for hive -d param1=2014-06-20
I can retrieve the value, i.e.
hive> select '${param1}';
OK
2014-06-20
but for a variable which hasn't been set the result is
hive> select '${param2}';
OK
${param2}
I would have expected null, etc
Ultimately I want to use a default value for a parameter if it hasn't been passed in, e.g.
SELECT * FROM test_table t
WHERE t.date < COALESCE(${param1}, CURRENT_DATE)

There doesn't seem to be a direct documented way to carry this out but there the parameter value can be tested and a default returned if the value equals the name. i.e.
hive> select if(locate('${par', '${param2}')==0, '${param2}', CURRENT_DATE);
OK
2018-03-09

Related

Issues trying to write an SQL query on snowflake that checks if datatype is timestamp

I am trying to write an sql on snowflake that checks if a column data type (value) is timestamp
id
value
111
2022-01-14 15:03:43:000
select value from cat
where is_date(value)
if value is timestamp, to return 1 and 0 if its not a timestamp
You could use TRY_TO_DATE/TRY_TO_TIMESTAMP functions:
SELECT value
FROM tab
WHERE TRY_TO_DATE(value) IS NOT NULL;
If a specific date format is expected it could be provided i.e.: TRY_TO_DATE(value, 'dd-MM-yyyy')
You can use below format and try_to_timestamp returns NULL in case if it is invalida data and also you can apply format of Timestamp.
select try_to_timestamp('2022-01-14 15:03:43','YYYY-MM-DD HH24:MI:SS'), try_to_date('Invalid');

Is there a native technique in PostgreSQL to force "timestamp without time zone" to not include milliseconds?

I am using PostgreSQL 9.6.17. (Migrating from MySQL)
A java program writes dates inside a table. The date formats is the following:
2019-01-01 09:00:00
But it can also be 2019-01-01 09:00:00.00 or 2019-01-01 09:00:00.000 when inserted in the database, which messes up my date management in my program when retrieved.
On insertion, I would like all the date to have the very same format: 2019-01-01 09:00:00. The datatype used by the column is timestamp without a time zone.
How can I tell postgresql to not input milliseconds in timestamp without timezone via configuration or SQL query ?
This data types doc does not provide any information about that.
Quote from the manual
time, timestamp, and interval accept an optional precision value p which specifies the number of fractional digits retained in the seconds field. By default, there is no explicit bound on precision. The allowed range of p is from 0 to 6
So just define your column as timestamp(0), e.g.:
create table foo
(
some_timestamp timestamp(0)
);
If you have an existing table with data, you can simply ALTER the column:
alter table some_table
alter column some_timestamp type timestamp(0);
If you now insert a timestamp with milliseconds, the value will be rounded to remove the milliseconds.
Note that technically you still have milliseconds in the stored value, but they are always set to 0
You can cast:
mytimestamptz::timestamp(0)
This will round the result to the nearest second. If you want to truncate instead:
date_trunc('second', mytimestamp)
Retrieve as a timestamp and in the application querying the database, manage the precision however you want. eg. via JDBC you'll get a Java LocalDateTime object, in Python you'll get a datetime object.
If you want to retrieve timestamps as strings, there are lots of formatting options available
SELECT to_char(when, 'YYYY-MM-DD HH24:MI:SS') FROM mytable
Drop any milliseconds on input by specifying the precision option to your timestamp type:
CREATE TABLE mytable (..., when TIMESTAMP(0));

Cannot DELETE or UPDATE row in Google BigQuery due to a timestamp for a field being outside the range of Standard SQL timestamp

In Google BigQuery, I have a timestamp field which has the year 20195. This is ofcourse causing errors as it's outside the Standard SQL Timestamp range. However I cannot update this records or delete this record as the error, even by using SAFE_CAST. For example, i've tried:
UPDATE [table] SET DateField = SAFE_CAST('2019-01-01 00:00:00 UTC' AS TIMESTAMP)...
"Cannot return an invalid timestamp value of 575129030400000000 microseconds relative to the Unix epoch." pops up. I know how to SELECT and return a null instead of an error using Safe-Cast, but cannot use this to update/delete.
Question is: How can I delete or update this field successfully with out any errors?
You can use the safe. prefix to return NULL instead:
select safe.timestamp_micros(575129030400000000)
You can convert to microseconds (which doesn't result in a bounds check) and then attempt to convert back, deleting the row if the safe conversion results in NULL:
DELETE dataset.table
WHERE ts IS NOT NULL
AND SAFE.TIMESTAMP_MICROS(UNIX_MICROS(ts)) IS NULL
Or if you want to set such values to NULL instead:
UPDATE dataset.table
SET ts = SAFE.TIMESTAMP_MICROS(UNIX_MICROS(ts))
WHERE true
If you want to repair bad values using UPDATE you have to apply it across the whole table, using a conditional. Elliot's answer is correct but I can't can't comment or upvote so I'll elaborate slightly and answer this question:
UPDATE `my.table`
SET DateField = IF(SAFE.DATE(DateField) IS NULL AND DateField IS NOT NULL, TIMESTAMP('2019-01-01 00:00:00 UTC'), DateField)
WHERE true
The WHERE true is the secret sauce, which isn't obvious from Elliot's answer. If you try to do WHERE id = 1234 or whatever it will continue to give the same error. SAFE.DATE is a concise way of checking for valid dates and will return NULL if they are invalid, however your field may be nullable which is why I have added the null check.

executeQuery() in JDBC is returning incorrect rows

The following query is returning one extra row than when the query is run in database:
select distinct employeeId as facilityId,0 as numberOfRequests
from If_User
where role='Facility Staff'
and employeeId not in (select distinct facilityId
from IF_Request
where requestStatus='C'
and allocationTime like '09-12-16%');
This is returned in JDBC:
FacilityID numberOfRequests
8585 0
7427 0
2545 0
Where the actual data in SQLDeveloper is:
FacilityID numberOfRequests
8585 0
7427 0
This is caused by relying on the implicit data type conversion that is triggered by using LIKE on a DATE column. LIKE is only for character values. The LIKE forces Oracle to convert the date value to a string. When run from SQL Developer other rules apply for the evil implicit data type conversion then when you run this through your application.
Use proper date literal instead e.g.
where trunc(allocationtime) = date '2016-12-09'
If that needs to be a parameter from within Java, use a PreparedStatement and pass an instance of java.sql.Timestamp using setTimestamp() do not pass String values for DATE or TIMESTAMP parameters.
Oracle's DATE always contains a time part and and therefor you have to use trunc() on the column value to normalize that to 00:00:00
Never use LIKE on a DATE, TIMESTAMP or NUMBER column!

How to get update record based on given record in oracle?

I want to get record from DB based on given date value. I used Timestamp as coulmn type. I used query as
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12')
Getting empty table. where as records were present in the table.
Please, sort out me from this issue.
Thanks in advance.
May be your table contains dates with time part so try to use:
SELECT *
FROM table1
WHERE Trunc(LAST_UPDT_S) = to_date('23-AUG-12')
Try this,
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12', 'DD-MON-YY')
SEE: TO_DATE Manual
Generally it's better to use a time range - this opens the possibility of using an index if one exists:
SELECT *
FROM table1
WHERE LAST_UPDT_S >= DATE '2012-08-23'
AND LAST_UPDT_S < DATE '2012-08-24'
(Also, it's better to not use TO_DATE without specifying the date format, otherwise it will use whatever the current session's NLS setting happens to be.)