Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DateTime conversion

Author  Topic 

itpriyesh88
Starting Member

13 Posts

Posted - 2012-05-22 : 06:05:38
I want to return an output from a subquery as datetime.

select SUM(Total) from Orders where OrderDateTime in ( select distinct(datename(month,OrderDateTime)) from Orders where OrderDateTime between '2011-05-01' and '2012-05-01')

Error message is:

Conversion failed when converting date and/or time from character string.

Plz Help!!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-22 : 06:15:32
What you want is it just simply


select SUM(Total)
from Orders
where OrderDateTime >= '2011-05-01'
and OrderDateTime < '2012-05-02'



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

itpriyesh88
Starting Member

13 Posts

Posted - 2012-05-22 : 06:19:55
Yes. But I want to display the output as month wise like,

Month Total
April 11000.00
May 12678.00
June 97344.00


etc...
Go to Top of Page

itpriyesh88
Starting Member

13 Posts

Posted - 2012-05-22 : 06:23:36
Output should be month wise total sum of the orders...

Is this possible?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-22 : 06:25:37
[code]
select datename(month, OrderDateTime), SUM(Total)
from Orders
where OrderDateTime >= '2011-05-01'
and OrderDateTime < '2012-05-01'
group by datename(month, OrderDateTime)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

itpriyesh88
Starting Member

13 Posts

Posted - 2012-05-22 : 07:26:53
Thanks..It works.. :)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2012-05-22 : 13:16:11
See this for more on "grouping by month," since grouping by a month's name is usually not ideal (doesn't sort right, you'll have issues with multiple years, etc); there are better options:

http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-30 : 06:18:27
Also make sure to use unambigious date formats
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-ii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -