adding parameters to a query

Hello,

How can I modify this query for adding date and hours in parameters ? :

select a.[In #]
,b.[Urgency ID:]
      ,a.[Sequence]
      ,a.[Date]
      ,a.[From Group:]
      ,a.[To Group:]
      ,a.[Action ID]
      ,a.[Login ID]
from _SM_.[Inc Details] a,_SM_.Inc b
where [Date] <= (DATEADD(hh,19, DATEADD(dd, DATEDIFF(dd,0,GETDATE())-1, 0)))
and [Date] >=   (DATEADD(hh, 1, DATEADD(dd, DATEDIFF(dd,0,GETDATE())-1, 0))) 

Open in new window


Thanks

Regards
bibi92Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
try look for DatePart function

ref:
DATEPART (Transact-SQL)
https://msdn.microsoft.com/en-us/library/ms174420.aspx
0
Ryan ChongCommented:
To add hour comparison, try like:
...
and DATEPART(hour, [yourDatefield] ) = <value> 

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>How can I modify this query for adding date and hours in parameters ? :

Give us a 'for example' of how you intend to use this.

Changing the query to a SP and adding parameters..
CREATE PROC your_proc_name (@hours_from_add int, @hours_to_add int) AS
select a.[In #]
,b.[Urgency ID:]
      ,a.[Sequence]
      ,a.[Date]
      ,a.[From Group:]
      ,a.[To Group:]
      ,a.[Action ID]
      ,a.[Login ID]
from _SM_.[Inc Details] a,_SM_.Inc b
where [Date] <= (DATEADD(hh,@hours_to_add DATEADD(dd, DATEDIFF(dd,0,GETDATE())-1, 0)))
and [Date] >=   (DATEADD(hh, @hours_from_add, DATEADD(dd, DATEDIFF(dd,0,GETDATE())-1, 0))) 
GO

Open in new window


btw I recommend losing the spaces and colons : and pound signs # in your column names, which is forcing the use of square brackets [ ].   Also wouldn't be a bad idea to change the Date column to something other than date, which is a column data type, to avoid any confusion.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
#s do not force brackets to be used around column names, it is the space forcing that.  "In#" would be a perfectly valid column name.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.