Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Concat while selecting column

Author  Topic 

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-03-08 : 03:49:56
Hi,

I need to prefix a string while selecting a column

Select ('username' + Col) as uname from MyTable.

Table
abc
123

Output
usernameabc
username123

I tried
Select (uname = 'username' + Col) FROM MyTable

How should my query be.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-08 : 03:55:20
what is first row ? How do you define 1st row ? There isn't any first or last or nth row in database. The physical ordering of record has no meaning at all.



KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-08 : 04:00:55
SELECT 'UserName' + Col as uName
FROM MyTable


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-03-08 : 04:01:16
Sorry I modified my post.
I got a workaround for my issue.

quote:
Originally posted by khtan

what is first row ? How do you define 1st row ? There isn't any first or last or nth row in database. The physical ordering of record has no meaning at all.



KH



Go to Top of Page

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-03-08 : 04:01:16
Sorry I modified my post.
I got a workaround for my issue.

quote:
Originally posted by khtan

what is first row ? How do you define 1st row ? There isn't any first or last or nth row in database. The physical ordering of record has no meaning at all.



KH



Go to Top of Page

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-03-08 : 04:04:32
I need to cast my column.
The reply from Peso worked great.

Thanks a lot

quote:
Originally posted by Babli

Sorry I modified my post.
I got a workaround for my issue.

quote:
Originally posted by khtan

what is first row ? How do you define 1st row ? There isn't any first or last or nth row in database. The physical ordering of record has no meaning at all.



KH





Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-08 : 04:07:31
No need to cast if both 'abc' and '123' are valid data.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-08 : 04:08:18
But if you tricked us by posting invalid sample data, and you are only using numbers, this is the way to go

SELECT 'UserName' + CAST(Col AS VARCHAR) as uName
FROM MyTable


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -