Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

SQL Query that will run on schedule and query data need to be currentdate.

SELECT *
  FROM [Prod].[Data].[Data_IncomingUpdate]
  WHERE InsertTime >='2018-01-03'
  PRINT @@ROWCOUNT
 
Dear expert
Up there is a simple query that shows InsertTime >='2018-01-03' now i want instead of catch "2018-01-03' I want it first catch execution date, like today is 8/2/2018, I want first make a variable like: $Currentdate and reformat it to '2018-02-08' then add it after the WHERE InsertTime >= $CurrentDate is this possible to do? This is for i want this to run every month 2th of feb. And the data I want to query is current date and format need to be like that '2018-02-02' etc.

Thanks
Avatar of Manju
Manju
Flag of India image

SELECT *
  FROM [Prod].[Data].[Data_IncomingUpdate]
  WHERE InsertTime >=getdate()
  PRINT @@ROWCOUNT
SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi
WeTi

ASKER

what is the output of this:
DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
Output is 2018-02-08 00:00:00.000.

Try this


SELECT *
  FROM [Prod].[Data].[Data_IncomingUpdate]
  WHERE InsertTime >=convert(date,DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))
  PRINT @@ROWCOUNT

Output of convert(date,DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)) is 2018-02-08 (today's date)
Avatar of WeTi

ASKER

Well I want it to be only 2018-02-08 tho. I don't want those 00:00:00
pls check my previous comment. It will have only the date.
Avatar of WeTi

ASKER

Ran this in sql studio
Output of convert(date,DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)) is 2018-02-08 (today's date)
Error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'Output'.
Msg 105, Level 15, State 1, Line 6
Unclosed quotation mark after the character string 's date)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi

ASKER

Thanks for the kindly help, worked fine.
Well I want it to be only 2018-02-08 tho. I don't want those 00:00:00

That's not really logical.  If it's a datetime, it will always have a time.  The time of midnight / 00:00:00 is start of day and is there whether you code it or consider it or not.

Good luck with future qs.