Update statement to change all values in a column [closed] - sql-server

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to write a query that will update all values the values in the Values column.
This is what I've come up with:
Begin
UPDATE Order_Details
SET Value = (Value / 100) * 10
WHERE Cust_Ref = ALL
End
It doesn't work and comes up with
Incorrect syntax near keyword END
Does anyone know how to correct this?
Thanks guys! Much appreciated!!!

You don't need Where clause
UPDATE Order_Details SET Value = (Value / 100.0) * 10
You need to use Where clause only when you want to filter the records
Also Value / 100 will do a Integer division. The decimal values will not be present in result. So make either the numerator or denominator as decimal value
Example :
select 1/ 10 -- Result 0
select 1/ 10.0 -- Result 0.100000

Related

in SQL Server does LEFT() convert nvarchars to int? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I have a bunch of alphanumeric codes stored as Nvarchar. I want to take all the codes that begin with a 1 and append .00001 onto the end of them. I tried this code
,CASE
WHEN left(1,[CASE_CODE]) = '1'
THEN concat([CASE_CODE],'.00001')
ELSE [CASE_CODE]
END as [CASE_CODE]
But I get this error message
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the nvarchar value 'PVC00595' to data type int.
Is the LEFT function converting to int for some reason?
As per the documentation, left takes a character expression followed by an integer expression. Your error is coming from left(1,[CASE_CODE]) trying to convert CASE_CODE into an integer expression (since it is the second argument).
You want left([CASE_CODE], 1) (reversed arguments) instead.

Must declare scalar variable with update percentage [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am updating the articles by a stored procedure but I get an error. No idea how to solve my problem. I am totally new to database programming. Please help me.
This is my code:
create procedure p_rabatt
(#articleID int,
#precentage int)
as
update Artikel
set Preis = Preis * (1 + (#percentage / 100))
where ArtikelCode = #articleID
Thanks for your gently feedback, guys!
The #precentage parameter name is not the same as #percentage

Why operator AND returns error in tsql, that seems to be valid in general sql [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I try to apply WHERE AND WHERE operators, but somehow AND returns an error, single WHERE for both cases works fine.
I don't feel like I made a mistake in code as well, at least not in general SQL syntax.
SELECT *
FROM dbo.flat_file
WHERE [Date] > '1/1/2020'
AND
WHERE [Deposit] > 1000
I am new to T-SQL, so I may not be aware of basic principles.
tsql printscr
There can only be one WHERE clause in a query. You try to have two. Instead just AND the second condition to the first in the first (and only) WHERE clause.
SELECT *
FROM dbo.flat_file
WHERE [Date] > '1/1/2020'
AND [Deposit] > 1000;
Side note: varchar is likely the wrong datatype for date and deposit. date and integer my be a better fit.

Round up with ceiling in SQL server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have to increase by 3% and round up the prices I have in a table. My script:
UPDATE tbl_products
SET Prices = CEILING(prices * 1.03, 0)
After I run the script, SQL Server Management Studio returns an error, saying:
ceiling function requires one argument.
If I use ROUND instead of CEILING, the script works great. What I'm doing wrong?
Syntax is as shown below. Ceiling() function is used to find the smallest integer value.
CEILING( number )
Here is an example.
SELECT CEILING(32.65);
Result: 33
SELECT CEILING(32.1);
Result: 33
SELECT CEILING(32);
Result: 32
SELECT CEILING(-32.65);
Result: -32
SELECT CEILING(-32);
Result: -32

I have the following query whenever I run it, it gives me incorrect syntax near 'As' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
SELECT
CAST((Discount_Amount AS numeric(11,2))/1.14)*0.14 AS [Service Tax]
FROM APDEPADV
I think your parentheses are in the wrong place:
SELECT
(CAST(Discount_Amount AS numeric(11,2))/1.14)*0.14 AS [Service Tax]
FROM APDEPADV
You should be casting Discount_Amount first, then perform the other calculations:
Cast Discount_Amount as a numeric value
Next, divide the result by 1.14
Finally, multiply it all by 0.14
If you break your code down a little, you may understand what's going on:
CAST has the following syntax: CAST(a AS b)
Your code has: CAST((Discount_Amount AS numeric(11,2))/1.14)
Because you have the division inside the CAST function, then the whole expression (Discount_Amount AS numeric(11,2))/1.14 is treated as the value you want to cast (a in point 1 above).
This has a couple of consequences. First, CAST(a AS b) has now become CAST(a), which doesn't make much sense (what are you casting it to?)
Secondly, it means that the expression Discount_Amount AS numeric(11,2) is being processed separately to the CAST function. Something like Discount_Amount/1.14 is a valid expression, but (Discount_Amount AS numeric(11,2))/1.14 on its own is not, which is why you're receiving the error.
By rearranging the parentheses, you should get closer to what you intend:
SELECT
(
CAST(Discount_Amount AS numeric(11,2))
/1.14
)
* 0.14
AS [Service Tax]
FROM APDEPADV
Try this
SELECT
(CAST(Discount_Amount AS numeric(11,2))/1.14)*0.14 AS [Service Tax]
FROM APDEPADV
Order of parentheses is wrong. Understand the expression and then re-write the parentheses accordingly.
SELECT
((Cast(Discount_Amount AS numeric(11,2))/1.14)*0.14 AS [Service Tax]
FROM APDEPADV

Resources