How to assign current date to scalar variable? - sap

I am trying the following syntax to assign current date to one HANA variable for further processing. I am getting an error
scalar variable is not allowed
DAY1 is declared as NVARCHAR(8) .
DAY1 := SELECT to_nvarchar ((current_date),'YYYYMMDD') from dummy ;
Please provide some input .

You could also do this:
declare DAY1 nvarchar(8);
SELECT to_nvarchar (current_date,'YYYYMMDD') into DAY1 from dummy ;
however, if the direct assignment works on your revision, go with that as you safe the parsing & execution of the above SQL statement.

Related

How can I use a variable on BigQuery SQL Editor?

While writing SQL queries on BigQuery UI, sometimes I do a lot of JOIN over multiple tables using many WHERE clauses with the same date condition for each table.
Whenever I need to see the results for a different date, I have to replace it at multiple locations in the query. I wonder if there is a simple way to use a variable in the BQ SQL Editor and pass it just once (top/bottom)?
This is true for all the complicated queries as we have to search throughout the query for variables to change.
While parameterized queries are not available in the Console. You can use Scripting, instead.
According to your need, you can use DECLARE and/or SET. It is stated in the documentation that:
DECLARE: Declares a variable of the specified type. If the DEFAULT clause is specified, the variable is initialised with the
value of the expression; if no DEFAULT clause is present, the variable
is initialised with the value NULL
The syntax is as follows:
#Declaring the variable's type and initialising the variable using DEFAULT
DECLARE variable STRING DEFAULT 'San Francisco';
SELECT * FROM `bigquery-public-data.san_francisco_bikeshare.bikeshare_regions`
#using the variable inside the where clause
WHERE name = variable
SET: Sets a variable to have the value of the provided expression, or sets multiple variables at the same time based on the
result of multiple expressions.
And the syntax, as below:
#Declaring the variable using Declare and its type
DECLARE variable STRING;
#Initialising the variable
SET variable = 'San Francisco';
SELECT * FROM `bigquery-public-data.san_francisco_bikeshare.bikeshare_regions`
#using the variable inside the where clause
WHERE name = variable
In both examples above, I have queried against a public dataset bigquery-public-data.san_francisco_bikeshare.bikeshare_regions. Also, both outputs are the same,
Row region_id name
1 3 San Francisco
In addition to the above example, more specifically to your case, when declaring a variable as date you can to it as follows:
DECLARE data1 DATE DEFAULT DATE(2019,02,15);
WITH sample AS(
SELECT DATE(2019,02,15) AS date_s, "Some other field!" AS string UNION ALL
SELECT DATE(2019,02,16) AS date_s, "Some other field!" AS string UNION ALL
SELECT DATE(2019,02,17) AS date_s, "Some other field!" AS string UNION ALL
SELECT DATE(2019,02,18) AS date_s, "Some other field!" AS string
)
SELECT * FROM sample
WHERE date_s = data1
And the output,
Row date_s string1
1 2019-02-15 Some other field!

Hive variable concatenation

I am facing problems in concatenating the value of a variable with a string .
my script contains the below
set hivevar:tab_dt= substr(date_sub(current_date,1),1,10);
CREATE TABLE default.udr_lt_bc_${hivevar:tab_dt}
(
trans_id double
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ",";
in the above, the variable tab_dt gets assigned correctly with yesterdays date in the format yyyymmdd.
but when i try to concatenate this variable in a table name with a static string, the script fails. it is not doing the concatenation .
Kindly provide a solution.
note: i tried the below too, which is erroring out too
set hivevar:tab_dt= substr(date_sub(current_date,1),1,10);
set hivevar:tab_nm1= default.udr_lt_bc_;
set hivevar:tab_name= concat(${hivevar:tab_dt},${hivevar:tab_nm1})
CREATE TABLE ${hivevar:tab_name}
(
trans_id double
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ",";
This too is returning an error.
Hive does not calculate expressions in the variables, substituting them as is.
Your create table expression results in this:
CREATE TABLE default.udr_lt_bc_substr(date_sub(current_date,1),1,10)...
Your second expression results in this:
CREATE TABLE concat(substr(date_sub(current_date,1),1,10),default.udr_lt_bc_)
Unfortunately Hive does not support such expressions in DDL.
I recommend to calculate this variable in a shell and pass as a --hivevar to the hive script.
For example in the sell script:
table_name=udr_lt_bc_$(date +'%Y_%m_%d' --date "-1 day")
#table_name is udr_lt_bc_2017_10_31 now
#call your script
hive -hivevar table_name="$table_name" -f your_script.hql
And then in your_script you can use variable:
CREATE TABLE default.${hivevar:table_name}
Note that '-' is not allowed in table names, that is why i used '_' instead.
For better understanding how Hive substitutes variables, try this:
hive> set hivevar:tab_dt= substr(date_sub(current_date,1),1,10);
hive> select ${hivevar:tab_dt};
OK
2017-10-31
Time taken: 1.406 seconds, Fetched: 1 row(s)
hive> select '${hivevar:tab_dt}';
OK
substr(date_sub(current_date,1),1,10)
Time taken: 0.087 seconds, Fetched: 1 row(s)
Note that in the first select statement the variable was substituted as is before execution and then calculated in the SQL. Second select statement prevent calculation because the variable is quoted and remains as is: substr(date_sub(current_date,1),1,10).
Another way in Hive:
select concat("table_",date_sub(from_unixtime(unix_timestamp(current_date,'yyyy-MM-dd'),'yyyy-MM-dd'),0));
Here, we can use above in a variable and use it as per our needs.

Oracle function doesn't recognize the input from a column of a table

We have pre-built function which I'm using in one of my oracle PLSQL Block to get the output of the function. I'm passing a column of a field as input to the function, but I'm getting the output of the function as 0, but if I pass hard coded value to test the function I get the desired value for the same function.
Is there an issue how I pass input to the function? I saw a very similar working code in a forum based on which I developed this PLSQL block
DECLARE
EXTRACT_DT_IN DATE := TO_DATE('07-JUL-17','DD-MON-RR');
BEGIN
insert into OPS.temp_table
Select POT.Order_no,
OPS.function_Order_Total(mvq.quoted_order_no)
FROM ORDER POT, mv_quoted mvq where POT.Order_no = mvq.quoted_order_no;
END;
I get the o/P for the above as
12345 0
23456 0
etc
But when I pass hard coded value to test the function as below I get the correct value
Select POT.Order_no,
OPS.function_Order_Total('12345')
FROM ORDER POT, mv_quoted mvq where POT.Order_no='12345'
I get O/P as
12345 14563.23
23456 12564.76
etc
Your help is very appreciated and thanks in advance

sqlldr - how to use if/then logic on a field?

I am loading a particular field that has date values. However, some of them are not complete... for example the values look like this
START_DATE
'2015-06-12'
'2016-12-24'
'2015-02' <--- this is what causes an error
'2016-01-03'
I have tried solving this by combining NULLIF with a LENGTH() function like so, but this is not allowed:
Start_date NULLIF LENGTH(:start_date)<10 to_date .....
this returns the error
Expecting positive integer or column name, found keyword length.
My main objective is to load dates that are of a proper format, and load NULL otherwise. What is the easiest way to do this within the ctl file? Can I avoid creating a custom function?
Say I have a table like this:
create table dateTable(START_DATE date)
and I need to load this file, where I want to insert NULL where the string does not match my pattern
'2016-12-28'
'2016-12-'
'2016-12-31'
I can add some logic in my ctl file to check the length of the string to load this way:
load data
infile dateTable.csv
into TABLE dateTable
fields enclosed by "'"
( START_DATE "to_date(case when length(:START_DATE) = 10 then :START_DATE end, 'yyyy-mm-dd')"
)
This simply checks the length of the string, but you can edit it anyway you need to build your own logic; notice that CASE gives NULL when no condition is matched, so this is equivalent to case when length(:START_DATE) = 10 then :START_DATE else NULL end.
This gives the following result:
SQL> select * from dateTable;
START_DATE
----------
28-DEC-16
31-DEC-16
In oracle, you can verify a string to make sure that is it valid date or not. Please Check IsDate function.

Syntax Errors in User Defined Function in SQL

I'm unable to understand why this code to calculate the Volume of a cube results in the Error Procedure or function 'CubicVolume' expects parameter '#CubeLength', which was not supplied..
CREATE FUNCTION CubicVolume
-- Input dimensions in centimeters
(
#CubeLength decimal(4,1),
#CubeWidth decimal(4,1),
#CubeHeight decimal(4,1)
)
RETURNS decimal(12,3)
AS
BEGIN
RETURN(#CubeLength * #CubeWidth * #CubeHeight)
END
I then try to Execute it using EXEC CubicVolume, which is how one would Execute a Stored Procedure.
I know some Syntax is wrong, but I'm unable to tell where.
Thank You
I'm assuming SQL Server.
You defined a scalar function, but are trying to invoke it like a stored procedure. The two are not the same. Stored procedures are basically batches of SQL statement that execute sequentially, and optionally send resultsets back to the client. Scalar functions behave like built-in functions (e.g., LEN(), CHARINDEX(), ABS(), etc.). That is, they belong in an expression inside a SELECT statement, or moral equivalent. So you need to use your function in a SELECT statement where you'd use a column or an expression. Examples:
SELECT dbo.CubicVolume(12, 5, 3) AS vol
-- result is: 180
SELECT CubeName, l, w, h FROM dbo.SomeTable WHERE dbo.CubicVolume(l, w, h) > 200
-- result could be something like: MyCube, 10, 10, 10 etc.
Basically, it's equivalent to using a mathematical expression based on columns or constants. You cannot use EXEC on it.