Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenation

Status
Not open for further replies.

NotSQL

Technical User
May 17, 2005
205
0
0
GB
Can anyone help, im trying to concatenate data together. However im not getting the correct results.

What I have is a productiondate which I want to take the year and month and concatenate together.

eg

200602
200512
200401

I've tried:

select cast(year(productiondate as varchar)) & cast(month(productiondate as varchar))
from table1

What am i doing wrong?
 
try using a + not a &

Code:
select (cast(year(productiondate as varchar)) + cast(month(productiondate as varchar))) AS yearmonthcat
from table1

Sean. [peace]
 
How 'bout [!]convert(varchar(6), datecolumn, 112)[/!]?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Code:
select cast(year(productiondate as varchar)) & cast(month(productiondate as varchar))
from table1

First, forgot the close-parenthesis after "productiondate" in the YEAR and MONTH function calls and second follow sean4e's advice about the "+" instead of "&" (which is used to do a logical order, not concatenation).

select cast(year(productiondate) as varchar(4)) + cast(month(productiondate) as varchar(2)) + cast(day(productiondate) as varchar(2))
from table1


If you choose vongrunt's solution, make the varchar size 8 {convert(varchar(8), productiondate, 112)}, otherwise you'll only get back year and month (e.g. convert(varchar(6), getdate(), 112) returns "200603" for today...).

The convert-112 will give you leading zeros for the month and day values, so if you don't want them, use the first code in blue, otherwise, the convert-112 is cleaner and more efficient.


-Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top