Why when I extract from my database it automatically adds new lines? - sql

For example, my database would have an entry that says "This is a title". In server management studio, it would present it as that one line. Whenever I try to export or copy and paste that result into excel, it turns into
This
Is
A
Title
in 4 different cells
Instead of
This is a title
in one cell
Why is this happening and do I fix it so I can just have the data in one cell when I export?

After connecting to the database using python and examining the data in text, it seems as though the data had the new line escape character,\n,'embedded in them. This is what resulted in excel displaying them as new lines.

Related

Question about adding column to data source and destination in SSIS package

I have a data source which is something like
select
patient_id
from patient_table
the destination is a CSV file.
Now I want to add patient_name to both the source and the destination.
I go to the source and I change the query to
select
patient_id,
patient_name
from patient_table
After I add this when I click on columns the patient_name column is not there.
The same thing happens for my destination. I have a flat file destination with the patient_id column so I add the patient_name column to the actual .csv file and that column is not reflected on the flat file connection manager.
The only way that I've been able to get these new columns to show up is to delete the data flow task, connection managers, sources and destinations and to create everything new from scratch.
Is there any other way to do this?
I just created a simple data flow with an OLE DB source and a flat file destination. After adding a second column to the OLE DB source I double clicked my flat file destination which opened the Flat File Destination Editor. Clicking UPDATE added the 2nd column to the flat file connection.
Are you using the latest tooling available to modify SSIS packages?
I don't have an SSDT installation handy right now, so I'll do the best I can without screenshots (and working from memory).
In the Source object, after you add the column to the text of your query, click on Columns, which you already know. Your new column doesn't show up in the list at the bottom yet, which you also know. Up in the top of that window, there's a grid representation of the result set from the query. Find your new column in that grid and check the box to tell the connector you want that column to enter the data flow.
Now go to the connection manager for the .csv file. Add the new column there.
Once it's in the connection manager, now you should be able to map it in the destination object.
There's a possibility that you'll have to click on the arrow or arrows in your data flow task and map the new column in those, too, but it doesn't always happen like that. I haven't taken the time to figure out why that's necessary sometimes and not others, but you'll know right away because the arrows will have red Xs on them.
And that should get you there.

"No column information was returned" error when selecting from empty Excel sheet

I have an Excel workbook that contains a series of sheets for import into a database. For most of the sheets I'm using a query to get at the data along the lines of:
SELECT [F1], [F2] FROM [Sheet Name$A2:B10000]
In my development I had a workbook with some test data for each sheet. Now someone else is using the package to import a copy of the workbook, but one of the sheets is blank because they don't want any data loaded for that particular table. Unfortunately, when the package tries to run that portion of the import it errors out with a VS_NEEDSNEWMETADATA error.
This can't be a unique problem, but I haven't been able to find any solutions online. Any suggestions on how to get around it?
In a similar case, I would use a Script Task to check if the Excel sheet contains data, if so, the Data Flow Task is executed (using precedence constraints).
Also, make sure to set the Data Flow Task Delay Validation property to True.
Useful links:
Check if Excel file is empty in C#
How to check if Excel-workbook is empty?
Working with Precedence Constraints in SQL Server Integration Services

How to connect an Excel file to an Access Database

I have created a query in an Access database and exported the query result into an Excel file. Now, I want to connect that Excel file to the Access database (or to that query) so that whenever some fields are updated in database, those changes would be automatically updated in the exported Excel file (report). What would be the best way to do it?
Thanks
You can create a link between Excel and an Access table or query by going to the Data tab on Excel and clicking the "From Access" icon.
Alternatively if there is a trigger inside access that you can use to update data (or if you don't need real time updates just hourly or daily ones), you could have access programmatically re-export.
If you want to work with data in Access, but still maintain the data in Excel, you need to link to the data rather than import it. Follow these steps:
Create a blank database or open an existing file in Access.
Select File, Get External Data, Link Tables.
Select Microsoft Excel as the file type.
Select a worksheet or named range to import, and then
click Next. You can import only one worksheet or named range at a
time, and each one will become an Access table.
In the next dialog box, select or deselect the check box First Row
Contains Column Headings, depending on whether your worksheet has
headings. Then click Next.
Enter a name for the table (or accept the default name that Access
suggests), click Finish, and click OK.
Now you have an Access table that looks almost exactly like the imported table. The advantage is that it maintains a live link to the Excel worksheet and can be edited in either application.

Importing Excel Data With Images into SQL Server

I wish to import data from an Excel File into SQL Server 2012. The Excel file has one column which contains an image in each row.
So when I use the SQL Server Import & Export Wizard, it imports everything fine except the pictures.
Thank you.
It's not possible to import images from an Excel file into SQL Server using the Import & Export Wizard.
This is partly due to the fact that images are actually not located inside the individual spreadsheet cells. In Excel, images are stored as free objects (granted, you can anchor an image to a cell, but this is pure layout - not data).
To be able to do what you want, you'd probably have to create some custom VBA code, to loop through each image in your spreadsheet, serialize the image into binary format and then write it to your SQL database table, along with the name of the image object. You will probably have to do this on an image-by-image basis, inserting one record at a time, so hopefully, your Excel spreadsheet does not contain too many images.
Binding each image record to the cell record will be a problem on its own, but hopefully, the images in your Excel file have been named in a way, that enables you to match them with their corresponding data record.

generated excel from SSIS but getting quote in every column?

I have generated and excel from SSIS package successfully.
But every column is having extra ' (quote) mark why is it so?
My source sql table is like below
Name price address
ashu 123 pune
jkl 34 UK
In my sql table i took all column as varchar(50) datatype.
In Excel Manager when it is going to create table
Excel Destination took all column as same varchar(50) datatype.
And in Data Flow I have used Data Conversion transformation to prevent unicode conversion error.
Please advice where i need to change to get the clear columns in excel file.
You could create a template Excel file in which you have specified all the column types (change to Text from General) and headers you will need. Store it in a /Template directory and have copy it over to where you will need it from within the SSIS package.
In your SSIS package:
Use Script Component to copy Excel Template file into directory of choice.
Programatically change its name and store the whole filepath in a variable that will be used in your corresponding Data Flow Task.
Use Expression Builder for your Excel Connection Manager. Set the ExcelFilePath to be retrieved from your variable.
the single quote or apostrophe is a way of entering any data (in Excel) and ensure it is treated as text so numbers with leading zeros or fractions are not interpreted by Excel as numeric or dates.
a NJ zip code for instance 07456 would be interpreted as 7456 but by entering it as '07456 it keeps its leading zero (please note that numbers in your example are left aligned, like text is)
I guess SSIS is adding the quotes because your data is of VARCHAR type
First, define the field types for your excel destination in SSIS, any non-text fields will format properly without the '. Then, add a derived column transformation between your source and destination, and use a replace statement for any text columns.
Should be:
(REPLACE(Column1, "'","")
This caused me major problems! So I completed the following:
You can change the excel version to 'Microsoft Excel 4.0' within the excel connection manager in your SSIS package.
Then within excel follow Options > Trust Center > Trust Center Settings > File Block Settings > Untick the 'Open' checkbox for 'Excel 4 workbooks' and 'sheets'.
It is a particular problem when using the Excel destination, at least with older versions of SSIS anyway. To answer the why question, there is this in the Microsoft documentation:
The following behaviors of the Jet provider that is included with the Excel driver can lead to unexpected results when saving data to an Excel destination.
Saving text data. When the Excel driver saves text data values to an Excel destination, the driver precedes the text in each cell with the single quote character (') to ensure that the saved values will be interpreted as text values. If you have or develop other applications that read or process the saved data, you may need to include special handling for the single quote character that precedes each text value.
Taken from https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms137643(v=sql.105)