SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


OrderDate = Today


OrderDate = Today

Author
Message
adonetok
adonetok
SSChasing Mays
SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)

Group: General Forum Members
Points: 637 Visits: 789
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
salliven
salliven
SSC-Enthusiastic
SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)

Group: General Forum Members
Points: 164 Visits: 678
Please check this code:

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


Gazareth
Gazareth
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3977 Visits: 5798
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
ben.brugman
ben.brugman
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1036 Visits: 2385

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 :-P)
(For shortdatetime it is 23:59:59 Me thinks).

ben brugman
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search