简体   繁体   中英

Inserting values into a column by derived from another column in the same table?

I have two columns in a table:

  • FlightNumber (which has values such as AI-1234, AI-3242), and
  • FlightId (which is supposed to be 1234, 3242 based on the FlightNumber)

I am able to select the FlightNumber column with last 4 characters of each record.

 SELECT RIGHT(flightnumber, 4) FROM pnrdetails

But I am not sure how to insert this into the FlightId column. Suggestions?

I guess , you dont need insert. you should be thinking about update. and I assume FlightId datatype is int .

 update pnrdetails set FlightId = convert(int,RIGHT(flightnumber, 4));

you could update the table

update pnrdetails
set FlightId =RIGHT(flightnumber, 4)

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