sql loader - check for specific field value - sql

I have a field called "FLOATVALUE" in csv/text file. I receive this file from third party, this has below possible values
NULL
{}
Any number
When I insert this value as below
.... [OTHER FIELD CHECKS]
isValid "nvl(:isValid, '')",
FLOATVALUE FLOAT EXTERNAL "nvl(:FLOATVALUE, NULL)"
.... [OTHER FIELD CHECKS]
Its checking whethre ":FLOATVALUES" is null or not. If yes it's inserting NULL into the oracle table, if not it's failing to insert for the values ("{}").
The field I'm loading into oracle table is 'FLOATVALUE FLOAT'. I would like to have something like below in my control file -
if :FLOATVALUE is null insert NULL
elseif :FLOATVALUE is '{}' insert NULL
else inert original value
How can specify a condition like above in control file?

You can use the decode function:
decode(:FLOATVALUE, '{}', NULL, :FLOATVALUE)
That says: if FLOATVALUE is '{}', insert NULL, otherwise insert FLOATVALUE

You can use either decode or case when statement. See the example below
Decode :
decode(Float_value,{},NULL,Float_value);
Case when :
CASE [ FLOAT_VALUE ]
WHEN NULL THEN NULL
WHEN {} THEN NULL
ELSE [FLOAT_VALUE]
END;

Related

Empty string being stored as null and need to differentiate between null and empty string in Orade [duplicate]

I am using Oracle DB. At the database level, when you set a column value to either NULL or '' (empty string), the fetched value is NULL in both cases. Is it possible to store '' (empty string) as a non NULL value in the database?
I execute this
UPDATE contacts SET last_name = '' WHERE id = '1001';
commit;
SELECT last_name, ID FROM contacts WHERE id ='1001';
LAST_NAME ID
------------ ------
null 1001
Is it possible to store the last_name as a non-NULL empty string ('')?
The only way to do this in oracle is with some kind of auxiliary flag field, that when set is supposed to represent the fact that the value should be an empty string.
As far as i know Oracle does not distinguish between '' and NULL, see here.
Oracle has a well know behavior that it silently converts "" to NULL on INSERT and UPDATE statements.
You have to deal with this in your code to prevent this behavior by converting NULL to "" when you read the columns back in and just do not use null in your program to begin with.
A long time since I used Oracle, but I believe we used to use a single space ' ' to represent an empty string, then trim it after reading.
If you use a VARCHAR2 data type then NULL and '' are identical and you cannot distinguish between them; so, as mentioned in other answers, you would either need to:
Have an additional column that contains a flag that distinguishes between non-NULL and NULL values so that if then flag states it is non-NULL and it contains a NULL then you know it is an empty string; or
Use an alternate representation, such as a single space character, for an empty string. This would then mean that you cannot store a string with that alternate representation; however, if trailing white-space was syntactically invalid for the strings you are storing then using a single space character to represent an empty string would be fine.
If you are using a CLOB data type then you CAN store an empty string using the EMPTY_CLOB() function:
CREATE TABLE table_name (value CLOB);
INSERT INTO table_name (value) VALUES (NULL);
INSERT INTO table_name (value) VALUES (EMPTY_CLOB());
INSERT INTO table_name (value) VALUES ('A');
Then:
SELECT value, LENGTH(value) FROM table_name;
Outputs:
VALUE
LENGTH(VALUE)
null
null
0
A
1
db<>fiddle here

replace 'null' strings with NULL in sql

I wanted to check my column. If there was a 'null' string, I wanted to replace it with a NULL value. This works but is there a better way to do it? Such that I don't have to repeat the same thing twice JSON_DATA :"ContactPerson"::STRING
SELECT
IFF(JSON_DATA :"ContactPerson"::STRING = 'null',NULL, JSON_DATA :"ContactPerson"::STRING) AS "ContactPerson",
FROM TEST_TABLE
I want to use REPLACE or REGEX_REPLACE instead.
Using IS_NULL_VALUE could be a bit shorter:
SELECT
IFF(IS_NULL_VALUE(JSON_DATA:"ContactPerson"), NULL,
JSON_DATA :"ContactPerson"::STRING)
FROM TEST_TABLE;
or NULLIF:
Returns NULL if expr1 is equal to expr2, otherwise returns expr1.
SELECT NULIF(JSON_DATA :"ContactPerson"::STRING, 'null')
FROM TEST_TABLE;
Regarding comments:
Still, how would regex_replace be used? REGEXP_REPLACE( , [ , , , , ] )what would the subject be here?
REGEXP_REPLACE(JSON_DATA :"Business_Type"::STRING, 'null', NULL) AS "BS2",but this would give me NULL if "null" doesn't exist in the original value
CREATE FUNCTION:
CALLED ON NULL INPUT
Specifies the behavior of the UDF when called with null inputs. In contrast to system-defined functions, which always return null when any input is null, UDFs can handle null inputs, returning non-null values even when an input is null
and REPLACE function has this behaviour described explicitly"
If any of the arguments is a NULL, the result is also a NULL.

How do I store a boolean value that takes in yes or no without quotes?

For my assignment I need help storing a boolean value that can hold yes or no. The column name is Required and the column attributes I received are Boolean value (1/0 or T/F) , Default value: NULL
I listed the sample data to be used bellow. The yes/no has to be stored without quotes somehow. Thank you
INSERT INTO CIS_Courses
VALUES ("CIS 105", "Computer Applications and Information Technology", null, Yes);
boolean data type is not available in sql server
you can use BIT datatype to represent boolean data.
A BIT field's value is either 1,0 or null
It also use the strings 'true' and 'false' in place of 1 or 0, like so-
declare #a bit = 'false'
print #a --output 0
declare #b bit = 'true'
print #b -- output 1
I always use tinyint(1) as boolean type value. Your table column would look like:
ColumnName tinyint(1) not null default 0
So when you don't insert a value, it will automatically be 0, or you can insert a 0 or 1.
I hope that this will help you

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() )

Is it possible to store '' (empty string) as a non NULL value in the database?

I am using Oracle DB. At the database level, when you set a column value to either NULL or '' (empty string), the fetched value is NULL in both cases. Is it possible to store '' (empty string) as a non NULL value in the database?
I execute this
UPDATE contacts SET last_name = '' WHERE id = '1001';
commit;
SELECT last_name, ID FROM contacts WHERE id ='1001';
LAST_NAME ID
------------ ------
null 1001
Is it possible to store the last_name as a non-NULL empty string ('')?
The only way to do this in oracle is with some kind of auxiliary flag field, that when set is supposed to represent the fact that the value should be an empty string.
As far as i know Oracle does not distinguish between '' and NULL, see here.
Oracle has a well know behavior that it silently converts "" to NULL on INSERT and UPDATE statements.
You have to deal with this in your code to prevent this behavior by converting NULL to "" when you read the columns back in and just do not use null in your program to begin with.
A long time since I used Oracle, but I believe we used to use a single space ' ' to represent an empty string, then trim it after reading.
If you use a VARCHAR2 data type then NULL and '' are identical and you cannot distinguish between them; so, as mentioned in other answers, you would either need to:
Have an additional column that contains a flag that distinguishes between non-NULL and NULL values so that if then flag states it is non-NULL and it contains a NULL then you know it is an empty string; or
Use an alternate representation, such as a single space character, for an empty string. This would then mean that you cannot store a string with that alternate representation; however, if trailing white-space was syntactically invalid for the strings you are storing then using a single space character to represent an empty string would be fine.
If you are using a CLOB data type then you CAN store an empty string using the EMPTY_CLOB() function:
CREATE TABLE table_name (value CLOB);
INSERT INTO table_name (value) VALUES (NULL);
INSERT INTO table_name (value) VALUES (EMPTY_CLOB());
INSERT INTO table_name (value) VALUES ('A');
Then:
SELECT value, LENGTH(value) FROM table_name;
Outputs:
VALUE
LENGTH(VALUE)
null
null
0
A
1
db<>fiddle here