June 3, 2013 at 9:51 pm
I have a table with the next structure:
CREATE TABLE students
(
ID_Student INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
Name nchar(30),
rate nchar(5)
)
and i have some data into the table like:
(Name, rate) ('Chipper Jones', '678*9')
(Name, rate) ('Mike Piazaa', '98***')
(Name, rate) ('Barry Bonds', '678**')
(Name, rate) ('Larry Walker', '*88*9')
Now I want separate the column (rate) in five columns more depending each number,
at the same time change the (*) for (10):
something like this:
game1 game2 game3 game4 game5
6 7 8 10 9
Thanks in advance.
June 4, 2013 at 1:15 am
Something like this..
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 4, 2013 at 7:30 pm
Kingston Dhasian (6/4/2013)
Something like this..
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
That's amazing ! it really Works.
thank you so much.
June 11, 2013 at 7:04 pm
Thanks for your help.
Now I have a doubt, I would like to add the numbers of each column and after add them i want divide them to get the percent, for example:
game1 game2 game3 game4 game5 percent
8 + 9 + 7 + 8 + 10 / 8.4
Thanks in advance.
June 11, 2013 at 7:08 pm
Pelon (6/4/2013)
Kingston Dhasian (6/4/2013)
Something like this..
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
That's amazing ! it really Works.
thank you so much.
Thanks for your help.
Now I have a doubt, I would like to add the numbers of each column and after add them i want divide them to get the percent, for example:
game1 game2 game3 game4 game5 percent
8 + 9 + 7 + 8 + 10 / 8.4
Thanks in advance.
June 12, 2013 at 2:55 am
You can use a CTE or a Derived table like this
; WITH cte_Students AS
(
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
)
SELECT*, ( game1 + game2 + game3 + game4 + game5 ) / 5 AS [percent]
FROMcte_Students
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 12, 2013 at 8:21 pm
Kingston Dhasian (6/12/2013)
You can use a CTE or a Derived table like this
; WITH cte_Students AS
(
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
)
SELECT*, ( game1 + game2 + game3 + game4 + game5 ) / 5 AS [percent]
FROMcte_Students
Thanks for you prompt response. Let me tell you that the query has showed a diferent result.
for example:
id name game1 game2 game3 game4 game5 percent
3Larry Walker 10998721997
the result should be 8.6
or may be I am doing something wrong.
I appreciate your attention.
June 12, 2013 at 9:28 pm
Kingston Dhasian (6/4/2013)
Something like this..
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN '10' ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
๐
--Jeff Moden
Change is inevitable... Change for the better is not.
June 13, 2013 at 2:10 am
It happened because the data types for columns game1, game2,...game5 were taken as VARCHAR
The below code will avoid the issue
; WITH cte_Students AS
(
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN 10 /*The previous version had apostrophes here which made the data type of the column as VARCHAR*/ ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
)
SELECT*, ( game1 + game2 + game3 + game4 + game5 ) / 5.0 /*Made 5 as 5.0 to avoid integer conversion*/ AS [percent]
FROMcte_Students
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 13, 2013 at 3:06 am
-- sample data
;WITH students (ID_Student, Name, rate) AS (
SELECT 1, CAST('Chipper Jones' AS nchar(30)), CAST('678*9' AS nchar(5)) UNION ALL
SELECT 2, 'Mike Piazaa', '98***' UNION ALL
SELECT 3, 'Barry Bonds', '678**' UNION ALL
SELECT 4, 'Larry Walker', '*88*9'
)
-- solution
SELECT
s.ID_Student, s.Name, s.rate,
x.game1, x.game2, x.game3, x.game4, x.game5,
(x.game1 + x.game2 + x.game3 + x.game4 + x.game5) / 5.0 AS [percent]
FROM students s
CROSS APPLY (
SELECT
Game1 = CAST(REPLACE(SUBSTRING(rate, 1, 1),'*','10') AS INT),
Game2 = CAST(REPLACE(SUBSTRING(rate, 2, 1),'*','10') AS INT),
Game3 = CAST(REPLACE(SUBSTRING(rate, 3, 1),'*','10') AS INT),
Game4 = CAST(REPLACE(SUBSTRING(rate, 4, 1),'*','10') AS INT),
Game5 = CAST(REPLACE(SUBSTRING(rate, 5, 1),'*','10') AS INT)
) x
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 13, 2013 at 6:19 pm
Kingston Dhasian (6/13/2013)
It happened because the data types for columns game1, game2,...game5 were taken as VARCHARThe below code will avoid the issue
; WITH cte_Students AS
(
SELECT*,
CASE WHEN SUBSTRING(rate, 1, 1) = '*' THEN 10 /*The previous version had apostrophes here which made the data type of the column as VARCHAR*/ ELSE SUBSTRING(rate, 1, 1) END AS game1,
CASE WHEN SUBSTRING(rate, 2, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 2, 1) END AS game2,
CASE WHEN SUBSTRING(rate, 3, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 3, 1) END AS game3,
CASE WHEN SUBSTRING(rate, 4, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 4, 1) END AS game4,
CASE WHEN SUBSTRING(rate, 5, 1) = '*' THEN 10 ELSE SUBSTRING(rate, 5, 1) END AS game5
FROMstudents
)
SELECT*, ( game1 + game2 + game3 + game4 + game5 ) / 5.0 /*Made 5 as 5.0 to avoid integer conversion*/ AS [percent]
FROMcte_Students
I am very grateful to you for your kind attention to my questions.
Let me tell you that the query works. But when returns the result it show me five numbers more.
For example the query returns:
Name game1 game2 game3 game4 game5 percent
Larry Walker 10 9 9 87 8.600000
I would like the results were like: 8.6, 9.0, 10, 8.8
thanks in advance.
June 13, 2013 at 6:30 pm
ChrisM@Work (6/13/2013)
-- sample data
;WITH students (ID_Student, Name, rate) AS (
SELECT 1, CAST('Chipper Jones' AS nchar(30)), CAST('678*9' AS nchar(5)) UNION ALL
SELECT 2, 'Mike Piazaa', '98***' UNION ALL
SELECT 3, 'Barry Bonds', '678**' UNION ALL
SELECT 4, 'Larry Walker', '*88*9'
)
-- solution
SELECT
s.ID_Student, s.Name, s.rate,
x.game1, x.game2, x.game3, x.game4, x.game5,
(x.game1 + x.game2 + x.game3 + x.game4 + x.game5) / 5.0 AS [percent]
FROM students s
CROSS APPLY (
SELECT
Game1 = CAST(REPLACE(SUBSTRING(rate, 1, 1),'*','10') AS INT),
Game2 = CAST(REPLACE(SUBSTRING(rate, 2, 1),'*','10') AS INT),
Game3 = CAST(REPLACE(SUBSTRING(rate, 3, 1),'*','10') AS INT),
Game4 = CAST(REPLACE(SUBSTRING(rate, 4, 1),'*','10') AS INT),
Game5 = CAST(REPLACE(SUBSTRING(rate, 5, 1),'*','10') AS INT)
) x
Thanks for your kind reply .
But the result show me something like 8.600000
I would like it returns 8.6
Thanks in advance.
June 14, 2013 at 3:44 am
Change the final SELECT statement to something like this
SELECT*, CAST( ( game1 + game2 + game3 + game4 + game5 ) / 5.0 AS NUMERIC(10,1) ) AS [percent]
FROMcte_Students
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 17, 2013 at 7:50 pm
Kingston Dhasian (6/14/2013)
Change the final SELECT statement to something like this
SELECT*, CAST( ( game1 + game2 + game3 + game4 + game5 ) / 5.0 AS NUMERIC(10,1) ) AS [percent]
FROMcte_Students
Thanks for your kind reply
I am very grateful.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply