Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Using a 'created' field name in a "WHERE" statement

Using a 'created' field name in a "WHERE" statement

Using a 'created' field name in a "WHERE" statement

Good afternoon. Is it possible to use a 'created' field name in a "WHERE" statement?

Some of our tables contain entries where the record was opened (presumably in eror) and closed at the same time so when we calculate the difference the result is 0. What I thought might be useful would be to eclude the records at selection time. Here is what I mean:


When Datediff(day,epstart,EpEnd)/365.25 is Null
Then Datediff(day,epstart,getdate())/365.25
Else Datediff(day,epstart,EpEnd)/365.25
End As StayYears

WHERE StayYears <> 0

Many thanks,

RE: Using a 'created' field name in a "WHERE" statement

First of all Null is not 0, a datediff of two equal values will not be Null, unless one of the datetime fields is Null.
Then you will not be able to use the created name in WHERE, no, unfortunately not.
But you can simplify the expression and repeating it in the WHERE clause will not insult the eye as much.


Datediff(day,epstart,COALESCE(EpEnd,getdate())/365.25 As StayYears 

Bye, Olaf.

RE: Using a 'created' field name in a "WHERE" statement

Hi Olaf, thanks for the response. Some of the episodes don't have an end date recorded and that produces a NULL.

I'll have to have a look at the COALESCE feature.

The ones that were opened and immediately closed are the ones that produced the 0 (actually 0.000000).

Yes, I'd figured that I'd have to use the same expression in the WHERE statement.

Many thanks,

RE: Using a 'created' field name in a "WHERE" statement

If you need a link: https://msdn.microsoft.com/de-de/library/ms190349....

ISNULL(EpEnd,getdate()) works quite similar and would also be sufficient in this case, as you only need one alternative value getdate() and know that replacement is never NULL. Read the paragraph about the differences in the COALESCE online help topic.

Bye, Olaf.

RE: Using a 'created' field name in a "WHERE" statement

OK, will do.

Many thanks.

Many thanks,

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close