Monday, February 13, 2012

Add a constant to a select result

Hi,
I have an statement select a, b, c from table with returns the
the contents of the columns a, b, c. I now would like to add a constant to
the result. What does a statement look like to return
content of a, b, c, 1
with 1 is my constant?
Thanks for any advice in advance
Pete
Pete Smith"Pete Smith" <msdn@.nospam.com> wrote in message
news:Oku5O9eUGHA.1672@.tk2msftngp13.phx.gbl...
> Hi,
> I have an statement select a, b, c from table with returns the
> the contents of the columns a, b, c. I now would like to add a constant to
> the result. What does a statement look like to return
> content of a, b, c, 1
> with 1 is my constant?
> Thanks for any advice in advance
> Pete
> --
> Pete Smith
>
SELECT a, b, c, 1 AS d
FROM tbl ;
d is a name for the column containing the constant. Although SQL doesn't
require it, it's always better to give every column a name.
Better still, be explicit about the datatype for your new column. Don't rely
on the server to guess what you intended because it may not always guess
right:
SELECT a, b, c, CAST(1 AS INTEGER) AS d
FROM tbl ;
Hope this helps.
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
--|||Hi David,

> SELECT a, b, c, CAST(1 AS INTEGER) AS d
> FROM tbl ;
Thanks, great help. Works excellent!
Thanks again!
Pete

No comments:

Post a Comment