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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 =Today

Author  Topic 

sz1
Aged Yak Warrior

555 Posts

Posted - 2014-06-03 : 05:29:31
Guys,

Anyone tell me why this doesn't work? I want to return todays calls that are closed without having to change the dates all the time using a between clause.

declare @mydate date
Set @mydate = GETDATE()

select ItemNumber
From dbo.Item
where Status = 'Closed'
and convert(date,CreatedDateTime) = @mydate

This works by doing a between but why doesn't the @mydate read the getdate() is today from the createddatetime field?

Thanks
And CreatedDateTime between '2014-06-02 00:00:00' and '2014-06-02 23:59:59'

We are the creators of our own reality!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-06-03 : 05:43:29
run your working select and add the following columns to your select list.
then you can see if they are equal or not.

select ItemNumber,@mydate as mydate, convert(date,CreatedDateTime) as CreatedDateTime_converted
From dbo.Item
where Status = 'Closed'
and CreatedDateTime between '2014-06-03 00:00:00' and '2014-06-03 23:59:59'



Too old to Rock'n'Roll too young to die.
Go to Top of Page

sz1
Aged Yak Warrior

555 Posts

Posted - 2014-06-03 : 06:39:11
It did work after all there were none but 2 have come in now, your query helped me though to look again and I see why you did it that way.

Thanks :)

declare @mydate date
Set @mydate = GETDATE()

select ItemNumber, @mydate 'IsToday'
From dbo.Item
where Status = 'Closed'
and convert(date,CreatedDateTime) = @mydate

We are the creators of our own reality!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-06-03 : 08:02:45
[code]AND CreatedDateTime >= '20140602'
AND CreatedDateTime < '20140603'[/code]


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

sz1
Aged Yak Warrior

555 Posts

Posted - 2014-06-03 : 08:25:48
yep cool got that one,
thanks

We are the creators of our own reality!
Go to Top of Page
   

- Advertisement -