Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a table with a column Sold that has rows with the flowing values:

81, 10, 40, 71, 21, 31, 41, 30, 90, 61, 50, 11, 20, 91, 60

I'm trying to write a query to add a 2 where the number ends 0 and add a 3 where the number ends in 1.

What I have tried:

This is my sad attempt so far

Select SalesId, EmployeeId, CAST(Sold as varchar(20)) + '1'
From Sales
WHERE Sold LIKE '%1'

I don't know if I should use CASE or IF statements
Posted
Updated 24-May-17 20:17pm
Comments
MadMyche 24-May-17 23:46pm    
LEFT and RIGHT can be used on numeric fields, Right(Sold, 1) is a little cleaner looking IMHO

try

select SalesId, EmployeeId, case when right(cast(sold as varchar(20)),1) = '0' then sold + 2 else case when right(cast(sold as varchar(20)),1) = '1' then sold + 1 else sold end end from Sales
 
Share this answer
 
Comments
Maciej Los 25-May-17 2:08am    
5ed!
In addition to solution1 by RossMW[^] i'd suggest to do it in more efficient way by using modulo[^].

SQL
DECLARE @tmp TABLE(myNumber INT)

INSERT INTO @tmp (myNumber)
VALUES(81), (10), (40),(71), (21), 
	(31), (41), (30), (90), (61), 
	(50), (11), (20), (91), (60)

SELECT myNumber, CASE WHEN myNumber % 10 = 0 THEN myNumber + 2 ELSE myNumber + 1 END AS NewMyNumber
FROM @tmp 

Result:
myNumber	NewMyNumber
81			82
10			12
40			42
71			72
21			22
31			32
41			42
30			32
90			92
61			62
50			52
11			12
20			22
91			92
60			62
 
Share this answer
 
Comments
CPallini 25-May-17 3:21am    
5.
Maciej Los 25-May-17 4:29am    
Thank you, Carlo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900