Home All Groups Group Topic Archive Search About

Using Select and Convert in procedure..

Author
23 Mar 2006 11:34 PM
Habibullah
Hi.

I can't figure this out.  I like to select all the record from Table_tbl and
converting one field at the same time.

Here are two separate statements:

1: Select * From Table_tbl
2: Select convert (decimal (5,2),strcost) from Table_tbl

Goal is to get this both things done in one shot, means converting the
strCost field and then selecting all record including new converted value.

Thanks.

Habib.

Author
24 Mar 2006 12:07 AM
David Portas
Show quote
"Habibullah" <Habibul***@discussions.microsoft.com> wrote in message
news:10AB383E-09A7-41C2-A6A8-2903BF14DD5F@microsoft.com...
> Hi.
>
> I can't figure this out.  I like to select all the record from Table_tbl
> and
> converting one field at the same time.
>
> Here are two separate statements:
>
> 1: Select * From Table_tbl
> 2: Select convert (decimal (5,2),strcost) from Table_tbl
>
> Goal is to get this both things done in one shot, means converting the
> strCost field and then selecting all record including new converted value.
>
> Thanks.
>
> Habib.

For example:

SELECT CONVERT(DECIMAL(5,2),strcost) AS col1,
col2, col3, col4
FROM Table_tbl ;

You can use * in the SELECT list but this is definitely not recommended for
production code. SELECT * hurts performance and makes your code harder to
debug and maintain. Avoid SELECT * in any persistent code.

SELECT CONVERT(DECIMAL(5,2),strcost) AS col1, *
FROM Table_tbl ;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
Author
24 Mar 2006 12:11 AM
P. Ward
The following example below should return the desired results:

CREATE TABLE dbo.Table_tbl
    (
    col1 INT,
    col2 INT,
    strcost DECIMAL(5, 4),
    col3 INT
    )

INSERT dbo.Table_tbl SELECT 1, 1, 1.1234, 1
INSERT dbo.Table_tbl SELECT 2, 2, 2.4567, 2
INSERT dbo.Table_tbl SELECT 3, 3, 3.7890, 3


SELECT     col1, col2, CONVERT (DECIMAL (5,2),strcost) AS strcost, col3
FROM      dbo.Table_tbl


col1        col2        strcost col3       
----------- ----------- ------- -----------
1           1           1.12    1
2           2           2.46    2
3           3           3.79    3

(3 row(s) affected)


HTH

- Peter Ward
WARDY IT Solutions

Show quote
"Habibullah" wrote:

> Hi.
>
> I can't figure this out.  I like to select all the record from Table_tbl and
> converting one field at the same time.
>
> Here are two separate statements:
>
> 1: Select * From Table_tbl
> 2: Select convert (decimal (5,2),strcost) from Table_tbl
>
> Goal is to get this both things done in one shot, means converting the
> strCost field and then selecting all record including new converted value.
>
> Thanks.
>
> Habib.

AddThis Social Bookmark Button