HeidiSQL import csv empty fields - mysql

I want to import a csv file to my MySQL database with HeidiSQL.
But some of my fields are empty.
What could I do to let HeidiSQL know these empty values have to be seen as NULL-values?
Sample of csv-file (last 2 fields not yet known):
NULL;Students Corner;437452182;;
Create commands:
CREATE TABLE `db`.`customers` (
`company_id` INT NOT NULL AUTO_INCREMENT ,
`company_name` VARCHAR(40) NULL ,
`company_number` INT NULL ,
`company_vat` INT NULL ,
`company_zip` INT NULL,
PRIMARY KEY (`company_id`) );
I get these error:
Incorrect integer value: '' for column 'company_id' at row 1 */
Incorrect integer value: '' for column 'company_vat' at row 1 */
Incorrect integer value: '' for column 'company_zip' at row 1 */
etc

If solved it by writing \N in each empty field instead of writing NULL !

You can import CSV files into MySQL using a LOAD DATA INFILE query.
In your case, you would write something like this:
LOAD DATA INFILE filename.txt
INTO TABLE customers
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(#id, #name, #number, #vat, #zip)
SET
company_id = (CASE WHEN #id='' THEN NULL ELSE #id END),
company_name = (CASE WHEN #name='' THEN NULL ELSE #name END),
company_number = (CASE WHEN #number='' THEN NULL ELSE #number END),
company_vat = (CASE WHEN #vat='' THEN NULL ELSE #vat END),
company_zip = (CASE WHEN #zip='' THEN NULL ELSE #zip END)
(you may need to adjust this, depending on your end of line markers, etc, but this should be pretty close to what you need)

Related

oracle replace '0' with 'N/A'

I would like to replace data in column RANDOM that equal to '0', NULL, 'NONE', 'NA' WITH 'N/A'.
I used nested REPLACE as below:
SELECT IDNO,
REPLACE (
REPLACE (
REPLACE(
REPLACE(RANDOM, 'NONE|NA', 'N/A'),
'0','N/A'),
'-',''),
NULL, 'N/A') AS NUMBER
FROM TABLE1
It does replace data '0' with 'N/A'. However, it does replace other data that contain 0 in that too. Example: 04110 change to N/A411N/A
I want to replace the one that exactly have value '0' only.
SELECT IDNO,
REPLACE (
REPLACE (
REPLACE(
REPLACE(RANDOM, 'NONE|NA', 'N/A'),
'/0','N/A'),
'-',''),
NULL, 'N/A') AS NUMBER
FROM TABLE1
When I do this (add \ in front of 0) it does not change any of the 0 to 'N/A'
How do I replace data with the exact value '0' with 'N/A' then?
Thank you in advance. :)
... RANDOM that equal to '0', NULL, 'NONE', 'NA' WITH 'N/A'
"Equal" means that there's nothing else in that column. In that case, use CASE:
select idno,
case when random in ('0', 'NONE', 'NA') or radnom is NULL then 'N/A'
else random
end as result
from table1
Hint: Check out IF function of your sql server. Try this:
SELECT
IDNO,
IF(Random IS NULL or Random = '0' or Random='NONE' or Random = 'NA', 'N/A', Random)
FROM
TABLE1

Concatenating fields in select clause JOOQ

I'm working in a query using JOOQ and I'm trying to output a column as a
concatenation (space separated) of other fields extracted in the same query.
Getting into detail, with the next code I try to create a select statement with a column called fullAdress by grouping all the address lines contained in the address table. So, for each field, if it's not null or empty it will be concatenated to the result (actually no space is being added).
#Override
protected List<Field<?>> selectCustomFields() {
List<Field<?>> customSelect = new ArrayList<Field<?>>();
// Fields to use in the concatenation
Field<?> field1 = field("addr.AddressLine1"), field2 = field("addr.AddressLine2"),field3 = field("addr.AddressLine3"),
field4 = field("addr.AddressLine4"), field5 = field("addr.PostalCode"), field6 = field("addr.City"),
field7 = field("addr.State"), field8 = field("addr.County"), field9 = field("addr.Country");
// Create non null/empty conditions
Condition condLine1 = field1.isNotNull().and(field1.length().ne(0));
Condition condLine2 = field2.isNotNull().and(field2.length().ne(0));
Condition condLine3 = field3.isNotNull().and(field3.length().ne(0));
Condition condLine4 = field4.isNotNull().and(field4.length().ne(0));
Condition condLine5 = field5.isNotNull().and(field5.length().ne(0));
Condition condLine6 = field6.isNotNull().and(field6.length().ne(0));
Condition condLine7 = field7.isNotNull().and(field7.length().ne(0));
Condition condLine8 = field8.isNotNull().and(field8.length().ne(0));
Condition condLine9 = field9.isNotNull().and(field9.length().ne(0));
// Concat address lines when meets condition
customSelect.add(concat(DSL.when(condLine1, field1),
DSL.when(condLine2, field2),
DSL.when(condLine3, field3),
DSL.when(condLine4, field4),
DSL.when(condLine5, field5),
DSL.when(condLine6, field6),
DSL.when(condLine7, field7),
DSL.when(condLine8, field8),
DSL.when(condLine9, field9))
.as("fullAddress"));
return customSelect;
}
JOOQ will generate the next from the previous select statement, which is giving a null value and not concatenating the fields correctly.
select
concat(
cast(case when (
addr.AddressLine1 is not null
and char_length(cast(addr.AddressLine1 as char)) <> 0
) then addr.AddressLine1 end as char),
cast(case when (
addr.AddressLine2 is not null
and char_length(cast(addr.AddressLine2 as char)) <> 0
) then addr.AddressLine2 end as char),
cast(case when (
addr.AddressLine3 is not null
and char_length(cast(addr.AddressLine3 as char)) <> 0
) then addr.AddressLine3 end as char),
cast(case when (
addr.AddressLine4 is not null
and char_length(cast(addr.AddressLine4 as char)) <> 0
) then addr.AddressLine4 end as char),
cast(case when (
addr.PostalCode is not null
and char_length(cast(addr.PostalCode as char)) <> 0
) then addr.PostalCode end as char),
cast(case when (
addr.City is not null
and char_length(cast(addr.City as char)) <> 0
) then addr.City end as char),
cast(case when (
addr.State is not null
and char_length(cast(addr.State as char)) <> 0
) then addr.State end as char),
cast(case when (
addr.County is not null
and char_length(cast(addr.County as char)) <> 0
) then addr.County end as char),
cast(case when (
addr.Country is not null
and char_length(cast(addr.Country as char)) <> 0
) then addr.Country end as char)) as `fullAddress`
from Address as `addr`
....
My questions are,
how should I create my select statement correctly?
how can I best add the space separator?
is there any better alternative to JOOQ ( when = case ) condition clause?
how should I create my select statement correctly?
You forgot the CASE .. ELSE part, or otherwise() in jOOQ:
// Concat address lines when meets condition
customSelect.add(concat(DSL.when(condLine1, field1).otherwise(""),
DSL.when(condLine2, field2).otherwise(""),
DSL.when(condLine3, field3).otherwise(""),
DSL.when(condLine4, field4).otherwise(""),
DSL.when(condLine5, field5).otherwise(""),
DSL.when(condLine6, field6).otherwise(""),
DSL.when(condLine7, field7).otherwise(""),
DSL.when(condLine8, field8).otherwise(""),
DSL.when(condLine9, field9).otherwise(""))
.as("fullAddress"));
how can I best add the space separator?
If you want an additional space separator between your address parts, you could write:
// Concat address lines when meets condition
customSelect.add(concat(DSL.when(condLine1, field1.concat(" ")).otherwise(""),
DSL.when(condLine2, field2.concat(" ")).otherwise(""),
DSL.when(condLine3, field3.concat(" ")).otherwise(""),
DSL.when(condLine4, field4.concat(" ")).otherwise(""),
DSL.when(condLine5, field5.concat(" ")).otherwise(""),
DSL.when(condLine6, field6.concat(" ")).otherwise(""),
DSL.when(condLine7, field7.concat(" ")).otherwise(""),
DSL.when(condLine8, field8.concat(" ")).otherwise(""),
DSL.when(condLine9, field9.concat(" ")).otherwise("")).trim()
.as("fullAddress"));
is there any better alternative to JOOQ ( when = case ) condition clause?
I think the approach is sound. Of course, you probably shouldn't repeat all that logic all the time, but create a loop of the sort:
List<Field<String>> list = new ArrayList<>();
for (int i = 0; i < fields.size(); i++) {
list.add(DSL.when(conditions.get(i), (Field) fields.get(i)).otherwise(""));
}
customSelect.add(concat(list.toArray(new Field[0])).trim().as("fullAddress"));

Stuffing multiple rows to see if columns are populated?

I'm trying to figure out a way in MySql to stuff multiple records for the same user into a single row to see which columns are populated. For example:
Username Height Weight Age
Bob123 6ft
Bob123 100lbs
Bob123 120lbs 25yrs
Let's say I have these three records in a table. I want to be able to combine them into a single row that just indicates if each column was populated in any of the records. My hopeful result record would look something like this for each user:
Username Height Weight Age
Bob123 True True True
Is there a way to do this in MySQL or do I need to look at doing this programmatically?
A generic sql method would be like this:
select username
, case when maxheight is not null then 'true' else 'false' end hasheight
, etc
from
(select username
, max(height) maxheight
, etc
from yourtables
where whatever
group by username) temp
CREATE TABLE person (Username VARCHAR(55), Height VARCHAR(55), Weight VARCHAR(55) , Age VARCHAR(55)
);
INSERT INTO person VALUES
('Bob123' , '6ft' , NULL , NULL),
('Bob123' , NULL , '100lbs' , NULL),
('Bob123' , NULL , '120lbs' , '25yrs');
SELECT username,
CASE height WHEN NULL THEN ' ' ELSE 'True' END,
CASE weight WHEN NULL THEN ' ' ELSE 'True' END,
CASE age WHEN NULL THEN ' ' ELSE 'True' END
FROM person
GROUP BY username;
DEMO

issue while trying to insert one table into another one

I have a mysql table of 1.5 million record in a temproary table that I want to insert it into the main table with an inner join.
My code works but it stops at 103,613 records. IT does not keep going. Why would it stops? why it is not going all the way to the end?
This is my current code
INSERT INTO phone_calls(next_attempt, created_on, modified_on, status, call_subject,
account_number, call_code, last_attempt_on, total_attempts, account_id
,team_id
,campaign_id
,call_code_id
,result_code
,result_code_id
,time_zone_id
,trigger_on
,first_attempt_on
,first_attempt_by
,last_attempt_by
,modified_by
,client_id
,last_call_id
,call_notes
,owner_id
,industry_id
)
SELECT
CASE WHEN next_attempt IS NULL OR next_attempt = '' THEN STR_TO_DATE(replace(t.next_attempt,'/',','),'%m,%d,%Y %T') END as next_attempt,
CASE WHEN t.created_on IS NULL OR t.created_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.created_on,'/',','),'%m,%d,%Y %T') END as created_on,
CASE WHEN t.modified_on IS NULL OR t.modified_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.modified_on,'/',','),'%m,%d,%Y %T') END AS modified_on,
CONVERT( CASE WHEN t.status IS NULL OR t.status = '' THEN 0 ELSE t.status END, UNSIGNED INTEGER) AS status,
LEFT(IFNULL(t.call_subject, ''), 100),
t.account_number,
CONVERT( CASE WHEN t.callcode IS NULL OR t.callcode = '' THEN 0 ELSE t.callcode END , UNSIGNED INTEGER) AS callcode, STR_TO_DATE(replace(t.last_attempt_on,'/',','),'%m,%d,%Y %T') as last_attempt_on,
CONVERT( CASE WHEN t.New_Attempts IS NULL OR t.New_Attempts = '' THEN 0 ELSE t.New_Attempts END , UNSIGNED INTEGER) AS New_Attempts,
a.account_id
,0
,0
,0
,0
,0
,0
, '0000-00-00 00:00:00'
, '0000-00-00 00:00:00'
,1
,1
,1
,1
,0
,'IMPORTED FROM CRM'
,1
,1
FROM tmp_table_for_rdi_cms AS t
INNER JOIN accounts AS a ON a.account_number = t.account_number LIMIT 9999999999;
these 2 fields are indexed so it runs fast
a.account_number
t.account_number
Now, I know i am inserting no value for some fields that has a type if unsigned integer but that is okay as i will be updating this at later time.
How can i execute this INSERT INTO query without losing any record?
The "doesnt have a default value" problem is that the source table has nulls for columns on the new table that are defined S NOT NULL and which are not defined with a default value.
You have three choices to fix this:
Define the target columns as null able (leave out the NOT NULL)
Define the target columns with a default value, ie NOT NULL DEFAULT 'some value'
Provide a value if the column is null, ie SELECT ifnull(source_column, 'some value)
The Truncated incorrect INTEGER value problems and you are selecting from a char value and putting it in an int column, but some vyes are blank, so you need to handle that:
select if(source_column = '', 0, source_column)
The Data truncated for column problem is there because the source value is larger/longer than the target column can accommodate. To fix, define your target column to be larger (bigint instead of int) or longer (eg text or varchar(80) instead of varchar(20))

Select works, but can't update rows with a string of all spaces -- why?

This query
SELECT count(data_id)
FROM cdiac_data_AL
WHERE (data_id >= 1 and data_id <= 30437)
AND (TMIN_flags = '')
returns 844 records, but update query affects 0 records:
UPDATE cdiac_data_AL set TMIN_flags=' '
WHERE (data_id >= 1 and data_id <= 30437)
AND ( TMIN_flags = '' )
What am I missing?
TMIN_flags is:
char(3) ascii_general_ci, allow null, default NULL
It's because the CHAR data type does not save trailing spaces.
You should change TMIN_flags data type to binary