Разделить столбец на два столбца в зависимости от числа в int - PullRequest
0 голосов
/ 09 июля 2020

Это мой текущий код:

create table client (
  client_id int,
  birth_number int,
  district_id int
  Primary Key (district_id)
);

truncate client;

load data local infile 'C:\\Users\\james\\Desktop\\Wecloud_Data_Bootcamp\\bank_loan\\client.asc'
into table client
fields terminated by ';'
lines terminated by '\n'
ignore 1 lines
(client_id, @birth_number, district_id)
SET birth_number = REPLACE(@birth_number, '"','');

-- Check the table
select * from client;

это текущая таблица

enter image description here

What I need to do is split the birth_number column into two columns, which are birthday date and gender respectively. The instructions are below, probably integer manipulation is needed, how do I do so?

инструкция вопроса

реквизиты столбца

1 Ответ

0 голосов
/ 10 июля 2020

Попробуйте следующее:

SELECT 
     DATE_FORMAT(CONCAT(SUBSTR(birth_number, 1, 2), 
          IF(SUBSTR(birth_number, 3, 2) > 50, 
          SUBSTR(birth_number, 3, 2) - 50, SUBSTR(birth_number, 3, 2)), 
          SUBSTR(birth_number, 5, 2)),
         '%Y-%M-%d'
     ) AS `date`, 
     IF(SUBSTR(birth_number, 3, 2) > 50, 'Female', 'Male') AS Gender
FROM `client`

Измените формат даты в соответствии с вашими требованиями.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...