![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 I'd like to comfortably stay within one-liner territory which is where I'm stalled. |
My System Specs![]() |
| | #2 (permalink) |
| | 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) } > $recentXpu = $xpu | ? {$_ -and $_.StartDate -ge [DateTime]::Now.AddDays(-60) } -- Keith |
My System Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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) } Thanks Keith & Kiron. ![]() |
My System Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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) } > $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 Specs![]() |
![]() |
| 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 | |||