December 31, 2007 at 1:58 am
Experts,
I have a table like,
Table Name: Emp
ENo Ename Age Level Stars
1 AAAA 42 8 Null
2 BBBB 38 6 Null
3 CCCC 48 10 Null
4 DDDD 32 5 Null
5 EEEE 25 3 Null
Now i want to update the Stars column like
ENo Ename Age Level Stars
1 AAAA 42 8 ********
2 BBBB 38 6 ******
3 CCCC 48 10 **********
4 DDDD 32 5 *****
5 EEEE 25 3 ***
Stars column value = Values in the Level Column.
for ex,If level is 8,then Stars column should contain 8 stars.
Thanks in advance.
karthik
December 31, 2007 at 2:29 am
Hi, you could use the REPLICATE function.
E.g.:
UPDATE [Emp]
SET [Stars] = REPLICATE('*', [Level])
Regards
December 31, 2007 at 2:38 am
Yes, you are correct.
But,how to perform this one without REPLICATE function ?
karthik
December 31, 2007 at 3:02 am
Well, you could use some sort of looping using cursors or CLR functions made using C# or so, but I do not see any advantage in not using REPLICATE.
December 31, 2007 at 3:22 am
karthikeyan (12/31/2007)
Yes, you are correct.But,how to perform this one without REPLICATE function ?
Why the restriction against replicate?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 31, 2007 at 3:27 am
My new RM asked me to provide the solution without REPLICATE function.
Initially i used REPLICATE function.But he did not want to use REPLICATE.
karthik
December 31, 2007 at 3:31 am
Did he give you a reason why? Sound a bit strange.
Here's one that doesn't actually use replicate, but does the same thing as replicate would.
DECLARE @Length INT
SET @Length = 4
SELECT REPLACE(SPACE(@Length),' ','*')
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 31, 2007 at 3:34 am
Will this logic work out to update * in the star column ?
karthik
December 31, 2007 at 3:39 am
Did you try it?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 31, 2007 at 3:42 am
It should, shouldn't it, if you just modify it a little to:
UPDATE Emp
SET [Level] = REPLACE(SPACE([Star])),' ','*')
December 31, 2007 at 3:43 am
Not yet. I will get back to you shortly.
karthik
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply