Excel/SQL - Parameter in Variable (2024)

  • If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.

  • Forums
  • Question Forums
  • Excel Questions
  • Thread starterWizardSleeve
  • Start dateOct 9, 2014
  • Tags
    excelparametersqlvariable

W

WizardSleeve

New Member
Joined
Oct 9, 2014
Messages
4
  • Oct 9, 2014
  • #1

I have built a report and, as an example, one of the tables uses unions to bring in various sets of information.

Previously they just wanted to look at the last 6 months, which was fine as I could use the getdate() function to bring back the data to Pivot within excel.

They have since decided they want to select the date that they would like to view six months up until.

Rather than add 7 parameters in for this table linked back to the same cell reference, then repeat this for other tables I though I could use a variable + a parameter

I simply did:
DECLARE @USEDATE AS DATETIME
SET @USEDATE = ?

? references a cell with a date, and I have tried numerous formats for this, but I keep getting the following errors:

Invalid Parameter Number
Invalid Descriptor Index

Putting in a date directly seems to work but using a parameter doesn't

Am I doing something wrong?
Any help is much appreciated! - thank you in advance.

Excel Facts

What do {} around a formula in the formula bar mean?

{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Sort by dateSort by votes

W

WizardSleeve

New Member
Joined
Oct 9, 2014
Messages
4
  • Oct 9, 2014
  • #3

Hi, thanks for the response.

I am not doing it in VB though, I've but the query through Microsoft Query (without the wizard)

Doing SET @USEDATE = GETDATE() works
Doing SET @USEDATE = '2014-10-01' works
but any variation of SET @USEDATE = ? doesn't

for example I tried using TEXT, FORMAT, CAST etc in the parameter field but no joy.

Upvote0

W

WizardSleeve

New Member
Joined
Oct 9, 2014
Messages
4
  • Oct 9, 2014
  • #4

Sorry missed the bit where you wanted me to post my code

The who query works apart from when trying to use a Parameter in there.

The cell it is referencing is a formula that is a date
I have tried various formats on the cell including TEXT to format it into a more recognised YYYY-MM-DD

If you think posting the whole query will help, I will, but it is purely the parameter section that doesn't work.

Upvote0

P

pechipun

New Member
Joined
Nov 28, 2014
Messages
1
  • Nov 28, 2014
  • #5

Hi,
I have the same problem with declared parametres using "?" .
In my case I'm trying this query:

SET NOCOUNT ON
DECLARE @DATA DATETIME
SET @DATA = ?

SELECT

C.ID_DATA_INCIDENT ,
C.REGISTRE ,
NULLIF(C.VOLUM_MESURAT+C.VOLUM_NO_MESURAT,0) ,
C.JUSTIFICACIO

FROM
V_PERDUES_CORRECCIONS C

WHERE
C.SUBCATEGORIA IN ('CNMNF','CMNF')
AND YEAR(C.ID_DATA_INCIDENT) = YEAR(@DATA)
AND month(C.ID_DATA_INCIDENT) = MONTH(@DATA)

ORDER BY 1
For me Doing (like happens with WizardSleeve :
SET @Data = GETDATE() works
Doing SET @Data = '01/10/2014' works
but SET @Data = ? doesn't

Is there any solution?
Thanks very much for your help!Excel/SQL - Parameter in Variable (1)

Upvote0

W

WizardSleeve

New Member
Joined
Oct 9, 2014
Messages
4
  • Nov 28, 2014
  • #6

pechipun said:

Hi,
I have the same problem with declared parametres using "?" .
In my case I'm trying this query:

SET NOCOUNT ON
DECLARE @DATA DATETIME
SET @DATA = ?

SELECT

C.ID_DATA_INCIDENT ,
C.REGISTRE ,
NULLIF(C.VOLUM_MESURAT+C.VOLUM_NO_MESURAT,0) ,
C.JUSTIFICACIO

FROM
V_PERDUES_CORRECCIONS C

WHERE
C.SUBCATEGORIA IN ('CNMNF','CMNF')
AND YEAR(C.ID_DATA_INCIDENT) = YEAR(@DATA)
AND month(C.ID_DATA_INCIDENT) = MONTH(@DATA)

ORDER BY 1
For me Doing (like happens with WizardSleeve :
SET @Data = GETDATE() works
Doing SET @Data = '01/10/2014' works
but SET @Data = ? doesn't

Is there any solution?
Thanks very much for your help!Excel/SQL - Parameter in Variable (2)

The way I got around my issue was running the code in VBA and pasting the recordset into the table rather than refreshing a table from Microsoft query.

It would appear excel has trouble handling parameters, especially when date fields

I think this is because the query reads the date as a number where as in the macro you can set it to be a string and format the text how you want.

Upvote0

C

Comfy

Well-known Member
Joined
Dec 21, 2009
Messages
3,386
  • Nov 28, 2014
  • #7

As has been pointed out the date within Excel is actually a number.

I'm a complete beginner when it comes to SQL but I had a similar issue and resolved it (if I remember correctly) using the following.

WHERE FORMAT(Date_Column, 'dd/mm/yy') >= FORMAT(?, 'dd/mm/yy')

Upvote0

You must log in or register to reply here.

Similar threads

K

  • Question

Export based on criteria and save as single separate workbooks

  • Kersh82
  • May 15, 2024
  • Excel Questions
Replies
0
Views
108

May 15, 2024

Kersh82

K

  • Question

VBA Hangs While Producing Complex Graph

  • pbornemeier
  • Feb 25, 2024
  • Excel Questions
Replies
1
Views
225

Feb 25, 2024

frabulator

D

  • Question

Excel adodb Operation must use an updateable query

  • drag1c
  • Nov 23, 2023
  • Excel Questions
Replies
2
Views
443

Nov 24, 2023

drag1c

D

A

  • Question

Set SQL variable with a cell value

  • Arsouille
  • Mar 17, 2023
  • Excel Questions
Replies
1
Views
373

Mar 18, 2023

rpaulson

K

  • Question

How to Use "GetPivotData" formula to retrieve data from a closed workbook

  • kizzie37
  • Feb 12, 2024
  • Excel Questions
Replies
3
Views
661

Feb 12, 2024

RoryA

Forum statistics

Threads
1,218,254
Messages
6,141,403
Members
450,356
Latest member
q11

Share this page

  • Forums
  • Question Forums
  • Excel Questions
Excel/SQL - Parameter in Variable (2024)

FAQs

How do I pass a parameter to a SQL query in Excel? ›

On the Data tab, in the Queries & Connections group, click Properties. In the Connection Properties dialog box, click the Definition tab, and then click Parameters. In the Parameters dialog box, in the Parameter name list, click the parameter that you want to change. Click Get the value from the following cell.

How do I set parameters in Excel query? ›

Create a parameter
  1. Select Data > Get Data > Other Sources > Launch Power Query Editor.
  2. In the Power Query Editor, select Home > Manage Parameters > New Parameters.
  3. In the Manage Parameter dialog box, select New.
  4. Set the following as needed: Name. ...
  5. To create the parameter, select OK.

How to write SQL query in Excel formula? ›

How to Use SQL Statements in MS Excel?
  1. Make sure you have installed pip for Python beforehand. If not, refer to this GeeksforGeeks link. ...
  2. Type in the SQL query to be executed in Excel. ...
  3. For executing the SQL query, type in =sql( in a new cell, where you need the retrieved data to be displayed.
Nov 1, 2023

How do you give parameters in SQL query? ›

Add a parameter to a union query
  1. Open the union query in SQL view.
  2. Add a WHERE clause that contains the fields you want to add parameters to. ...
  3. Type your parameter prompt into the where clause, for example, WHERE [StartDate] = [Enter the start date:]

How to pass variable value to SQL query? ›

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

How do I pass a parameter to a SQL file? ›

In order to set a dynamic parameter in SQL script, you must use the '?' symbol. The question mark will bind positionally to the parameter listed in either the Profile or Parameters list (Program Command, Decision, etc.) Note: the substitution occurs based on the order of the parameters, not based upon their names.

Is it possible to run a SQL query in Excel? ›

Open a SQL connection to an Excel file

Before running a SQL query, you have to open a connection with the Excel file you want to access. To establish the connection, create a new variable named %Excel_File_Path% and initialize it with the Excel file path.

How do I write SQL query results in Excel? ›

Step 2. Connect to the SQL Server and export SQL data to Excel
  1. Right-click on the database name and select Tasks - Export Data. An Import and Export Wizard will open;
  2. Configure the Data source first. ...
  3. Finally, press Test Connection and OK once finished.
Jan 30, 2023

Can you use a query function in Excel? ›

To open the query, from the worksheet select a cell in the data, and then select Query > Edit. For more information see Create, load, or edit a query in Excel (Power Query). In the Power Query Editor, select Home > Advanced Editor, which opens with the statement you created in Phase 2: Define the data source.

How do you pass parameters to a string query? ›

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

How to assign a value to a variable in SQL? ›

To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.

How do you write a parameter for a query? ›

The query string begins after the question mark (?) and can include one or more parameters. Each parameter is represented by a unique key-value pair or a set of two linked data items. An equals sign (=) separates each key and value. An ampersand (&) separates multiple parameters.

How to get data from Excel to SQL Server using query? ›

Import data directly into SQL Server from Excel files by using the Transact-SQL OPENROWSET or OPENDATASOURCE function. This usage is called a distributed query. In Azure SQL Database, you cannot import directly from Excel. You must first export the data to a text (CSV) file.

How do you enter a parameter query? ›

To create and run a parameter query:

Locate the field or fields where you want the variable criteria to appear, then select the Criteria: row. Type the phrase you want to appear in the prompt that will pop up each time you run your query. Make sure to enclose the phrase in brackets [ ].

Top Articles
Four Brothers | Rotten Tomatoes
Indianapolis | City Guide & Attractions
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
Tri-State Dog Racing Results
Navy Qrs Supervisor Answers
Trade Chart Dave Richard
Lincoln Financial Field Section 110
Free Stuff Craigslist Roanoke Va
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5777

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.