Phoenix: Convert String column to Integer column - hadoop

I am looking for a Built-in UDF or any other method to convert values of a string column to integer in my phoenix table for sorting using SELECT and ORDER BY. I searched in the apache language Manual, but no use. Any other suggestions also welcome.
Actual Query
select "values" from "test_table"
I tried below approach but did not work
select TO_NUMBER("values", '\u00A4') from "test_table"

TO_NUMBER returns decimal but you can cast the result to INTEGER
SELECT CAST(TO_NUMBER(MY_COLUMN) AS INTEGER) FROM MY_DB

select TO_NUMBER(values) from test_table;
see https://phoenix.apache.org/language/functions.html#to_number

Related

Converting binary to varchar in PL/SQL

My table consists of a field that is binary_double. However I want to convert it to varchar.
The column currently has sample values stored as binary_double. It looks like this:
69623829
I want the result to be returned in the same format when I convert it to varchar. So expected output is like this:
69623829
I have tried this:
select
convert(varchar(20),r.col_14,1)
from sample_table r
The error message is:
ORA-00936: missing expression
p.s : I am just starting off in PL/SQL
I suggest using TO_CHAR function. See TO_CHAR.
As in
SELECT TO_CHAR(col14,'99999999') FROM sample_table
Please see linked documentation for the desired format according to your requirements.
Use cast function:
CAST ( { expr | ( subquery ) | MULTISET ( subquery ) } AS type_name )
so:
select CAST(col_14 as varchar2(20)) from sample_tabe
Oracle live compiler - CAST as varchar2
For more, check this link:
CAST function

conversion function is required?

In my oracle database contains table Text_Details, field F_TEXT is now an NCLOB field so anywhere this is used in WHERE-clause comparisons may require casting it to other datatypes (NVARCHAR2(4000)probably) for those comparisons.
Text_Details
---------------
Name Data type
Id Number
F_Text NCLOB
any casting is required in where clause comparison with other data types(NVARCHAR2(4000))or any other data types?.Please help.
You can't put a LOBs in the WHERE clause. From the documentation:
however you can use to_char(F_Text) in your where clause like this:
SELECT * FROM WHERE to_char(F_Text) = 'something';

How to write date condition on where clause in oracle

I have data in the date column as below.
reportDate
21-Jan-17
02-FEB-17
I want to write a query to fetch data for 01/21/2017?
Below query not working in Oracle.
SELECT * FROM tablename where reportDate=to_date('01/21/2017','mm/dd/yyyy')
What is the data type of reportDate? It may be DATE or VARCHAR2 and there is no way to know by just looking at it.
Run describe table_name (where table_name is the name of the table that contains this column) and see what it says.
If it's a VARCHAR2 then you need to convert it to a date as well. Use the proper format model: 'dd-Mon-rr'.
If it's DATE, it is possible it has time-of-day component; you could apply trunc() to it, but it is better to avoid calling functions on your columns if you can avoid it, for speed. In this case (if it's really DATE data type) the where condition should be
where report_date >= to_date('01/21/2017','mm/dd/yyyy')
and report_date < to_date('01/21/2017','mm/dd/yyyy') + 1
Note that the date on the right-hand side can also be written, better, as
date '2017-01-21'
(this is the ANSI standard date literal, which requires the key word date and exactly the format shown, since it doesn't use a format model; use - as separator and the format yyyy-mm-dd.)
The query should be something like this
SELECT *
FROM table_name
WHERE TRUNC(column_name) = TO_DATE('21-JAN-17', 'DD-MON-RR');
The TRUNC function returns a date value specific to that column.
The o/p which I got when I executed in sqldeveloper
https://i.stack.imgur.com/blDCw.png

how to insert uniontype in hive

I read the famous example about union type in hive
CREATE TABLE union_test(foo UNIONTYPE<int, double, array<string>, struct<a:int,b:string>>);
SELECT foo FROM union_test;
{0:1}
{1:2.0}
{2:["three","four"]}
{3:{"a":5,"b":"five"}}
{2:["six","seven"]}
{3:{"a":8,"b":"eight"}}
{0:9}
Ok.. great.
what is the syntax in hive sql to insert these lines ?
I tried insert into union_test values (1.0)
SemanticException [Error 10044]: Line 1:12 Cannot insert into target table because column number/types are different 'union_test': Cannot convert column 0 from string to uniontype,struct>.
On the other hand, if I create one table with a double, how can I feed it with union_test table ?
Surely there is a tip.
Thanks
Did you consider looking at the documentation?
Straight from Hive Language Manual UDF, under "Complex type constructors"...
create_union (tag, val1, val2, ...)
Creates a union type with the value that is being pointed to by the tag parameter.
OK, that explanation about the "tag parameter" is very cryptic.
For examples, just look at the bottom of that blog post and/or at the answer to that question on the HortonWorks forum.
CREATE table testtable5(c1 integer,c2 uniontype<integer,string>);
INSERT INTO testtable5 VALUES(5,create_union(0,1,'testing'));
SELECT create_union(if(c1<0,0,1),c1,c2) from testtable5;
INSERT INTO testtable5 VALUES(1,cretae_union(1,1,'testing'));
SELECT create_union(if(c1<0,0,1),c1,c2) from testtable5;

SQL Server 2008 search for date

I need to search rows entered on a specific date.
However the datatype of column I need to search on is datetime, and the datatype of argument is Date.
I can use the the query like
Select result
from table
where
convert(date, Mycolumn) = #selectedDate
but this would affect the SARGability of the query and will not use indexes created on mycolumn.
I was trying to use the following query:
Select result
from table
where
Mycolumn
BETWEEN #selectedDate AND Dateadd(s, -1, Dateadd(D, 1, #selectedDate))
However this does not work since the #selectedDate is Date type and a second can't be added or removed.
Can someone help me with a working query?
Thanks.
It is my understanding that using:
convert(date, Mycolumn) = #selectedDate
is SARGable. It will use the index on Mycolumn (if one exists). This can easily be confirmed by using the execution plan.
Select result
from table
where
Mycolumn >= #selectedDate
AND Mycolumn < Dateadd(D, 1, #selectedDate)
If you need to do these searches a lot, you could add a computed, persisted column that does the conversion to DATE, put an index on it and then search on that column
ALTER TABLE dbo.YourTable
ADD DateOnly AS CAST(MyColumn AS DATE) PERSISTED
Since it's persisted, it's (re-)calculated only when the MyColumn value changes, e.g. it's not a "hidden" call to a stored function. Since it's persisted, it can also be indexed and used just like any other regular column:
CREATE NONCLUSTERED INDEX IX01_YourTable_DateOnly ON dbo.YourTable(DateOnly)
and then do:
SELECT result FROM dbo.YourTable WHERE DateOnly = #SelectedDate
Since that additional info is stored in the table, you'll be using a bit more storage - so you're doing the classic "space vs. speed" trade-off; you need a bit more space, but you get more speed out of it.

Resources