Scheduled datetime conversion error (SQL) - sql-server

When trying to convert a datetime field to get rid of NULL's I am getting the error that the datetime cannot be converted.
This query is populating another database where the column datatype is date.
This job runs perfectly fine when not scheduled, but results in an error during the scheduled insert job.
Any ideas?
, (SELECT
CASE
WHEN MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(VARCHAR(50), MIN([Discount Ledger].[Posting Date]), 121)
ELSE '-'
END
FROM
[Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN
[Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN
[Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN
[Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN
[Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE
[MixMatch].[No_] = [Item Ledger].[Item No_]
AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code]
AND [Store Price Group].[Store] = [Item Ledger].[Location Code]) AS "Discount Start Date"

You can do this using ISNULL. Since you are looking for the MIN date, I will use GETDATE() as the argument/replacement value for NULL. Unless your data has future dates, then GETDATE() should function as the MAX value. Unless you want the CASE statement to evaluate the NULL values as the MIN. In that case, you can use an empty string '' or '1900-01-01' instead of GETDATE().
, (SELECT
CASE
WHEN
MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(varchar(50),MIN(ISNULL([Discount Ledger].[Posting Date],GETDATE())),121)
ELSE '-'
END
FROM [Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN [Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN [Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN [Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN [Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE [MixMatch].[No_] = [Item Ledger].[Item No_] AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code] AND [Store Price Group].[Store] = [Item Ledger].[Location Code])
AS "Discount Start Date"

Related

SQL Server : restricting data range

The query below is supposed to show details for 2 types of products: DIS001 and DIS002.
When DIS002, this should "reset" the query, so that it only shows DIS001 products which were sold after that the date when DIS002 was sold.
To be honest, I'm not even sure if this is possible. I'll be grateful for any suggestions.
SELECT DISTINCT
Sales.RaisedDateTime AS [Date],
Contacts.ContactID AS [Contact ID],
Contacts.SiteID AS [Site ID],
Sales.ProductID AS [Product],
CASE
WHEN Sales.ProductID = 'DIS002'
THEN Sales.RaisedDate
ELSE CONVERT(DATETIME, CONVERT(VARCHAR(10), '2019-10-28', 101) + ' 00:00:00')
END AS [Start Date]
FROM
((Bookings.Bookings Bookings
INNER JOIN
Contacts.Contacts Contacts ON (Bookings.ContactID = Contacts.ContactID))
INNER JOIN
Sales.Sales Sales ON (Bookings.ContactID = Sales.ContactID))
WHERE
(Sales.ProductID = 'DIS001' AND
Sales.RaisedDate >= MAX([Start Date])
There are lots of different syntax's to solve this, but here is one:
;with DIS002 as (
select ContactID, max(RaisedDate) as DIS002Date
from Sales.Sales
where ProductID = 'DIS002'
group by ContactID
)
SELECT DISTINCT
Sales.RaisedDateTime AS [Date],
Contacts.ContactID AS [Contact ID],
Contacts.SiteID AS [Site ID],
Sales.ProductID AS [Product]
FROM
((Bookings.Bookings Bookings
INNER JOIN
Contacts.Contacts Contacts ON (Bookings.ContactID = Contacts.ContactID))
INNER JOIN
Sales.Sales Sales ON (Bookings.ContactID = Sales.ContactID))
LEFT JOIN
DIS002 on DIS002.ContactID = Sales.ContactID
WHERE
Sales.ProductID = 'DIS001' AND
Sales.RaisedDate >= isnull(DIS002date,'1900-01-01')

I want to convert inner join Query in SubQuery

I am using inner join in query but I want Subquery instead of join
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
header.[Customer Reference] as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
inner join [Uneek Clothing Company Ltd$Sales Invoice Header] header on ledger.[Entry No_]=header.[Cust_ Ledger Entry No_]
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc offset 0 ROWS FETCH NEXT 20 ROWS ONLY
[Entry No_] is foreign key in [Uneek Clothing Company Ltd$Sales Invoice Header] with name [Cust_ Ledger Entry No_]
if you really want to drop the join and use a subquery instead,
it will look something like this.
Since you did not use an table alias for all columns, I had to assume that no other column of table in the subquery is used anywhere else. If that is not the case then this will not work
It will also only work if the subquery returns no more then 1 row, otherwise a subquery cannot be used at all
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
( select header.[Customer Reference]
from [Uneek Clothing Company Ltd$Sales Invoice Header] header
where ledger.[Entry No_] = header.[Cust_ Ledger Entry No_]
) as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc
offset 0 ROWS FETCH NEXT 20 ROWS ONLY

Microsoft SQL Server: how can I group by this column? GROUP BY not working for me

I want to group by Quote ID, I am not able to get it to work though. I've been banging my head on this one for a while.
EDIT: I'm trying to group the Quantity items into one column, so for example if a Quote ID column has 3 items with the same Quote ID but different quantities, I want to display them in the same row in this fashiong - $QuoteID, $QuantityFromRow1, $QuantityFromRow2, $QuantityFromRow3.
SELECT TOP(100)
PPV8.dbo.CUSTOMER.ID As [Customer ID]
, PPV8.dbo.CUSTOMER.NAME As [Customer]
, PPV8.dbo.CUSTOMER.CITY As City, Null As [PM]
, PPV8.dbo.CUSTOMER.STATE As State
, PPV8.dbo.QUOTE_PRICE.QUOTE_ID As [Quote ID]
, FLOOR(PPV8.dbo.QUOTE_PRICE.QTY) As Quantity
, PPV8.dbo.QUOTE_LINE.PART_ID As [Part ID]
, SUBSTRING(PPV8.dbo.QUOTE_LINE.CUSTOMER_PART_ID, 6, 12) As [Style #]
, PPV8.dbo.QUOTE_LINE.DESCRIPTION As Description
, PPV8.dbo.QUOTE_PRICE.UNIT_PRICE As [Unit Price]
, PPV8.dbo.QUOTE_LINE.CREATE_DATE As [Create Date]
, PPV8.dbo.QUOTE.SALESREP_ID As [Rep]
, PPV8.dbo.QUOTE.STATUS As [Quote Status]
, PPV8.dbo.QUOTE.FOLLOWUP_DATE As [Follow Up]
, PPV8.dbo.QUOTE.EXPECTED_WIN_DATE As [Due Date]
FROM
CUSTOMER
INNER JOIN
QUOTE ON PPV8.dbo.CUSTOMER.ID = PPV8.dbo.QUOTE.CUSTOMER_ID
INNER JOIN
QUOTE_LINE ON PPV8.dbo.QUOTE.ID = PPV8.dbo.QUOTE_LINE.QUOTE_ID
INNER JOIN
QUOTE_PRICE ON PPV8.dbo.QUOTE_LINE.QUOTE_ID = PPV8.dbo.QUOTE_PRICE.QUOTE_ID
AND PPV8.dbo.QUOTE_LINE.LINE_NO = PPV8.dbo.QUOTE_PRICE.QUOTE_LINE_NO
WHERE
PPV8.dbo.QUOTE.STATUS = 'A'
AND PPV8.dbo.QUOTE.EXPECTED_WIN_DATE > '20170101'
ORDER BY
PPV8.dbo.QUOTE.EXPECTED_WIN_DATE ASC
All (non aggregate) columns in the select statement must be included in the group by clause
So you are missing the point of group by

SQL Server Incorrect Syntax error

Thank for your help and reading, I have the below query and I don't understand why this error message occurs:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Price'
I am able to query this data column (Wholesale Price) in a standalone query. I'm using SQL Server 2005 Management Studio .
SELECT
ILE.[Location Code],
ILE.SUM(Quantity) AS "Transfer Qty",
PP.SUM(Wholesale Price) AS "Transfer Cost (HK)"
FROM
[DB].[dbo].[Company$Item Ledger Entry] ILE
INNER JOIN
[DB].[dbo].[Company$Purchase Price] PP ON ILE.[Item No_] = PP.[Item No_]
AND ILE.[Variant Code] = PP.[Variant Code]
WHERE
ILE.[Entry Type] = '4'
AND ILE.[Location Code] NOT LIKE '%STAFF%'
AND ILE.[Location Code] NOT LIKE 'WHSPACKAGE'
AND ILE.[Location Code] NOT LIKE 'WHS'
GROUP BY
ILE.[Location Code], ILE.[Quantity], PP.[Wholesale Price]
ORDER BY
[Location Code]
Thanks!
Regards,
Patrick
In SQL, you have to escape names that contain special characters -- and spaces are special characters. Because reading and writing code that has lots of square braces is cumbersome, the general advice is to avoid using such names.
In your case, you are missing the square braces:
SELECT ILE.[Location Code], ILE.Sum(Quantity) as "Transfer Qty",
Sum(PP.[Wholesale Price]) as "Transfer Cost (HK)"
FROM [DB].[dbo].[Company$Item Ledger Entry] ILE INNER JOIN
[DB].[dbo].[Company$Purchase Price] PP
ON ILE.[Item No_] = PP.[Item No_] AND
ILE.[Variant Code] = PP.[Variant Code]
Where ILE.[Entry Type] = '4' AND
ILE.[Location Code] NOT LIKE '%STAFF%' AND
ILE.[Location Code] NOT LIKE 'WHSPACKAGE' AND
ILE.[Location Code] NOT LIKE 'WHS'
Group by ILE.[Location Code], ILE.[Quantity]
Order by [Location Code];
In addition:
PP.SUM() doesn't make sense. The table alias goes with the column name.
Putting the wholesale price in the GROUP BY doesn't make sense. You want to aggregate the value, so it usually wouldn't go there.
Check this select statement for your error
SELECT
ILE.[Location Code],
ILE.SUM(Quantity) AS "Transfer Qty",
PP.SUM([Wholesale Price]) AS "Transfer Cost (HK)"--Error line
Thank you so much! Gordon!
Both SUM statement should be wording this format,
Sum(ILE.[Quantity] and Sum(PP.[Wholesale Price])
SELECT ILE.[Location Code], Sum(ILE.[Quantity]) as "Transfer Qty",
Sum(PP.[Wholesale Price]) as "Transfer Cost (HK)"
FROM [Dummy-28-Oct-2016].[dbo].[TEST ENV$Item Ledger Entry] ILE
INNER JOIN [Dummy-28-Oct-2016].[dbo].[TEST ENV$Purchase Price] PP
ON ILE.[Item No_] = PP.[Item No_]
AND ILE.[Variant Code] = PP.[Variant Code]
Where ILE.[Entry Type] = '4'
AND ILE.[Location Code] NOT LIKE '%STAFF%'
AND ILE.[Location Code] NOT LIKE 'WHSPACKAGE'
AND ILE.[Location Code] NOT LIKE 'WHS'
Group by ILE.[Location Code], ILE.[Quantity]
Order by [Location Code];

How to return NULL value is no records are found in multiple join conditions

I'm creating a report where i need to show all Purchase Indents as per filters and corresponding purchase lines of these indent lines.
If there is no purchase line found as per my condition then it should return NULL value. But report is not showing Indents for which there are no records in purchase lines.
I'm joining 4 tables - INH,INL,PL,PH. The Query is shown below.
SELECT
INH.No_
,INH.[Approved Date]
,INH.Indentor
,INL.No_ AS ItemCode
,INL.description
,INL.Description2
,INL.Req_Quantity
,INL.[Unit of Measure]
,PL.[Document No_]
,PH.[Order Date]
,PL.Quantity AS OrderedQuantity
,PL.[Quantity Received]
FROM [ICTL | HYDERABAD$Indent Header] AS INH
INNER JOIN [ICTL | HYDERABAD$Indent Line] AS INL ON INH.No_ = INL.[Document No_]
LEFT OUTER JOIN [ICTL | HYDERABAD$Purchase Line] AS PL ON INL.[Document No_] = PL.[Indent No_] AND INL.[Line No_] = PL.[Indent Line No_]
LEFT OUTER JOIN [ICTL | HYDERABAD$Purchase Header] AS PH ON PL.[Document No_] = PH.No_
WHERE (UPPER(INH.Indentor) = UPPER(#Name)
OR #Name IS NULL)
AND (INL.No_ <> '')
AND (INH.[Approved Date] >= #StartDate
OR #StartDate IS NULL)
AND (INH.[Approved Date] <= #EndDate
OR #EndDate IS NULL)
AND (PL.[Document Type] = 1)
ORDER BY ItemCode
The problem is that you are have PL.[Document Type] = 1 in your query. This will automatically exclude all rows where there is no PL record (your OUTER JOIN does not matter then).
Either you'd have to do PL.[Document Type] IS NULL OR PL.[Document Type] = 1 in the WHERE clause, or you'd have to add the PL.[Document Type] = 1 to the JOIN condition of the PL table.
Possible this be helpful for you -
SELECT
INH.No_
, INH.[Approved Date]
, INH.Indentor
, INL.No_ AS ItemCode
, INL.description
, INL.Description2
, INL.Req_Quantity
, INL.[Unit of Measure]
, PL.[Document No_]
, PH.[Order Date]
, PL.Quantity AS OrderedQuantity
, PL.[Quantity Received]
FROM [ICTL | HYDERABAD$Indent Header] INH
JOIN [ICTL | HYDERABAD$Indent Line] INL ON INH.No_ = INL.[Document No_]
LEFT JOIN [ICTL | HYDERABAD$Purchase Line] PL ON INL.[Document No_] = PL.[Indent No_] AND INL.[Line No_] = PL.[Indent Line No_]
LEFT JOIN [ICTL | HYDERABAD$Purchase Header] PH ON PL.[Document No_] = PH.No_
WHERE UPPER(INH.Indentor) = UPPER(ISNULL(#Name, INH.Indentor))
AND INL.No_ <> ''
AND INH.[Approved Date] BETWEEN ISNULL(#StartDate, '19000101') AND ISNULL(#EndDate, '30000101')
AND ISNULL(PL.[Document Type], 1) = 1
ORDER BY ItemCode

Resources