Reputation: 1079
I am trying to return a column by concatenating two DATEPART
functions.
SELECT DATEPART ( ww , [Date] ) + '-' + DATEPART ( yy , [Date] ) as Date, [Email], count(url) as Pages_Visited
From [Log]
WHERE UserId IS NOT NULL and UserId != 0
GROUP BY DATEPART ( ww , [Date] ) + '-' + DATEPART ( yy , [Date] ), [Email]
The result I derive is the addition of the results two functions. What I require is the string concatenation.
For example if the two DATEPARTS
return 12
and 15
respectively I need the result to look like 12-15
but for now I get 27
Upvotes: 0
Views: 3552
Reputation: 5202
Cast them to varchars explicitly.
What is happening is that '-' is being implicitly converted to -0, and then added with the other two numbers!
Upvotes: 2
Reputation:
Convert to nvarchar first:
SELECT CONVERT(nvarchar(3), DATEPART(HOUR, GETDATE()))
+ N'-' + CONVERT(nvarchar(3), DATEPART(MINUTE, GETDATE()))
Or if you are using varchar
then simply remove N
leading string.
Upvotes: 2