473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is it possible to run a cast function on entire table possible?

120 100+
is it possible to perform a cast function on an entire table via sql? im basically trying to select all table contents multiply by 1.1 and then display the converted data on a webpage using sql, classic asp.

i can do a cast function on a single table column like this
Expand|Select|Wrap|Line Numbers
  1. Select columnname = Cast(columnname * 1.1 as int)from tablename WHERE ID ='1'
how would I apply this to an entire table? there's quite a few columns so I dont wish to do this the long way by specifying each column with a cast function over and over again.

I tried this but no joy
Expand|Select|Wrap|Line Numbers
  1. Select * = Cast(* * 1.1 as int)from tablename WHERE ID ='1'
thanks in advance
Omar.
Apr 7 '10 #1
20 3371
Delerna
1,134 Expert 1GB
Then you should have designed the table so that the fields are of the correct type so that casting is not needed.

There is no way to cast all fields with a single cast statement
Apr 8 '10 #2
omar999
120 100+
hi delerna

the table is fine as it contains uk prices. the reason I want to convert the whole table is to display converted euro prices on a different page.

trying to kill 2 birds with 1 stone hehe

for now iv used the cast function on every column in the table. the sql statement is pretty long but it works.

omar.
Apr 8 '10 #3
nbiswas
149 100+
In such a case I would rather go with a function then.

e.g.
Expand|Select|Wrap|Line Numbers
  1. select dbo.MyConvertFunc(col1), Dbo.MyConvertFunc(col2).. from tablename WHERE ID ='1' 
and the MyConvertFunc will have the following implementation

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION MyConvertFunc 
  2. (@InputVals int)
  3. RETURNS int
  4. AS
  5. BEGIN
  6. declare @convertedVal varchar(30)
  7. set @convertedVal =  Cast(@InputVals  * 1.1 as int)
  8. return  @convertedVal 
  9. end
Give a thought pls.

Hope this helps
Apr 9 '10 #4
code green
1,726 Expert 1GB
I do currency exchange rates in my system.

I don't understand why the OP is casting prices to INT.
And we surely do not wnat '1.1' hard coded anywhere, as the exhange rate has to be dynamic.

I would modify nbiswas' function to pass an extra parameter defining the currency converting from to provide future expansion
Expand|Select|Wrap|Line Numbers
  1. MyConvertFunc (@InputVals int @curr VARCHAR) 
  2. --where @curr could be 'USD', 'EUR', 'GBP' etc
Apr 9 '10 #5
omar999
120 100+
code green I agree with you the exchange rate has to be dynamic - I will tackle this at a later stage.

nbiswasB your solution seems ideal but I have never used a function within a select statement before so I'm still a little confused..

does the function have to be saved as a stored procedure on sql server or within a sql table?

thanks in advance
Omar.
Oct 27 '10 #6
ck9663
2,878 Expert 2GB
If you could post some sample data and how do you want it to look like, we might be able to give you a more efficient solution.

Good Luck!!!

~~ CK
Oct 27 '10 #7
omar999
120 100+
hey CK :)

i can perform a cast on an entire table the long winded way like this - not a good solution in my opinion as I'm performing the cast function manually on approximately every single column.
Expand|Select|Wrap|Line Numbers
  1. RMT2DAYGOLD = "SELECT Date_Band, Redleaf_Tour, Goldleaf_Tour, Double_Accomodation, Triple_Accomodation, Single_Accomodation, Child_Accomodation, " & _
  2. "Double_Redleaf_Price = Cast(Double_Redleaf_Price - (Double_Redleaf_Price * 0.10) as decimal (10,0)), " & _
  3. "Double_Goldleaf_Price = Cast(Double_Goldleaf_Price - (Double_Goldleaf_Price * 0.10) as decimal (10,0)), " & _
  4. "Triple_Redleaf_Price = Cast(Triple_Redleaf_Price - (Triple_Redleaf_Price * 0.10) as decimal (10,0)), " & _
  5. "Triple_Goldleaf_Price = Cast(Triple_Goldleaf_Price - (Triple_Goldleaf_Price * 0.10) as decimal (10,0)), " & _
  6. "Single_Redleaf_Price = Cast(Single_Redleaf_Price - (Single_Redleaf_Price * 0.10) as decimal (10,0)), " & _
  7. "Single_Goldleaf_Price = Cast(Single_Goldleaf_Price - (Single_Goldleaf_Price * 0.10) as decimal (10,0)), " & _
  8. "Child_Redleaf_Price = Cast(Child_Redleaf_Price - (Child_Redleaf_Price * 0.10) as decimal (10,0)), " & _
  9. "Child_Goldleaf_Price = Cast(Child_Goldleaf_Price - (Child_Goldleaf_Price * 0.10) as decimal (10,0)) " & _
  10. "FROM RMT_2DayTours WHERE ID IN (1,2,3)"
  11.  
ideally im looking for a cast all method
Expand|Select|Wrap|Line Numbers
  1. 'RMT2DAYGOLD = "CAST = formula here..(SELECT * FROM RMT_2DayTours WHERE ID IN (1,2,3)"
  2.  
nbiswas provided help but im still a bit confused if the cast all function is to be called from a stored procedure or?...

omar.
Oct 28 '10 #8
ck9663
2,878 Expert 2GB
I don't think I have seen a solution for what you are trying to achieve. You can, technically, cast an entire the table result set, just not the way you're thinking of...

Good Luck!!!

~~ CK
Oct 28 '10 #9
NeoPa
32,556 Expert Mod 16PB
CK asked for sample data, which you didn't provide in your response. I wouldn't expect too much more clarification without first responding to the request.
Oct 28 '10 #10
omar999
120 100+
sample data below
Expand|Select|Wrap|Line Numbers
  1. ID Date_Band      Redleaf_Tour  Goldleaf_Tour Double_Accomodation Triple_Accomodation Single_Accomodation Child_Accomodation Double_Redleaf_Price Double_Goldleaf_Price Triple_Redleaf_Price Triple_Goldleaf_Price Single_Redleaf_Price Single_Goldleaf_Price Child_Redleaf_Price Child_Goldleaf_Price
  2. 1  May<br />2011  Redleaf       Goldleaf      Double              Triple              Single              Child(2-11 years)  489                  989                   479                  979                   539                  1039                  419                 909
  3.  
please advise..
Nov 4 '10 #11
ck9663
2,878 Expert 2GB
From the looks of it, you want to cast those numeric values and because it's too many, you don't want to type all the column names.

Analysis of your query shows that you are also performing subtraction and multiplication operation, not just conversion.

Either straight-forward conversion or mathematical operation, you cannot cast your entire table that way. Since each of those field needs to be returned separately, you need to perform the conversion operation individually.

If you're doing this for the purpose of displaying it on the front-end, why not perform the operation on the front-end instead.

Good Luck!!!

~~ CK
Nov 4 '10 #12
omar999
120 100+
hey CK

thanks for the reply. that's correct I want to cast only the numeric values. I'm basically taking 10% off the price/value and am trying to learn a more lean sql query that what i have illustrated above.

could you please advise as to what you mean by performing the operation on the front end?

do you mean an asp function recordset?

thanks again
Omar.
Nov 4 '10 #13
NeoPa
32,556 Expert Mod 16PB
Omar999:
could you please advise as to what you mean by performing the operation on the front end?
This is based on the assumption that you are using a Client/Server approach, where the Server would be the SQL Server - or Back End. Whatever software you're using on the client would be the Front End.

CK is saying just what most experienced system designers would say, which is to handle that part of your problem in the client and not burden the server with such matters that are better handled by the client.
Nov 4 '10 #14
omar999
120 100+
hi Neopa

thanks for explaining - I understand more clearly now. I'm using classic asp on the front end.

I can only guess that I have to create some sort of classic asp function to utilize on the recordset? Am i right..?
Nov 5 '10 #15
NeoPa
32,556 Expert Mod 16PB
Omar999:
Am i right..?
Yes :-D
Nov 5 '10 #16
omar999
120 100+
ah cool. could you advise as to how this could be performed on recordset retrievel please..

would it be possible to use a function of some sort...

thanks
Nov 5 '10 #17
NeoPa
32,556 Expert Mod 16PB
Omar, I'm sure if you think about it you'll realise that my response to that is logical - This is a Classic Asp question and needs to be posted in that forum. Most members that hang around in the SQL Server forum are there because they know about SQL Server. For members with Classic Asp expertise you'd need to look in that forum.
Nov 5 '10 #18
omar999
120 100+
hi neopa

no worries I've posted in the classic asp forum. classic asp function to utilize on the recordset

thanks again
Omar.
Nov 8 '10 #19
nbiswas
149 100+
Hi Omar,
Sorry for being too late in replying.. however, it is possible using dynamic query..

Use this program
Expand|Select|Wrap|Line Numbers
  1. Declare @listCol VARCHAR(2000)
  2. Declare @tableName VARCHAR(100)
  3. Set @tableName  = 'Table_Name' -- specify your table name here
--Step 1: Get the column names of the table dynamically , cast it to integer and --multiply by 1.1
Expand|Select|Wrap|Line Numbers
  1. SELECT  @listCol = (SELECT [name] +  ' = CAST([' + [name] + '] as int) * 1.1,' FROM syscolumns
  2. WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [NAME] = @tableName) FOR XML PATH(''))
--Strip out the last ,
Expand|Select|Wrap|Line Numbers
  1. SELECT @listCol = Left(@listCol, LEN(@listCol)-1)
--Step2: Execute the query
Expand|Select|Wrap|Line Numbers
  1. exec( 'SELECT ' + @listCol + ' FROM ' + @tableName)
This program assumes that the column values of the table can be converted to integer.. else you have to put a check for the same.

e.g.
Expand|Select|Wrap|Line Numbers
  1. Declare @t table(ID int identity, Col1 varchar(2)
  2. insert into @t select '10'
will work as the value 10 can be converted to integer but if you have say 'xxx', it cannot be.

Hope this cleares your doubt...

Thanks
Jun 30 '11 #20
try this:---

select 'CAST(' + name + ' AS INT)' from sys.columns where object_id = object_id('table name')

customize acc to ur problem.
Jul 1 '11 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
5
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
7
by: VPaul | last post by:
I hope I am posting to the correct group for assistance. Please advise me if there is another group I should go to. I am new to this, so I apologize if I don't explain this very well. I do...
10
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return...
6
by: John-Arne Lillebų | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ?...
5
by: mike | last post by:
If I have a document like: <script> function mike_test() {alert('hi');} </script> <iframe src="blank.html" id="my_iframe1"> </iframe> and in blank.html I have:
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.