Excel SQL Connection if parameter is blank

Dmcnich15

New Member
Joined
Feb 14, 2015
Messages
8
Hello-

Here is my setup. I have a data connection in Excel that runs a SQL query. There are 2 parameters, year and month, that point to cells in excel. I would like if month is left blank to pull everything for the year.

For instance

If year = 2017 and month = 12 pull december of 2017
If year = 2017 and the cell for month is blank pull all of 2017


Im not sure if this is possible but if so what would be the best way to use it? I tried adding a case statement in my SQL for isnull but that isnt translating to excel
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Something like this should work:
SELECT fields FROM table WHERE YEAR(datefield) = [year param] AND ([month param] = 0 or [month cell] IS NULL or MONTH(datefield) = [month param]
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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
Back
Top