Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot check for null values in sql database

Tags:

sql

sql-server

f#

Using the F# query syntax I'm trying to get all the records where a certain field is not null, but I can't seem to figure out a way of doing this.

First, I attempted just:

query {
    for h in dc.Table do
    where (h.SectorId <> null)
    select h
}

But an error stated The type 'Nullable<Guid>' does not have 'null' as a proper value. To create a null value for a Nullable type use 'System.Nullable()'. Thus, replacing the null with Nullable() as suggested, I used:

query {
    for h in dc.Table do
    where (h.SectorId <> Nullable())
    select h
}

When I use the above query in LINQPad, it does not retrieve any values, even though I know they exist. The issue appears to be in the SQL created:

-- Region Parameters
DECLARE @p0 UniqueIdentifier = null
-- EndRegion
SELECT [t0].[Id], [t0].[Name], [t0].[SectorId], [t0].[Blah], [t0].[Meh], [t0].[DisplayOrder]
FROM [Table] AS [t0]
WHERE [t0].[SectorId] <> @p0

Of course this will not work because NULL <> NULL in SQL will always be false; the where should read WHERE [t0].[SectorId] is not null. How can I check against null in F# queries?

like image 313
Alexander R Avatar asked Aug 16 '26 15:08

Alexander R


1 Answers

Try

query {
    for h in dc.Table do
    where h.SectorId.HasValue
    select h
}
like image 81
Daniel Avatar answered Aug 19 '26 05:08

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!