Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

OrderDate = Today Expand / Collapse
Author
Message
Posted Friday, February 22, 2013 6:49 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, July 24, 2014 7:27 AM
Points: 306, Visits: 580
How to make code below working? (OrderDate is date type)

I want to add some code if OrderDate is today

if (select OrderDate from Order) = CONVERT(VARCHAR(20), Getdate(), 101)
Begin
...some code...
End

Post #1423031
Posted Friday, February 22, 2013 6:54 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 8:36 AM
Points: 56, Visits: 511
Please check this code:
if exists (select OrderDate from Order) = CONVERT(VARCHAR(20), Getdate(), 101)
Begin
...some code...
End

Post #1423038
Posted Friday, February 22, 2013 7:08 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:44 AM
Points: 1,930, Visits: 3,156
if exists (select OrderDate from Order where OrderDate = CAST(Getdate() AS DATE))
Begin
...some code...
End

Note with this code, the if check will evaluate to true if any value of OrderDate in the table is today - is that the desired behaviour?

Cheers
Gaz
Post #1423046
Posted Friday, February 22, 2013 8:56 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, August 2, 2014 1:47 AM
Points: 246, Visits: 1,168
DECLARE @TODAY DATETIME = CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),126))
SELECT @TODAY

DECLARE @TOMORROW DATETIME = @TODAY+1
--
-- Alternative
-- DECLARE @TOMORROW DATETIME = DATEADD(DD,1,@TODAY)
-- DECLARE @TOMORROW DATETIME = DATEADD(HH,24,@TODAY)
--
SELECT @TOMORROW

SELECT * FROM TheTable WHERE TheDate >= @TODAY AND TheDate < @TOMORROW

Or something similar to this.
Because time is a continuous and you do not want to depend on granulaty of the storage system, you want to check from a beginning time where the time is included up to a end time where the time is excluded.

Why?
In this way it does not matter if you express your time in months, weeks, days, hours, minutes etc.

For example if you want to check for an half an hour, you check for the greater or equal to the begin time and less then the begintime plus 30 minutes.

Do not try to create an endtime which is included, because this is very type dependend. For a day the last endtime which is stored in datetime format is 23:59:59.997. But for other formats it is different. While the next day 00:00:00 is available in all storage types (I strongly hope )
(For shortdatetime it is 23:59:59 Me thinks).

ben brugman
Post #1423110
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse