sql server - SQL extracting information from columns names -
i have sql server table column names such int115
, int430
, on corresponding data. want multiply data in int115 column 115, data in int430 column 430 , on. not sure how proceed.
edit: need parse column names , rid of left 3 characters come multiplier number.
edit2: working grandfathered table can't make changes how database looks like.
edit after question updated
this not require hard coding @ all: cursor through temp table parsed through column names multiplication values, select table , give multiplied values. using 'sampletable' table name. assumes each of columns begin int , have number after.
select column_name, cast(substring(column_name, 4, 10)as int) multiplier_number #values information_schema.columns table_name = 'sampletable' declare @ssql nvarchar(max) declare @columnname nvarchar(50) declare thing cursor ( select column_name #values ) open thing fetch next thing @columnname while @@fetch_status = 0 begin set @ssql = 'select ' + @columnname + '* (select multiplier_number #values column_name = '''+ @columnname+''') '+@columnname+' sampletable' exec sp_executesql @ssql fetch next thing @columnname end close thing deallocate thing
Comments
Post a Comment