简体   繁体   中英

Split a column into two columns based on number in the int

This is my code currently:

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;

this is the current table

在此处输入图像描述

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?

instruction of the question

details of the column

Please assist me.i tried replicating this query using Microsoft SQL. i tried this below query but got "Argument data type nvarchar is invalid for argument 1 of format function." as error message. see my code below:

enter image description here

Try this:

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`

Change the format of the date as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM