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
 SQL Query

Author  Topic 

Stevan23
Starting Member

15 Posts

Posted - 2012-03-23 : 04:28:23
Hi all,

Sorry if I've posted this in the wrong forum. I'm new to the SQL world and have been trying to get this query to work:

DECLARE @FromDate Datetime
DECLARE @ToDate DateTime

SELECT DISTINCT OWO.DocEntry, OWO.DocNum, OWO.OcrCode, OWO.PlannedQty, OWO.CmpltQty, T1.Dscription, OWO.PostDate, -sum(T2.TransValue) as 'Actual Component Cost'

FROM SBO_KF_Live.dbo.OWOR OWO

LEFT JOIN SBO_KF_Live.dbo.IGN1 T1 ON OWO.DocNum = T1.BaseRef

INNER JOIN SBO_KF_Live.dbo.OINM T2 ON OWO.DocEntry = T2.AppObjAbs AND OutQTY>0

WHERE (OWO.ItemCode = '10094' or OWO.ItemCode = '10328' or OWO.ItemCode = '10329' or OWO.ItemCode = '10499' or OWO.ItemCode = '10311' or OWO.ItemCode = '10312') and OWO.PostDate BETWEEN '{?@FromDate}' AND '{?@ToDate}'

GROUP BY OWO.DocEntry, OWO.DocNum, OWO.OcrCode, OWO.PlannedQty, OWO.CmpltQty, T1.Dscription, OWO.PostDate, (T1.StockPrice*OWO.CmpltQty)

But it keeps coming up with an error saying: "conversion failed when converting datetime from character string" Msg 241, Level 16, State 1, Line 4

I have checked the database and the OWOR.PostDate datatype is Datetime and I have a feeling it's to do with the WHERE statement and choosing the parameters for the date. Only thing is that's the format the parameters need to be for Crystal Report integration.

If someone could give me a hand that would be terrific.

Thank you,
Stevan23

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-23 : 04:44:20
... and OWO.PostDate BETWEEN @FromDate AND @ToDate

btw. where did you set the vars to a value?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Stevan23
Starting Member

15 Posts

Posted - 2012-03-23 : 05:11:27
Hi webfred,

I don't think I set them. I'm presuming you mean something like:
DECLARE @FromDate DateTime
DECLARE @ToDate Datetime

SET @FromDate = '01/02/2012'
SET @ToDate = '29/02/2012'


Go to Top of Page

Stevan23
Starting Member

15 Posts

Posted - 2012-03-23 : 05:14:16
Hi webfred,

Here's an adjustment I've made to the query:
DECLARE @FromDate Datetime;
set @FromDate = '2012-02-01 00:00:00'
SELECT CONVERT(CHAR(11),@FromDate,111) + SUBSTRING(CONVERT(CHAR(19),@FromDate,100),13,19)

DECLARE @ToDate DateTime;
set @ToDate = '2012-02-29 00:00:00'
SELECT CONVERT(CHAR(11),@ToDate,111) + SUBSTRING(CONVERT(CHAR(19),@ToDate,100),13,19)


SELECT DISTINCT OWO.DocEntry, OWO.DocNum, OWO.OcrCode, OWO.PlannedQty, OWO.CmpltQty, T1.Dscription, OWO.PostDate, -sum(T2.TransValue) as 'Actual Component Cost'

FROM SBO_KF_Live.dbo.OWOR OWO

LEFT JOIN SBO_KF_Live.dbo.IGN1 T1 ON OWO.DocNum = T1.BaseRef

INNER JOIN SBO_KF_Live.dbo.OINM T2 ON OWO.DocEntry = T2.AppObjAbs AND OutQTY>0

WHERE (OWO.ItemCode = '10094' or OWO.ItemCode = '10328' or OWO.ItemCode = '10329' or OWO.ItemCode = '10499' or OWO.ItemCode = '10311' or OWO.ItemCode = '10312') and OWO.PostDate BETWEEN @FromDate AND @ToDate

GROUP BY OWO.DocEntry, OWO.DocNum, OWO.OcrCode, OWO.PlannedQty, OWO.CmpltQty, T1.Dscription, OWO.PostDate, (T1.StockPrice*OWO.CmpltQty)
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-23 : 05:35:15
And now? Do you still get the error message?
What is the datatype of column PostDate?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Stevan23
Starting Member

15 Posts

Posted - 2012-03-23 : 05:44:27
Thanks for that!

The PostDate is a datetime datatype.

The query completes successfully, but interestingly enough there are three tables:
1. No column name with date of first variable (01/02/2012)
2. No column name with date of second variable (29/02/2012)
3. All the information I was looking for in the query, with the DocEntry, DocNum etc.
Go to Top of Page
   

- Advertisement -