SQL Server is too happy to add day and month

I start with this query.

select 
  count(datepart(day, SomeTime)) as NumberOf, 
  datepart(day, SomeTime) as DaySansMonth
    from PMStationTightenings
    group by datepart(day, SomeTime)

He does what is supposedly, but not conveniently presented. Therefore, I will redo it to include a month.

select 
  count(datepart(day, SomeTime)) as NumberOf, 
  datepart(month, SomeTime) + ' ' + datepart(day, SomeTime) as DayAndMonth
    from PMStationTightenings
    group by datepart(month, SomeTime) + ' ' + datepart(day, SomeTime)

The computer tries to be smart and adds numbers anyway, so instead of, for example, 8 5 I get 13. Less perfection. What for? (I understand that he interprets two numbers as integers, of course, but why? There is clearly a space between ...)

In any case, I go ahead and make the trip, but put in things that he cannot add. Now I think to myself: "HA! Got ya!". You believe - a stupid computer barks at me, throwing these unpleasant red things, as if I were mistaken. Unbelievable! What a nerve! :)

select 
  count(datepart(day, SomeTime)) as NumberOf, 
  datepart(month, SomeTime) + '|' + datepart(day, SomeTime) as DayAndMonth
    from PMStationTightenings
    group by datepart(month, SomeTime) + '|' + datepart(day, SomeTime)

How can I make a computer using b! "# ยค and get him to give me combos per month / day from the db?

+4
2

, , WHY.

SQL Server:

, , . , .

INT ( DATEPART) AND (N) VARCHAR, INT , (N) VARCHAR), SQL Server INT.

, , . / .

: ,

+7

varchar:

CAST(datepart(month, SomeTime) AS VARCHAR(2)) 
+ ' ' 
+ CAST(datepart(day, SomeTime) AS VARCHAR(2))
+7

All Articles