Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - comparing dates to $null

Reply
 
Old 08-27-2007   #1 (permalink)
Hal Rottenberg


 
 

comparing dates to $null

I'm trying to find records matching a date within a certain amount of
time. Some of the source fields have no date specified and are
instead $null. As a result, I get some obvious errors. What is the
best way to structure my queries to avoid this error?

snippet:
$recentXpu = $xpu | ? { $_.StartDate -ge
[DateTime]::Now.AddDays(-60) }

error:
At line:1 char:41
+ $recentXpu = $xpu | ? { $_.StartDate -ge <<<<
[DateTime]::Now.AddDays(-60) }
The '-ge' operator failed: Could not compare "" to "6/28/2007 11:10:51
AM". Error: "Cannot conver
t value "6/28/2007 11:10:51 AM" to type "System.DBNull". Error:
"Object cannot be cast to DBNull.
"".
Quote:

>From my point of view, the error is irrelevant--obviously if the date
is $null it doesn't fit my criteria and should be tossed out.

I'd like to comfortably stay within one-liner territory which is where
I'm stalled.


My System SpecsSystem Spec
Old 08-27-2007   #2 (permalink)
Keith Hill [MVP]


 
 

Re: comparing dates to $null

"Hal Rottenberg" <halr9000@xxxxxx> wrote in message
news:1188227821.147518.250490@xxxxxx
Quote:

> I'm trying to find records matching a date within a certain amount of
> time. Some of the source fields have no date specified and are
> instead $null. As a result, I get some obvious errors. What is the
> best way to structure my queries to avoid this error?
>
> snippet:
> $recentXpu = $xpu | ? { $_.StartDate -ge
> [DateTime]::Now.AddDays(-60) }
>
How about this?

$recentXpu = $xpu | ? {$_ -and $_.StartDate -ge
[DateTime]::Now.AddDays(-60) }

--
Keith

My System SpecsSystem Spec
Old 08-27-2007   #3 (permalink)
Kiron


 
 

Re: comparing dates to $null

Try comparing $_.StartDate to $null:

$recentXpu = $xpu | ? { $_.StartDate -ne $null -and $_.StartDate -ge
[DateTime]::Now.AddDays(-60) }

--
Kiron

My System SpecsSystem Spec
Old 08-27-2007   #4 (permalink)
Hal Rottenberg


 
 

Re: comparing dates to $null

On Aug 27, 11:49 am, "Keith Hill [MVP]"
Quote:

> How about this?
>
> $recentXpu = $xpu | ? {$_ -and $_.StartDate -ge
> [DateTime]::Now.AddDays(-60) }
YES! I knew there was something easy that had not occurred to me.

Thanks Keith & Kiron.

My System SpecsSystem Spec
Old 08-27-2007   #5 (permalink)
Kiron


 
 

Re: comparing dates to $null

Since [dbNull] is the cause of -ge "Could not compare "System.DBNull" check
for this type instead:

$recentXpu = $xpu | ? { $_.StartDate -isNot [dbNull] -and $_.StartDate -ge
[DateTime]::Now.AddDays(-60) }

--
Kiron

My System SpecsSystem Spec
Old 08-27-2007   #6 (permalink)
Keith Hill [MVP]


 
 

Re: comparing dates to $null

"Keith Hill [MVP]" <r_keith_hill@xxxxxx_no_spam_I> wrote in message
news:081B7CCE-4644-45A0-8A3E-021724353A3A@xxxxxx
Quote:

> "Hal Rottenberg" <halr9000@xxxxxx> wrote in message
> news:1188227821.147518.250490@xxxxxx
Quote:

>> I'm trying to find records matching a date within a certain amount of
>> time. Some of the source fields have no date specified and are
>> instead $null. As a result, I get some obvious errors. What is the
>> best way to structure my queries to avoid this error?
>>
>> snippet:
>> $recentXpu = $xpu | ? { $_.StartDate -ge
>> [DateTime]::Now.AddDays(-60) }
>>
>
> How about this?
>
> $recentXpu = $xpu | ? {$_ -and $_.StartDate -ge
> [DateTime]::Now.AddDays(-60) }
>
Oops. Should have been:

$recentXpu = $xpu | ? {$_.StartDate -and $_.StartDate -ge
[DateTime]::Now.AddDays(-60) }

No need to compare explicitly to $null unless 0 is a valid value (since 0
evaluates to false).

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Comparing dates with If PowerShell
[string]$a = $null; $a -eq $null; $a -eq "" PowerShell
Dates of posts Vista General
Re: "Error: 'null' is null or not an object" when trying to view video Vista performance & maintenance
Gotcha: $null to [string] IS NOT $null PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46