Hive replacing a value with null value in hive - hive

I have a hive table where instead of NULL, it represents null record as string with value 'N/A',
Is there any query that will convert that 'N/A' into NULL value datatype in hive.

You can try if or case when or decode like below -
select
case when mycol='N/A' then NULL else mycol end as mycol,
if(mycol='N/A', NULL,mycol) as mycol_if
from mytable

The "CASE WHEN" syntax may be helpful.
Create hive table and import data.
hive> create table mytable(
name string,
mycol string
)
row format delimited fields terminated by "\t";
hive> load data local inpath '/opt/tempdata/mytable.txt' into table mytable;
sql syntax:
hive> select
name,
case mycol when 'N/A' then NULL else mycol end as mycol
from mytable
Hope it helps you.

Related

Insert into enum type using IF statement psql

Im copying data in from a csv file, I want insert a value which depends on if the data in the csv file reads yes or no
CREATE TYPE colour_type as ENUM ('red', 'blue');
CREATE TABLE colour_table
(
colour colour_type
);
CREATE TABLE Dummy
(
colour_type_dummy varchar(40)
);
\copy TABLE FROM 'colours.csv' WITH (FORMAT CSV, HEADER);
-- This is the part where I cant figure out the syntax for the IF statement
INSERT INTO colour_table (colour)
SELECT 'red' IF colour_type_dummy = 'yes' else 'blue' FROM Dummy
You can try to use CASE WHEN expression
INSERT INTO colour_table (colour)
SELECT CASE WHEN colour_type_dummy = 'yes' THEN 'red'::colour_type ELSE 'blue'::colour_type END
FROM Dummy
sqlfiddle

Handling null for char(1) and varcar(2) in hive

I am reading a flat file in hive and i have null values coming in file like below
a|b|null|null|d
and when I create table on top of this with below datatypes
a char(1),b char(1),c char(1),varchar2(2),char(1)
and the value in table coming like this
a,b,n,nu,d
The oneway I can do this is to make the datatype as varchar2(4) and add check at null.
But is there any other way i can do this.
SerDe treats 'null' strings as normal values, no difference between value 'a' and 'null'.
Try to add 'serialization.null.format'='null' property to your table definition:
ALTER TABLE mytable SET tblproperties('serialization.null.format'='null');
Another approach is to use STRING data type and case statements is select:
select case when col = 'null' then null end as col
...

Convert 'NULL' to Date in SQL

I have a column in my table called startdate. It is in string format. Most of the fields are 'NULL'. I am copying this column to another table which data type is 'Date'.
How can I convert all the values from string to Date in SQL.
I have tried this code:
INSERT INTO Destination_Table [new_date]
SELECT CONVERT(DATE,[startdate],103)
FROM Source_Table
nullif([startdate],'NULL') returns [startdate] unless it equals to 'NULL' and then it returns NULL (a real NULL, not the string 'NULL')
INSERT INTO Destination_Table [new_date]
SELECT CONVERT(DATE,nullif([startdate],'NULL'),103)
from Source_Table
For learning purposes, here are some expressions with the same results:
nullif(x,y)
case when x=y then null else x end
case x when y then null else x end
It looks like you are using MSSQL. If you are using MSSQL 2012, the following code should work :
INSERT INTO Destination_Table [new_date]
SELECT IIF([startdate] = "NULL", null, CONVERT(DATE,[startdate],103))
FROM Source_Table
What this does, is use the IIF() method to check the value of [startdate] and if the value is the text "NULL", then return the actual null value which can be allowed in most fields unless you have null disabled on the Destination_Table.[new_date] field.
Since the Date field can only accept and store Date/Time/Date&Time/(actual null) information, the text "NULL" is not valid.
Following is the equivalent for MySQL
INSERT INTO Destination_Table [new_date]
SELECT IF([startdate] == 'NULL', null, CONVERT(DATE,[startdate],103))
FROM Source_Table
(although I am unsure MySQL allows a conversion code as a param to CONVERT() )

SQL Server - 2008 Column Data Type

I have the below Case Statment and I'm inserting this into a new table. The column under the new table is Varchar but I need it as an INT. I have changed the data type using the Alter statment but I frequenctly delete and create the same table. Is there a way to have the new table create the data type of INT instead of varchar for the below syntax?
CASE WHEN F.END_DATE IS NOT NULL OR F.REASON IS NOT NULL THEN '0' ELSE '1' END Enrolled'
Get rid of the single quotes around 0 and 1:
CASE WHEN F.END_DATE IS NOT NULL OR F.REASON IS NOT NULL THEN 0 ELSE 1 END Enrolled'
Try Following:
CASE WHEN coalesce(F.END_DATE,F.REASON ,0)=0 THEN 0 ELSE 1 END Enrolled

How to write null value to datetime array in PostgreSQL 8.3?

I try to execute query:
INSERT INTO table_name
(
timedate_array_field
)
VALUES
(
'{NULL, NULL}'
)
But I get error "Could not convert string to DateTime: 'null'".
Just add single quotes for the value:
INSERT INTO table_name (timedate_array_field)
VALUES ('{NULL, NULL}')
-> SQLfiddle
BTW, the data type is not called "DateTime" or "timedate" in Postgres, but timestamp.