0

I am using LOAD DATA INTO command to read data from a txt file into my Database. In my input, ı need to take '-' characters as null (not the string null). For example;the input

stack - overflow

have to be

1st column = stack 2nd column = null 3rd column = overflow.

How can I do that??

1
  • What have you got so far? Can you get the data into your database at all, without worrying about the NULLs? Commented Jan 3, 2011 at 21:10

1 Answer 1

2

A simple approach would be to first use LOAD DATA without any conversions, then afterwards run some updates to correct the values that you want changing:

UPDATE yourtable
SET col1 = NULL
WHERE col1 = '-'

The LOAD DATA syntax also allows you to specify transformations to your data:

[SET col_name = expr,...]

The column list can contain either column names or user variables. With user variables, the SET clause enables you to perform transformations on their values before assigning the result to columns.

An example would be:

LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, @var1)
SET column2 = CASE WHEN @var1 = '-' THEN NULL ELSE @var1 END;
Sign up to request clarification or add additional context in comments.

2 Comments

May I do it while creating database?
Tried that with an if statement but I could not do it. I wrote IF name != '-' THEN SET name=@name END IF but says it is a syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.