I know the datepart is returned as an int. The string lives in WeeklyFreq column with nvarchar data type in the Alerts table.
I’ve tried every variation of cast and convert, but to no avail. The Google has let me down as well.
I’m missing something that must be very obvious here. Any help would be greatly appreciated.
SELECT *
FROM Alerts
WHERE DATEPART(dw, GETDATE()) IN
(SELECT WeeklyFreq
FROM Alerts
WHERE Id = 1)
Thank you both very much for your input. The solution seems to be:
SELECT *
FROM Alerts
WHERE ((SELECT WeeklyFreq
FROM Alerts
WHERE Id = 1) LIKE
N'%' + CAST(DATEPART(dw, GETDATE()) AS nvarchar(2)) + N'%')
I agree that storing the data in separate columns rather than in a string would be the proper solution, but circumstances don’t allow for that at this juncture.
How do I accept both of your answers as the best choice?
The big picture looks like this:
CREATE PROCEDURE [dbo].[usp_QueueOrdersWeeklyBetween] AS
DECLARE @Id int
DECLARE @VendorId int
DECLARE @SurveyId int
DECLARE @DealerId int
DECLARE @ItemId int
DECLARE @Email nvarchar(255)
DECLARE Transaction_Cursor CURSOR FOR
SELECT Orders.Id, Orders.VendorId, Orders.SurveyId, Orders.DealerId, Orders.ItemId, Retailers.Email
FROM Orders INNER JOIN
Retailers ON Retailers.DealerId = Orders.DealerId INNER JOIN
Alerts ON Alerts.VendorId = Orders.VendorId
WHERE (Orders.Queued = 0) AND (Orders.SurveyQueued = 0) AND (Alerts.Occurs = 3) AND (DATEPART(hh, GETDATE()) BETWEEN
Alerts.WeeklyDailyFreqEveryStart AND Alerts.WeeklyDailyFreqEveryEnd - 1) AND (Alerts.Alert1 = 1) AND (Alerts.WeeklyDailyFreq = 2) AND
(Alerts.WeeklyFreq LIKE N'%' + CAST(DATEPART(dw, GETDATE()) AS nvarchar(2)) + N'%')
OPEN Transaction_Cursor
FETCH NEXT FROM Transaction_Cursor
INTO @Id, @VendorId, @SurveyId, @DealerId, @ItemId, @Email
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO MailQueue (OrderId, VendorId, SurveyId, DealerId, ItemId, MailedTo, WhenQueued)
VALUES (@Id, @VendorId, @SurveyId, @DealerId, @ItemId, @Email, GetDate())
UPDATE Orders SET Queued = 1 WHERE ID = @Id
FETCH NEXT FROM Transaction_Cursor
INTO @Id, @VendorId, @SurveyId, @DealerId, @ItemId, @Email
END
CLOSE Transaction_Cursor
DEALLOCATE Transaction_Cursor
GO
This is not how the
INoperator works, see msdn. You say that weeklyfreq is of type nvarchar, so this seems to contain a comma-separated list of the values you want to check against. Otherwise I couldn’t see a reason why you’d limit the subquery to a single row. For the IN operator to work the column would have to be of type int as well.What could work in this case:
This assumes that weeklyfreq contains a series of one-digit numbers. Also, this will only work as long as the subquery returns only a single row. The current day of week is converted to varchar and enclosed in ‘%’ to make the
LIKEoperator work.But I would not recommend to use this; if you can, store your data in a different way that allows for easier analysis.
Also, I can’t quite see where exactly you’re going with this. For this to make any sense, the Alerts table of the outer select should be either referenced in the WHERE clause or within the subquery (that would then be a correlated subquery).
The only thing I can see that’d make sense is to lose the subquery, then it’d be
This you can additionally filter for an user’s ID by adding to the WHERE clause.