Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update column in a view

Author  Topic 

pitt1
Starting Member

16 Posts

Posted - 2006-05-21 : 03:39:15
Hello all,
I'd like to create a view which shows all specific column values
(null values for example)
as calculated values from another column,
if for example i have the table:
---------------
| col1 | col2 |
|------|------|
| 123 | null |
| 126 | 9 |
---------------
i would like the view to be:

---------------
| col1 | col2 |
|------|------|
| 123 | 6 | ----> 6, since it's the sum of col1 digits 1+2+3
| 126 | 9 |
---------------

I don't really know how to do that, although i'm quite sure it's possible, any help will be appreciated,

Best Regards,
pitt

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-05-21 : 04:03:47
How about somthing like this ..


--First lets create a functin will return the sumup values for you ..
--Function Starts here..

Create Function SumCol(@Val int )
returns Int
As
BEgin
Declare @Qry Varchar(100),@Cnt Int,@SumVal Int
Select @Qry = Cast(@Val as Varchar),@Cnt =0,@SumVal = 0
While (@Cnt <=Len(@Qry))
Begin
Select @SumVal = @SumVal + Cast(SubString(@Qry,@Cnt,1) As Int),@Cnt = @Cnt+1

End

Return @SumVal

--Now lets create a view

Create View VwSumColValues
As
Select Col1,Dbo.SumCol(Col1) As Col2 From TblTmp

End





If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-21 : 04:56:00
Alternative method using division
create function sumdigits(@i int)
returns int
as
begin
declare @n int,
@c int
select @n = 1000000000

select @c = 0
while @n > 0
begin
select @c = @c + @i / @n
select @i = @i % @n,
select @n = @n / 10
end
return @c
end



KH

Go to Top of Page

pitt1
Starting Member

16 Posts

Posted - 2006-05-21 : 08:37:10
Great, thanks guys !

Molto Grazies !
Go to Top of Page
   

- Advertisement -