SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select date form datetime field
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

JohnDW
Starting Member

Belgium
45 Posts

Posted - 09/17/2012 :  16:35:04  Show Profile  Reply with Quote
Hello,

I Want to make a query that is selecting the date part from a datetime field in sql server 2008 db, and order the query on
the date field the query selected.
I have the following query that selects the datetime field,
(underlined). I want to chanche that by selecting the date part from that field.


SELECT     TOP (100) PERCENT dbo.Orders.Klantnummer, (dbo.Orders.Orderdatum) AS Date, dbo.Orderdetail.Verkoopprijs, dbo.Orderdetail.Tal, 
                      SUM(dbo.Orderdetail.Verkoopprijs * dbo.Orderdetail.Tal) AS Expr1, dbo.Orders.Store, dbo.Orders.IsBetaald
FROM         dbo.Orders INNER JOIN
                      dbo.Orderdetail ON dbo.Orders.Orderid = dbo.Orderdetail.OrderId
GROUP BY dbo.Orders.Klantnummer, (dbo.Orders.Orderdatum) , dbo.Orderdetail.Verkoopprijs, dbo.Orderdetail.Tal, dbo.Orders.Store, dbo.Orders.IsBetaald
HAVING      (dbo.Orderdetail.Tal <> 0) AND (dbo.Orders.Store IS NULL) AND (dbo.Orders.IsBetaald <> 3)
ORDER BY (dbo.Orders.Orderdatum) DESC


Can someone help?

Txs,

John

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 09/17/2012 :  16:38:20  Show Profile  Reply with Quote
make it DATEADD(dd,DATEDIFF(dd,0,dbo.Orders.Orderdatum),0)

see

http://visakhm.blogspot.com/2010/01/some-quick-tips-for-date-formating.html

http://visakhm.blogspot.com/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JohnDW
Starting Member

Belgium
45 Posts

Posted - 09/17/2012 :  23:46:29  Show Profile  Reply with Quote
Ihave used the syntaxis in my query ; this query filters the date from the datetime field (yyyy-mm-dd 00:00:00.000)

ViewInkPermndLing



SELECT     TOP (100) PERCENT dbo.Orders.Klantnummer, DATEADD(dd, DATEDIFF(dd, 0, dbo.Orders.Orderdatum), 0) AS Datum, dbo.Orderdetail.Verkoopprijs, dbo.Orderdetail.Tal, 
                      SUM(dbo.Orderdetail.Verkoopprijs * dbo.Orderdetail.Tal) AS Expr1, dbo.Orders.Store, dbo.Orders.IsBetaald
FROM         dbo.Orders INNER JOIN
                      dbo.Orderdetail ON dbo.Orders.Orderid = dbo.Orderdetail.OrderId
GROUP BY dbo.Orders.Klantnummer, DATEADD(dd, DATEDIFF(dd, 0, dbo.Orders.Orderdatum), 0), dbo.Orderdetail.Verkoopprijs, dbo.Orderdetail.Tal, dbo.Orders.Store, 
                      dbo.Orders.IsBetaald
HAVING      (dbo.Orderdetail.Tal <> 0) AND (dbo.Orders.Store IS NULL) AND (dbo.Orders.IsBetaald <> 3)
ORDER BY Datum DESC



From that query I take the sum:

ViewInkPermndLing1

SELECT     Datum, SUM(Expr1) AS Som, Klantnummer
FROM         dbo.ViewInkPermndLin
GROUP BY Datum, Klantnummer
HAVING      (Klantnummer <> 1869) AND (Klantnummer <> 1847) AND (SUM(Expr1) > 0)

And I want to have the sum of the previous query order by month and year:

ViewTotalMonth

Transform Sum(Som) As SomofSom
SELECT     format(Datum, mmm) AS Month, Month(Datum) AS Expr 1
FROM         dbo.ViewInkPermndLing1
GROUP BY Format(Datum, mmm), Month(Datum)
Order By Month(Datum), Year (Datum)


How do I do this?


Txs,

John

Edited by - JohnDW on 09/17/2012 23:50:45
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 09/18/2012 :  01:47:21  Show Profile  Reply with Quote

SELECT     Month(Datum), Year (Datum),Sum(Som) As SomofSom
FROM         dbo.ViewInkPermndLing1
GROUP BY Month(Datum), Year (Datum)
Order By Month(Datum), Year (Datum)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JohnDW
Starting Member

Belgium
45 Posts

Posted - 09/18/2012 :  02:50:04  Show Profile  Reply with Quote
Txs,

Is there also a way to put the results in a way like in access (a crosstabel query?)

month Year2010 Year2011 Year2012
01 2314 3214 3421
02 3212 5462 4352

and so on...

John
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 09/18/2012 :  11:11:51  Show Profile  Reply with Quote
you can
see


SELECT     Month(Datum), 
SUM(CASE WHEN Year(Datum) = 2010 THEN Som END) As [Year2010],
SUM(CASE WHEN Year(Datum) = 2011 THEN Som END) As [Year2011],
SUM(CASE WHEN Year(Datum) = 2012 THEN Som END) As [Year2012]
FROM         dbo.ViewInkPermndLing1
GROUP BY Month(Datum)
Order By Month(Datum)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JohnDW
Starting Member

Belgium
45 Posts

Posted - 09/19/2012 :  15:58:01  Show Profile  Reply with Quote
Txs,

it works great.
But I have a few questions:
1. Does it mean I have to update the view each year?
2. What with null values in the query (or view)?
Because I use the view to fill a datagridview where I calculate
the sum. And with Null values this gives an error.

Is there a way to fill in an integer (0 value as integer) so the calcultion in vb.net can take place?

Txs,
John
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 09/20/2012 :  12:11:54  Show Profile  Reply with Quote
quote:
Originally posted by JohnDW

Txs,

it works great.
But I have a few questions:
1. Does it mean I have to update the view each year?
you can make it dynamic
see
http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

2. What with null values in the query (or view)?
Because I use the view to fill a datagridview where I calculate
the sum. And with Null values this gives an error.
its just a warning
you can disable it by setting ANSI_WARNINGS OFF

Is there a way to fill in an integer (0 value as integer) so the calcultion in vb.net can take place?
you mean replace NULL with 0?
yes its possible using ISNULL or COALESCE()


Txs,
John



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JohnDW
Starting Member

Belgium
45 Posts

Posted - 09/21/2012 :  11:30:09  Show Profile  Reply with Quote
Txs,

I'll learned from it.

John
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 09/21/2012 :  12:30:53  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000