month to Alphabet Format
Dear Friends,
How do we convert the month in integer format to a month in Alphabets
For eg: - 2 to February
3 to March
4 to April
etc
The Query I would like to convert is as follows
*************************************************************
select month(tdate) from hist group by month(tdate) order by month(tdate)
*************************************************************
I tried to convert it using Datename as follows
************************************************
SELECT datename(m,convert(smalldatetime,month(tdate))) from hist group by month(tdate) order by month(tdate)
**************************************************************
but the results came like this
January 1
January 2
January 3
January 4
January 5
January 6
January 7
January 8
regards
shown
# 1 Re: month to Alphabet Format
Hi,
You can get around this with a Case statement.
Select
Case
When tdate = 1 Then 'January'
When tdate = 2 Then 'February'
When tdate = 3 Then 'March'
When tdate = 4 Then 'April'
When tdate = 5 Then 'May'
When tdate = 6 Then 'June'
When tdate = 7 Then 'July'
When tdate = 8 Then 'August'
When tdate = 9 Then 'September'
When tdate = 10 The 'October'
When tdate = 11 Then 'November'
When tdate = 12 Then 'December'
End as 'Month'
Group By tdate
Order By tdate
Hope this helps.
# 2 Re: month to Alphabet Format
dear WarlockSoul,
Is that the only way u can do it. Won't the "Datename()" function does any help for this conversion.
regards
shown
:thumb:
# 3 Re: month to Alphabet Format
If it is SQLServer see SQL Server Books Online help:
DATENAME
Returns a character string representing the specified datepart of the specified date.
Syntax
DATENAME ( datepart , date )
Krzemo at 2007-11-9 13:41:21 >

# 4 Re: month to Alphabet Format
dear Krzemo,
I tried it using Datename function in sql server.But it doesn't work when i specify the following way
******************************************************************
SELECT datename(m,convert(smalldatetime,month(tdate))),month(tdate) from hist group by month(tdate) order by month(tdate)
******************************************************************
but the results came like this
January 1
January 2
January 3
January 4
January 5
January 6
January 7
January 8
regards
shown :thumb:
# 5 Re: month to Alphabet Format
Month function is not an agregate function (why U use GROUP BY)!
I've prepared exaple 4 U using Northwind database:
SELECT
DATENAME(month,OrderDate) MonthName
,COUNT(*) counter
,MONTH (OrderDate) MonthNumber
FROM Orders
GROUP BY MONTH(OrderDate),DATENAME(month,OrderDate)
ORDER BY MONTH(OrderDate)
Results:
MonthName counter MonthNumber
---------- ---- ----
January 88 1
February 83 2
March 103 3
April 105 4
May 46 5
June 30 6
July 55 7
August 58 8
September 60 9
October 64 10
November 59 11
December 79 12
(12 row(s) affected)
U can also use DISTINCT keyword ...
Does it helps You?
Krzemo at 2007-11-9 13:43:13 >

# 6 Re: month to Alphabet Format
convert(smalldatetime,month(tdate))) ??
Since months ranged from 1 to 12
possible converted dates ranged from 1900-01-02 00:00:00 to 1900-01-13 00:00:00
and it is allways January
:-)
Krzemo at 2007-11-9 13:44:21 >

# 7 Re: month to Alphabet Format
Dear Krzemo,
The examples u gave were very helpful.
But I wanted to ask one more doubt to u.
The query I gave to u was not the original query I am using. It was just an example query.
The result what I required to convert to datename is the month in Integer format that i receive from the subqueries.
So what i did in the following example I gave u
******************************************************************
SELECT datename(m,convert(smalldatetime,month(tdate))),month(tdate) from hist group by month(tdate) order by month(tdate)
******************************************************************
is to convert the month in integer format to date format , Since the datename function accepts only date as the 2nd argument.I really didn't check the output from
Convert(smalldatetime,month(tdate))
let me give u the output i got from the original query
month MonthBalance NetDeposits NetLoss FinalBal
---- ---- ---- ---- ----
1 8760891.44 1431843.88 -626416.62 9566318.70
2 9558698.16 1799597.71 -1547121.94 9811173.93
3 9809904.20 1290730.39 -1496494.68 9604139.91
4 9623744.12 2630806.60 -747713.02 11506837.70
5 11492194.80 1668921.67 -25368.63 13135747.84
So what i have to do is to convert this given month in integer to Alphabets.
Am I talking too much :)
are u getting :mad: at me.
regards
shown
:thumb:
# 8 Re: month to Alphabet Format
DATENAME(month,DATEADD(month,Month(tdate)-1,'20000101'))
Krzemo at 2007-11-9 13:46:16 >

# 9 Re: month to Alphabet Format
dear friend,
It works perfectly alright and thanks for that.
But will i be able to know what is this '20000101' in the query and the concept behind it.
***************************************************
DATENAME(month,DATEADD(month,Month(tdate)-1,'20000101'))
*****************************************************
and what does Convert(smalldatetime,2) does in my previous query to get that answer.
regards
shown_sunny
:thumb:
# 10 Re: month to Alphabet Format
1) '20000101' or '20000101 00:00:00.000' is a date (2000-01-01)
It is a shortcut to CONVERT(datetime,'20000101',112)
2) Dates are stored internaly in SQL Server as fixed point numeric values - number of days since 1990-01-01 00:00:00
So Convert(smalldatetime,2)
is logicaly the same as Convert(smalldatetime,Convert(datetime,2))
and is logicaly the same as Convert(smalldatetime,Convert(datetime,'19900103 00:00:00.000',112))
Does it helps You?
Krzemo.
Krzemo at 2007-11-9 13:48:23 >

# 11 Re: month to Alphabet Format
dear Krzemo,
That was excellent.It was very much clear and precise.
Thanks for the answers.
Hopes to get the same help from u in the future too.
regards
shown :wave: