sql server 2005 - SQL check for NULLs in WHERE clause (ternary operator?) -


what sql equivalent c# statement be?

bool isinpast = (x != null ? x < datetime.now() : true)

i need construct where clause checks x < now() if x not null. x datetime needs null sometimes, , not null other times, , want where clause consider non-null values, , consider null values true.

right clause is:

dbo.assignments.[end] < { fn now() }

which works non-null cases, null values seem make expression evaluate false. tried:

dbo.assignments.[end] not null , dbo.assignments.[end] < { fn now() }

and seems have no effect.

for use in clause, have test separately

where dbo.assignments.[end] null or dbo.assignments.[end] < getdate() 

or can turn nulls date (that true)

where isnull(dbo.assignments.[end],0) < getdate() 

or can negative test against bit flag derived below

where case when dbo.assignments.[end] < getdate() 0 else 1 end = 1 


below explanation , how derive isinpast select clause.

bool isinpast = (x != null ? x < datetime.now() : true)

a bool can have 1 of 2 results, true or false.
looking closely @ criteria, only condition false when

x != null && x < now

given fact, becomes easy translation, given in sql, x < now can evaluated when x!=null, only 1 condition needed

isinpast = case when dbo.assignments.[end] < { fn now() } 0 else 1 end 

(1 being true , 0 being false)

not sure { fn now() } represents, if want sql server provide current time, use either getdate() or if working utc data, use getutcdate()

isinpast = case when dbo.assignments.[end] < getdate() 0 else 1 end 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -