Search code examples
sqlsubquerysql-insert

Inserting values based on another column SQL


I've added the column DVDAtTime and I'm and trying to insert values using a subquery. Seems rather straight forward but I keep getting an error that I can't insert null into (I believe) an unrelated field in the table. Ultimately, DVDAtTime should be the number shown in MembershipType

My code is as follows:

Insert Into Membership(DVDAtTime)
Select LEFT(MembershipType,1)
FROM Membership

enter image description here


Solution

  • I suspect you want to update each existing row, not insert new rows:

    update membership
        set DVDAtTime = left(MembershipType, 1)