Excel - SQL Query - Parameters check for null input

bell282

New Member
Joined
Jun 2, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am using excel 365.
I have a drop down box as an input parameter to a query. I need to be able to check is that input parameter is null.

State: <drop down of available states>

Report showing city, zip code and other data

For the drop down box, I am using the following query:
Select null as [State]
UNION
Select distinct State from tbl_states

Then the query to display the report:

What I am using in the sql statement is:
Select columns
from table_State s
where ( s.State = ? or ? is null )

This is not working.

I know in tsql, it would be written:
declare @State varchar(50)
set @State= 'Ohio'
-- set @State = null
select distinct state
from tbl_States s
where ( (@State is null) or (s.State = @State) )

Any suggestions on how to correct my query?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

bell282

New Member
Joined
Jun 2, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Thanks! Yes, isnull does work when I run the query in Microsoft sql server management studio but not excel. I think the 'null' value that I am trying to return is being converted is an empty string.

stumped...
 

bell282

New Member
Joined
Jun 2, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Other items I have tried:
Select columns
from table
where column = ? or isnull(?, '') = ''

I have both parameters set to the same input parameter

With this approach, I receive an error message 'string date; right truncation'
 

Watch MrExcel Video

Forum statistics

Threads
1,129,858
Messages
5,638,722
Members
417,049
Latest member
baka416

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top