SQL pulling into Excel as text - sql

The data I'm pulling in from a SQL server is being displayed correctly as a date or number, but the data seems to be stored as text. For example, if I pulled in data with the number value 1, the cell would display "1" (no quotes) in it but Excel would not recognize it as a number - I can't perform any math to it and it won't be recognized as a number. I can fix that by selecting the cell and doing F2+Enter.
To clarify, the SQL data is not formatted in Excel as text. Changing formats does not solve my issue. The actual data in the cells isn't being recognized by Excel as numbers or dates, no matter what format I make the cells. I have to manually select and F2+Enter.
select distinct vl.Property As 'Property'
, vl.Property As 'Property number'
, vl.LeaseID as 'Lease ID'
, vl.priorleaseid as 'Prior lease ID'
from realpage_uds.dbo.property p
inner join realpage_uds.dbo.vwsa_lease vl
on p.propertyid = vl.propertyid
where p.name like '%Name%'
vl.LeaseID is a good example. It is data type "T-IDNumeric", system type "numeric" yet I can't select a leaseID and perform any arithmetic to it in Excel.
I'm pulling in the SQL data to Excel in the following way:
Go to "Data" tab, click "From Other Sources" under "Get External
Data"
Choose "From SQL server", enter server name to connect
Connect to a database and choose a random table in the database
Click finish, and choose to pull in the data as a table or pivot table. I've used both and they both have this issue.
Finally, update the SQL query under connection properties, and change the query to my desired query that I have typed up. Then I choose "SQL" and hit OK.
Has anyone else experienced this? Is there a way I can make the SQL data pull in as a number and date, and not have to manually select the cell and F2+Enter to get Excel to recognize the data?
Alternatively, if there is no way to change the way the SQL data pulls in, is there a macro I could implement to change all text data in a spreadsheet to number or date values?
Any help is greatly appreciated!

Wouldn't FORMAT(<<your field>>,'#') AS <<your alias>> solve your problem? Try using 'd' for date. I don't have much experience pulling data from SQL Server, but in Access that's how I'd try to work around this issue.

I had a similar problem with Dates. Excel only recognized them as strings. To solve the issue I created a view that converted the Date to an Int and then set up excel to recognize the Int column as dates.
To convert dates to Excel date integers you can use this function:
DATEDIFF(day,'1899-12-30',#yourDate)
I do not have experience with numeric types, but you may try and cast it to a decimal(18,4) which works for me in Excel.
The above works for me using Azure SQL.

Related

Excel cell Value as SQL query where statement

I am very new to SQL, I want to import data from SQL Server to Excel using this query
SELECT
Model, Factory, TargetTime, TotalEvalMins
FROM
AMSView
WHERE
WeekNumber = 45 AND WeekYear = 2021
I want to change the week number & year dynamically by taking user input from a cell.
Can anyone please suggest how to change the query?
Let's say the user values week & year in worksheet sample in A1,A2 , how can I write that query?
Since the amount of data is huge I must apply where while querying the data instead of applying filters in Excel.
Sorry for my bad English
Name each of your cells that you will use as parameters. This page describes the process.
Name a cell
1. Select a cell.
2. In the Name Box, type a name.
3. Press Enter.
For each cell containing a parameter for your query:
Select the cell
Use Data>Get & Transform Data>From Table/Range. This will open the PowerQuery Editor. You will see something like this:
Right-click the cell in row 1 in the grid in the Power Query Editor and select 'Drill Down'. This converts the query on your parameter cell to a named value which can be used in other queries. It looks like this:
Now in Excel, use Data>Get Data and create your query from the database. I created a sample table in a local SQL Server database called AMSView, then connected to it with the query text in your post. When finishing the query connection, select 'Transform' so the query opens in the PowerQuery Editor.
Now, use Home>Advanced Editor and edit as follows by replacing the fixed values in the WHERE clause with concatenated names of your parameter cells, converted to text. For brevity, I have only used one parameter. If you've used capital letters in your cell names, remember, the M language is case-sensitive, so the concatenated parameter name must have identical casing to the named value.
let
Source = Sql.Database("localhost", "StackOverflowTest", [Query="SELECT #(lf) Model, Factory, TargetTime, TotalEvalMins #(lf)FROM #(lf) AMSView #(lf)WHERE #(lf) WeekNumber = " & Number.ToText(week_number)])
in
Source
Once your query is finished, use Home>Close & Load to load the results to the workbook. Now, when your parameter cells change, you need only refresh the query (right-click, refresh) and the data will be filtered as required.

SSIS importing percentage column from Excel displaying as NULL in database

I have an ETL process set up to take data from an Excel spreadsheet and store it in a database using SSIS. However, one of the columns in the the Excel file is formatted as a percent, and it will sometimes erroneously be stored as a NULL value in the database, as if there was some sort of translation error.
Pictured is the exact format being used for the column in Excel.
Interestingly, these percent values do load properly on some days, but for some reason one particular Excel sheet I was given as an example of this issue will not load any of them at all when put through the SSIS processor.
In Excel, these values will show up like "50.00%", and when the SSIS processor is able to translate them properly it will display as the decimal equivalent in the database, "0.5", which is what I want instead of the NULL values. The data type I am using in SSIS for this is Unicode string [DT_WSTR], and it is saved as an NVARCHAR in the database.
Any insight as to why these values will sometimes not display/translate as intended? I have tried messing around with the data types in SSIS/SQL Server, but it has either resulted in no change or error. When I put test values in the Excel sheet, such as "test" to see if it is importing anything at all from this column, it does seem to work (just not for the percent numbers that I need).
The issue was caused by the "mixed data types" that were present in the first few rows of my data (the "mixed" part being blank fields), which would explain why some sheets would work and others wouldn't.
https://stackoverflow.com/a/542573/11815822
Setting the connection string to accommodate for this fixed the issue.

Use date in Excel cell in SQL Query

How do I connect the Excel cell with the date to the SQL Query? I use Power Query. The Database is SQL Server.Please Help me.
Example of Query:
Select Account, Date
From Accountdate
Where Date = "Value in Excel cell"
Type a value into an Excel cell
Keep that cell selected. From the Power Query ribbon choose From Table.
Uncheck My Table Has Headers before clicking OK.
In the Power Query Query Editor, right click on that single value which you entered before and choose Drill Down
Click Apply & Close
In the Power Query, choose From SQL
Connect to your data source (don't worry about using the parameter yet.)
Once you're in the Query Editor with the correct SQL table being shown, choose the column you want to filter by your parameter. Go ahead and do a filter using the filter dropdown. Now change the formula bar and replace the number that you filtered by with the name of the query that you created in Step 5. That query by default will be called Table1 so your query in this step might look something like this: = Table.SelectRows(dbo_MySqlTable, each [ID] > Table1)
You will probably get a prompt asking you to classify the permissions to apply to each data source to make sure there are no security leaks. Once you've done that, click OK.
Click Apply & Close.

converting date to different format

Ok this one has been asked a million times but I can't find any answer to work for me. I have a column named 'Invoice Date' that is data type 'date'. I have an excel pivot chart that uses my query to update itself every 60 mins. Everytime this updates I lose my cell formatting and what's weirder, is that when I format the cells in excel to ie: Mar-14 then nothing happens, until I double click in each cell, then it displays correctly. This is vital because I need to group by months, and this is the only way I have been able to do this.
I have another date field in the query that is 'smalldatetime' and when formatting the cells in excel I just select the column, and format and all cells change without me double clicking. So I'm thinking maybe if I can convert the data in my SQL query to display the date similar to 'smalldatetime' that my pivot will work properly.
I have tried the checking of boxes in excel that CLAIM to preserve cell formatting... this obviously doesn't work for me.
Any help is greatly appreciated
You could try the following SQL in your SELECT statement:
CONVERT(VARCHAR(11),YourDateColumn,106)
the end result will be "02 Apr 2014".
You should be able to do so for smalldatetime type of column as well.

Edit Parameter Field Crystal reports with NOT value?

In Crystal Reports, I want to add a WHERE field <> date to filter out dates that have a NULL value from my database in my report.
I'm using a legacy FoxPro database on the backend which generates an SQL statement from my report, but doesn't appear to have anyway of adding a WHERE clause to the generated statement.
When accessing the FoxPro backend directly, dates with psudo-NULL values have a date of 1899-12-30, but when they are pulled from FoxPro through Crystal they appear as 12/30/99 (which is maybe the same date just displayed in MM/DD/YY format).
I noticed that the report had an existing Parameter Field that prompts the user to filter out the original query down to a specific date range. I tried to add my own in addition to the Parameter Field, but discovered that what I needed with my WHERE field <> date is not an available option since there are only 3 types of Field Parameters mainly:
Discrete
Accept single and discrete values.
Ranged
Accept a lower and upper value in order to select everything in this range.
Discrete and Ranged
A combination of the two above
None of these appear able to filter the results of the query using a WHERE NOT type of clause, is there some other way to do this?
Add this to your record-selection formula:
// remove actual nulls
AND Not(Isnull({table.date_field}))
// remove old dates
AND {table.field} <> date(1899,12,30)
// remove dates not in select parameter value
AND {table.field} IN {#date_parameter}
All I really needed to do was add some criteria to the WHERE clause of the SQL statement, simple enough in an SQL client, but when you're doing this in Crystal Reports v10 it's a bit difficult to find, unless you know what you are looking for...
So what I needed to do was:
Select the field to filter by in the report (in the Details section)
Click the Select Expert button on the Experts toolbar.
In the Select Expert dialog the name of your field should appear in a tab.
Below you can select the WHERE criteria used to filter the records.