0

I'm having trouble inserting data from one column to another within the same table. I have no problem inserting individual rows but that is not what I want. I have a column called "PercentFull", I am multiplying this value by .01 to get the decimal equivalent, which is what I want to be inserted into a column I've called "NewPercentFull". I have tried the following:

insert into Table1 (NewPercentFull)
select PercentFull * .01
from Table 1

There is already data that populates the PercentFull column. However, after this query is executed, I get NULL for every column in my table, including the NewPercentFull. Anyone has an idea of how to achieve this? Thanks

1 Answer 1

5

Inserting data from one column, into another, from the same record, would involve using an UPDATE statement:

UPDATE Table1
SET NewPercentFull=PercentFull * .01

With your query, you should've seen double the amount of records in Table1 after execution of that query.

You've mentioned that the values were NULL even for NewPercentFull. If this is the case, then I think my query may return NULL values too. This will occur when PercentFull is also NULL.

To avoid this, only UPDATE records which don't have a NULL value for PercentFull:

UPDATE Table1
SET NewPercentFull=PercentFull * .01
WHERE PercentFull IS NOT NULL
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your quick response. Yes, you're right, after I executed that query, I did have double the amount of entries. The query you have suggested seems to be on the right track, the problem now is that they are populated with "0" instead of the actual value. I find this odd since I know the "percentfull * .01" function works when i use a select as statement.
@Rick it sounds like a rounding issue. What data types are your columns?
I have declared both, PercentFull and NewPercentFull as int. The NewPercentFull is a new column I created so this one is empty (I dont know if that will make a difference with the update statment)
@Rick int is for integer, which are only whole numbers (hence the rounding). Try changing it to decimal(18,3)
I see the logic here. I have changed both data types to decimal, but now my NewPercentFull is being populated by 1's and 0's. I'm confused as to why this isn't working as it is when I use the select as statement.
|

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.