Showing posts with label therei. Show all posts
Showing posts with label therei. Show all posts

Monday, March 19, 2012

Combine Fields

Hi there!

I have a table with three columns: Id, Name and Departament. (ex: 23, "Mary", "Check-out")

I'd like to write a SQL Select clause so that the three columns are combined in just one column (ex: "23 - Mary - Check-out")

Is it possible? Many thanks for any kind of help.

Hi,

assuming ID is int

SELECT cast(ID as char(5)) + ' - ' + Name + ' - ' + Department AS TheNameYouLike

FROM YourTableName

|||

try this

SELECT CAST(ID AS varchar(5))+'-'+ltrim(Rtrim(Name))+'-'+ltrim(rtrim(Department) From yourtablename

Madhu

Wednesday, March 7, 2012

Column summing other columns

Hi there
I've got this really basic question:
ocasionally I have to do something like this
select sum(a) as SA, sum(b) as SB , sum(c) as SC, sum(a)+sum(b)+sum(c)
e.g. to sum up columns in another column.
it seems extremely unelegant, unfortunately using aliases in last
column ( SA+SB+SC) is impossible.
does it make sense performance-wise to leave such an operation to sql
server? will he optimize the query to prevent multiple calculation of
the same column sums, or should I rather sum the columns up on the
client side?
alos, isn't it possible to get rid of duplicating complicated
expressions by using some kind of alias if I want to use them in other
columns ?
thanks
Hp.The following example is one way to simplify the calculation:
CREATE TABLE foo
(
a INT,
b INT,
c INT
)
INSERT foo SELECT 1, 1, 1
INSERT foo SELECT 1, 2, 3
INSERT foo SELECT 1, 2, 3
INSERT foo SELECT 1, 3, 4
INSERT foo SELECT 1, 3, 5
SELECT SA, SB, SC, SA + SB + SC
FROM (SELECT SUM(a) AS SA, SUM(b) AS SB, SUM(c) AS SC
FROM foo) AS T1
HTH
- Peter Ward
WARDY IT Solutions
"H5N1" wrote:

> Hi there
> I've got this really basic question:
> ocasionally I have to do something like this
> select sum(a) as SA, sum(b) as SB , sum(c) as SC, sum(a)+sum(b)+sum(c)
> e.g. to sum up columns in another column.
> it seems extremely unelegant, unfortunately using aliases in last
> column ( SA+SB+SC) is impossible.
>
> does it make sense performance-wise to leave such an operation to sql
> server? will he optimize the query to prevent multiple calculation of
> the same column sums, or should I rather sum the columns up on the
> client side?
> alos, isn't it possible to get rid of duplicating complicated
> expressions by using some kind of alias if I want to use them in other
> columns ?
> thanks
> Hp.
>