Solved

get year dash month number for sorting column

Posted on 2012-03-30
13
425 Views
Last Modified: 2012-03-30
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.
0
Comment
Question by:metropia
  • 7
  • 5
13 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 475 total points
ID: 37787965
try this...
ee.txt
0
 
LVL 18

Assisted Solution

by:lludden
lludden earned 25 total points
ID: 37787979
DECLARE @MyDate DATE = getdate()
SELECT CAST(DATEPART(yyyy,@MyDate) as varchar(4)) + ' - ' + LEFT('00' + CAST(datepart(mm,@MyDate) AS  varchar(2)),3)

Open in new window

0
 

Author Comment

by:metropia
ID: 37788023
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

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37788040
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
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 475 total points
ID: 37788044
note http:#a37787979

the LEFT should be RIGHT as in the first post.

 '00' + '12'  would become  '001'  with LEFT instead of the correct value '012' with RIGHT

I see in your use case you are using RIGHT, that's good. Just clarifying for all readers
0
 

Author Comment

by:metropia
ID: 37788069
Cool. Thank you!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 73

Expert Comment

by:sdstuber
ID: 37788198
glad to help,

if you need nothing else, don't forget to close the question.
0
 

Author Comment

by:metropia
ID: 37788316
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!
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37788418
don't cast the sum as money.  cast it as another numeric type
0
 

Author Comment

by:metropia
ID: 37788687
I did it like this:

REPLACE(CONVERT(varchar(20), (CAST(SUM(TOTAL_RUN) AS money)), 1), '.00', '')
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37788704
I guess that will work, but easier is to simply NOT use money,  money is why you have 2 decimal places
0
 

Author Comment

by:metropia
ID: 37788837
but when i use int, then the formatting is not applied.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37788849
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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now