# SQL prob please help!

**URL:** <https://forum.codewithmosh.com/t/sql-prob-please-help/20496>\
**Category:** Uncategorized\
**Created:** [May 27, 2023, 9:19am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496 "2023-05-27T09:19:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [May 27, 2023, 9:19am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/1 "2023-05-27T09:19:03Z")

</div>

update invoices  
set payment\_total = invoice\_total \* 0.5, payment\_date = due\_date  
where invoice\_id = 3

I wrote the code exactly as taught.

but why it’s giving me a warning ?

16:33:20 update invoices set payment\_total = invoice\_total \* 0.5, payment\_date = due\_date where invoice\_id = 3 0 row(s) affected, 1 warning(s): 1265 Data truncated for column ‘payment\_total’ at row 1 Rows matched: 1 Changed: 0 Warnings: 1 0.000 sec

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [May 28, 2023, 11:02am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/2 "2023-05-28T11:02:19Z")

</div>

The error message says it: At least one of the computed values had more digits than the column can hold so the value was truncated to fit.

Say we have an invoice\_total of 100.01. Multiplying that by 0.5 gives us 50.005. The three decimal places do not fit into a column with two decimal places.

In real world scenarios you should pay attention to such warnings since truncation often isn’t what you want. Instead you might want to apply some rounding.

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [May 30, 2023, 10:13am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/3 "2023-05-30T10:13:35Z")

</div>

ah I see.

So what should I do in this case.

Thank you so much ^ ^

W

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [May 30, 2023, 4:25pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/4 "2023-05-30T16:25:41Z")

</div>

In _this_ case - nothing. It’s just an example.

In real world scenarios you would check the business rules on how to handle a situation like this and add an explicit truncation or rounding.

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [June 3, 2023, 10:07am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/5 "2023-06-03T10:07:48Z")

</div>

But I canNOT execute the code?

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [June 13, 2023, 4:59pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/6 "2023-06-13T16:59:54Z")

</div>

Can you clarify your question?

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 3, 2023, 3:40pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/7 "2023-07-03T15:40:06Z")

</div>

WHAT??! I said I cannot execute the bloody code but It WORKED in Mosh’s SQL platform!!!

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [July 3, 2023, 3:59pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/8 "2023-07-03T15:59:51Z")

</div>

You’re not making any sense at all.

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 3, 2023, 4:17pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/9 "2023-07-03T16:17:02Z")

</div>

What you DONT UNDERSTAND WHAT I CANT EXECUTE THE CODE

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 3, 2023, 4:17pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/10 "2023-07-03T16:17:58Z")

</div>

What you DONT UNDERSTAND WHAT I CANT EXECUTE THE CODE AND IT THROWS AN ERROR MEANS??

IF YOU DON’T KNOW HOW TO RESOLVE SAY IT.DONT GIVE ME CRAP. ,

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [July 3, 2023, 10:04pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/11 "2023-07-03T22:04:28Z")

</div>

If your MySQL server is in [strict mode](https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict) you can use `UPDATE IGNORE` or need to explicitly apply truncation or rounding where necessary. To achieve the same result as Mosh did, use `ROUND(invoice_total * 0.5, 2)`.

---

<div class="post-metadata">

**Author:** ![gulshan212](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gulshan212/32/6217_2.png) [@gulshan212](https://forum.codewithmosh.com/u/gulshan212)\
**Post date:** [July 4, 2023, 3:29pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/12 "2023-07-04T15:29:18Z")

</div>

Well, I search about it and I can say that to fix this issue, you can check the information types and section sizes of the ‘payment\_total’ and ‘invoice\_total’ segments in the ‘invoice’ table. Ensure that the ‘payment\_total’ column can accommodate the calculated value without truncation. You might need to adjust the data type or increase the column size to store the calculated payment total accurately.  
I hope it will give you some idea.  
Thanks

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [July 4, 2023, 7:31pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/13 "2023-07-04T19:31:38Z")

</div>

Of course such a warning can give you hints that your data types are not correct. But that’s really depending on the problem domain.

In this case the columns contain amounts of money and have two decimal digits. They are designed for amounts with dollars and cents. It wouldn’t make much sense to extent a payment total column to more than two decimal digits.

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 21, 2023, 7:08pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/14 "2023-07-21T19:08:11Z")

</div>

hi,

this is the error message

 ![Screenshot 2023-07-21 200705](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/d/d55459473dcacc927a07492681cfca8c0a972fff.png)

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 21, 2023, 7:08pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/15 "2023-07-21T19:08:57Z")

</div>

THIS does NOT work unfort

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 21, 2023, 7:10pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/16 "2023-07-21T19:10:37Z")

</div>

why the hell the simple code not work

---

<div class="post-metadata">

**Author:** ![lucidlear](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/lucidlear/32/5477_2.png) [@lucidlear](https://forum.codewithmosh.com/u/lucidlear)\
**Post date:** [July 21, 2023, 10:59pm UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/17 "2023-07-21T22:59:38Z")

</div>

> [@molly](#):
>
> update invoices  
> set payment\_total = invoice\_total \* 0.5, payment\_date = due\_date  
> where invoice\_id = 3

Hi Molly,

Its because invoice\_total \* 0.5 is $73.995 which is 3 decimal places but we can only fit data with two decimal places in that column so the warning was displayed to say the value returned was shorted to fit 2 decimal places there. This is a warning, it does not affect if the operation actually ran or not, this is different to an error in which case the operation will not run at all. Because this is a tutorial you can usually ignore such warnings as data integrity is not so important here rather the underlying concepts.

If you would like to resolve the warning just add another digit after the decimal places, so put a 3 instead of two for the data type. ie: instead of DECIMAL(9,2) write DECIMAL(9,3) and apply to the payment\_total column and do it to the invoice\_total also for consistency. screenshots below. However, this may cause some data inconsistencies as your setup will be different to mosh’s so as Sam mentioned the best way would be to use the ROUND function, then you wont get the warning, so your sql query would look like:

```auto
update invoices
set payment_total = ROUND(invoice_total * 0.5, 2), payment_date = due_date
where invoice_id = 3

```

 ![sql1](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/e/e153270a245dd2a22bfeda2dacd6a15eb452cc12.png)  
 ![sql2](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/b/be928353ec4457bc3b2a3994bb1d5217a3133cf4.png)

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 24, 2023, 11:04am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/18 "2023-07-24T11:04:29Z")

</div>

Hi thanks

When I clicked the tool button after invoices, I get this: (not the table you shown there):

 ![Screenshot 2023-07-24 120138](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/f/f2d78afad366d8b1418e91b115f4e73a6d710aa6.png)

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [July 24, 2023, 11:07am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/19 "2023-07-24T11:07:03Z")

</div>

I think sb did some bad things!! fking hell

---

<div class="post-metadata">

**Author:** ![molly](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@molly](https://forum.codewithmosh.com/u/molly)\
**Post date:** [August 2, 2023, 11:01am UTC](https://forum.codewithmosh.com/t/sql-prob-please-help/20496/20 "2023-08-02T11:01:42Z")

</div>

no one did bad things. other code work.

apolog
