Link to home
Start Free TrialLog in
Avatar of metropia
metropiaFlag for United States of America

asked on

get year dash month number for sorting column

I have a date field: TRANSACTION_DATE

I would like to return the year and month number separated by a dash to create a new column that would look like (not the month name):

2012 - 001  (January)
2012 - 002 (February)
2012 - 003 (March)
..
...
....
2012 - 011 (November)


Would someone shoe me how I can do this?

Thank you much.
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of metropia

ASKER

Thank you, that worked.

What if I do not want to include that column on my display results, but I want to use it to sort the results?

Here is my entire query as right now:

SELECT 
MILL_TYPE, 
(CONVERT(VARCHAR(4),TRANSACTION_MONTH_AND_YEAR,100) + '-' + CONVERT(VARCHAR(4),YEAR(TRANSACTION_MONTH_AND_YEAR))), 
REPLACE(CONVERT(varchar(20), 
(CAST(SUM(TOTAL_RUN) AS money)), 1), '.00', ''), 
cast(year(TRANSACTION_MONTH_AND_YEAR) as varchar) +'-' + right('000' + cast(month(TRANSACTION_MONTH_AND_YEAR) as varchar),3)
FROM RG_MIL_PRODUCTION_REPORT_VW
WHERE TRANSACTION_DATE >= {ts '2011-01-01 00:00:00'} AND TRANSACTION_DATE <{ts '2012-01-01 00:00:00'}
GROUP BY 
(CONVERT(VARCHAR(4),TRANSACTION_MONTH_AND_YEAR,100) + '-' +  CONVERT(VARCHAR(4),YEAR(TRANSACTION_MONTH_AND_YEAR))), 
MILL_TYPE, 
cast(year(TRANSACTION_MONTH_AND_YEAR) as varchar) +'-' + right('000' + cast(month(TRANSACTION_MONTH_AND_YEAR) as varchar),3)

Open in new window

Avatar of Sean Stuber
Sean Stuber

simply include the column in the order by but don't select it.

since you are doing a group by, the value that you order by must be part of your grouping
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cool. Thank you!
glad to help,

if you need nothing else, don't forget to close the question.
i am about to close this question. and i know this will be unrelated to the original post but you may be willing to throw a hand quickly.

i am trying to remove the 2 decimal places from the amount returned by this query:

CONVERT(varchar, CAST(SUM(TOTAL_RUN) AS money), 1)

I get: 178,373.00

I only need: 178,373

If you can help with this that would be sweet if not, no big deal.

Thank you much!
don't cast the sum as money.  cast it as another numeric type
I did it like this:

REPLACE(CONVERT(varchar(20), (CAST(SUM(TOTAL_RUN) AS money)), 1), '.00', '')
I guess that will work, but easier is to simply NOT use money,  money is why you have 2 decimal places
but when i use int, then the formatting is not applied.
we're spending more time on the tangent than the original question.

if you want to pursue that line more, how about opening a new question.