Ignacio
Ignacio

Reputation: 5

Insert into table when the day is Monday

Is there a way in which I can insert data from one table into another when today is Monday?

I tried making something like the below, using CASE WHEN, however it doesn't quite work right, any help would be welcomed

INSERT INTO [dbo].[WF_All]

SELECT (CASE WHEN (DATENAME(WEEKDAY,FLOOR(convert(float,getdate()))))='MONDAY' THEN  

(
SELECT [Parent Number]
  ,[Parent Name]
  ,[Customer Number]
  ,[Customer Name]
  ,[Collector]
  ,[Outstanding]

   FROM dbo.[Invoices]
 )

  ELSE  NULL END )

Upvotes: 0

Views: 716

Answers (2)

Nicholas Hunter
Nicholas Hunter

Reputation: 1845

Is this what you want.

if datename(weekday,getdate()) = 'MONDAY' then
    INSERT INTO [dbo].[WF_All] (
        -- column names go here
    )
    SELECT [Parent Number]
      ,[Parent Name]
      ,[Customer Number]
      ,[Customer Name]
      ,[Collector]
      ,[Outstanding]
   FROM dbo.[Invoices]
 

Upvotes: 1

eshirvana
eshirvana

Reputation: 24603

yes , like this :

INSERT INTO [dbo].[WF_All]
SELECT [Parent Number]
  ,[Parent Name]
  ,[Customer Number]
  ,[Customer Name]
  ,[Collector]
  ,[Outstanding]
FROM dbo.[Invoices]
WHERE DATENAME(WEEKDAY,GETDATE()) = 'Monday'

or

... WHERE DATEPART(WEEKDAY, GETDATE()) = 2 --Monday

Upvotes: 1

Related Questions