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
 datetime

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-09-04 : 23:18:25
how can i pull data by datetime on same day? can't manually put the datetime as i would like to schedule it

daily 2014-09-05 00:00:00.000 to 2014-09-05 03:59:59.999

an idea:
select ... from ..
where goingOffdatetime >= GETDATE()
and goingOffdatetime <= ...

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-09-05 : 00:22:40
[code]WHERE goingOffdatetime >= dateadd(day, datediff(day, 0, getdate()), 0)
AND goingOffdatetime < dateadd(day, datediff(day, 0, getdate()), 0) + '04:00'[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-09-05 : 02:15:30
Avoid arithmetic operations on datetimes
WHERE	goingOffdatetime >= DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()), '19000101')
AND goingOffdatetime < DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()), '04:00')



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-09-05 : 03:25:16
Thanks Khtan and swepeso.

Swepeso, what does it meant avoid arithmetic operations on datetimes?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-09-05 : 04:48:29
Adding dates and times with new datatypes DATE and TIME is not possible with newer versions of SQL Server.
For backwards compatibility you will need to convert to DATETIME to be able to do arithmetic on dates.
See http://www.sqltopia.com/?page_id=35



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -