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
 use Stuff to define range of values?

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2014-11-13 : 07:16:12
I have this query that calculates the next possible shipping day, based on 3 conditions:

- It has to be a workingday (WORKTIMECONTROL: 1 workingday, 0 holiday) - marked green
- There might be extra days (@xdays) required by the process - marked blue
- Customer wants their goods to be shipped on special days - marked red:
select TOP 1 Transdate 
from WORKCALENDARDATE
where Transdate > @startday and WORKTIMECONTROL = 1 and DATEPART(WEEKDAY,TRANSDATE)-1 in (2,4) and

(select SUM(WCD.WORKTIMECONTROL)
from WORKCALENDARDATE as WCD
where WCD.Transdate > @startday and WCD.WORKTIMECONTROL = 1 and WCD.Transdate >= WORKCALENDARDATE.Transdate)

> @xdays


ORDER BY Transdate
The table that has the information regarding shipping days has this structure:
CollectiveShipment:
DELIVERYDAY  CUSTACCOUNT
------------------------
2 123456
4 123456
3 789101
So customer 123456 accepts shipping of goods only on tuesday and thursday as in the above example "... in (2,4)". Multiple shipping days means that the Subquery returns more than one record, which gives me a headache as I don't see how to integrate this portion into my query. I tried to use the stuff function as I formally need a result that can be provided that way; but the format is incorrect as it in varchar, while an array of integer is needed.

DATEPART(WEEKDAY,TRANSDATE)-1 in (select stuff((select ',' + CAST(DELIVERYDAY as nvarchar) from CollectiveShipment where custaccount = '123456'
for xml path('')),1,1,''))

Anybody sees how to acchieve this?

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-11-13 : 08:44:38
why not:

DATEPART(WEEKDAY,TRANSDATE)-1 in (select DELIVERYDAY from CollectiveShipment where custaccount = '123456')
Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2014-11-13 : 09:24:59
Because this expression returns several records... I already started to answer, without trying it out. But to my surprise: IT WORKS!!!

Thank you very much. Problem solved and a new lesson learned!
Go to Top of Page
   

- Advertisement -