Columns misaligned when exporting SQL Studio query to .CSV - sql

I am trying to export results of query in SSMS to CSV file. I have a number of different tables, and the Export Wizard has thrown a bunch of mapping errors which I can't figure out, so I am trying to use Save As method by right-clicking the query result window. When I open up the CSV file, a number of rows are incomplete, while others are misaligned, and seemed to have shifted one column. All column headers exported correctly. I cannot figure out how to export the results correctly.
Query is as follows:
SELECT
contract.Description,
contract_type.type,
Destination_City.Name_,
contract.Tour_Summary,
employee.First_Name,
employee.Last_name,
Currency_.Currency_Name,
contract_Items.twin,
contract.Upfront_Room_Comp,
contract.Room_Comp_Number,
contract.Room_Comp,
contract.Start_Date,
contract.End_Date,
contract.Payment_Terms
FROM
Contract,
Contract_items,
Contract_type,
Employee,
Currency_,
Destination_City
WHERE
Contract.Contract_ID=Contract_Items.Contract_ID
and contract_items.Contract_Type_Id=contract_type.Contract_Type_Id
and employee.Employee_Id=Contract.Contracted_By_Employee_Id
and Currency_.Currency_Id=contract.currency_id
and Destination_City.Destination_City_Id=Contract_Items.Destination_City_Id
and contract.Contracted_Date > '01/01/2014'
and Contract_type.type = 'Hotel'
and contract.signed = 1
ORDER BY
employee.First_Name

Try to get result in grid (default action), select whole table and copy to clipboard.
Open new Excel sheet and paste result. If you have a problem with alignment, format all columns as text and then paste.

Related

Export values to specific cell in an excel file (using PDI)

I have in table input result as below:
I would like pass this result to excel file in specific row.
For example number_record I want to write to column J5, amount_ma to column K2 and so on. Can something like this be done in pentaho?
I have prepare a solution for you HERE. Using this solution, you can specifically tell which field you want to insert in which position in Excel. Like in my solution i told to insert field [NUMBER_RECORD to C5, AMOUNT_MA to D7,AMOUNT_WD to B5] Please run transformation "getData.ktr".
Add a new sheet to this Excel file, and output a line with the result to the new sheet. And on the old sheet in the cells you need, add formulas with a link to the first row in the new sheet

Excel 2016 - Using Get Data to import CSV and format to show in a different column in the worksheet

Currently I have a CSV file that I am trying to import into excel using Get Data, and format to show each value separated in a different column within the excel workbook. I have the CSV with the values separated in the file in such a way as seen below:
VBU|"VBU Name"|"PO Number"|"Customer Order Number"|"Line Number"|"Insert Date/Time"|"Ack Date"|"Confirm Date"|"Company Item Number"|"Vendor Model Number"|"Quantity Ordered"|"Line Status"|"Back Order Reason"|"Cancel Reason"|"Origin State"|"Destination State"|"Tracking Number"|"Expected Delivery Date"|"Expected Ship Date"|"Actual Ship Date"
I am having a hard time separating each value into it's separate column in the excel workbook after importing it using the Get Data - From Text/CSV.
Is there a step I am missing like a custom delimiter for instance, in order to have this imported and formatted in such a way that each column shows the individual value (column 1 = VBU, column 2 = VBU Name, Column 3 = PO Number), etc.? This CSV file, all these values are crammed into the first column when I open the CSV in excel 2016.
Any help would be appreciated.
Nevermind, it was an anomally in one file. Using Get Data -> From File -> From Text/CSV and setting delimiter as | did the trick and I was able to transform it and separate the values into different columns

SQL query to link data in 2 Excel sheets

I want to link 2 excel tables on different excel sheets. I do this for years without problems. Both excel sheets contain a table with the same reference number so i can link them and job done.
The Excel sheet i got this time from the vendor has little different reference numbers. They all have a character in front of the reference numbers.
Sheet 1: Reference number: 12455630098
Sheet 2: Reference number: F12455630098
The problem here is that the data is a little different now, so the link can't be made with the code i used previously.
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber = `tab2$`.PARTNO
I tried to experiment with the wildcard character to get the job done, but without succes.
This is my SQL code so far.
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber LIKE '%' + `tab2$`.PARTNO
Whats wrong here?
Why not
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber = substring(`tab2$`.PARTNO,2,11)
The easiest way is to run a replace command.
Press CTRL+H and when the replace dialog opens search for letter F and replace it with nothing.
Ok, in your previous question you did not made that specific, didn't use SQL in MS producst for a long time, but if I'm right than you there isn't substring function in MS office, instead you have LEFT RIGHT and MID so you should try something like:
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber = MID(`tab2$`.PARTNO,2,LEN(`tab2$`.PARTNO)-1)
Not 100% sure about the keywords...

Export specific data from a selected cell's row into a CSV file

I have been searching for a solution to this, but it seems like I cannot find it.
So basically, I want to select e.g. H2 and then run the macro.
Then it should copy/paste some specific cells into a new CSV file, e.g. O2 and F2. I also want a fixed value that should always be there, called "No".
The first row of the CSV file should be "UTF-8". The next (2nd row in the CSV) should be some headers that is fixed, just as the UTF-8.
Could a solution be to copy all the relevant data into another sheet with the proper format, and then just export that sheet as a CSV?
Illustration:
"UTF-8"
"Name","ID","Email","Customer"
"H2","O2","F2","No"
Solution ended up being exporting the correct data into another sheet with the proper setup, using the following.
Sheets("Sheet1").Range("XX:XX").Copy Destination:=Sheets("Sheet2").Range("XX")

Convert xls File to csv, but extra rows added?

So, I am trying to convert some xls files to a csv, and everything works great, except for one part. The SaveAs function in the Excel interop seems to export all of the rows (including blank ones). I can see these rows when I look at the file using Notepad. (All of the rows I expect, 15 rows with two single quotes, then the rest are just blank). I then have a stored procedure that takes this csv and imports to the desired table (this works on spreadsheets that have been manually converted to csv (e.g. open, File--> Saves As, etc.)
Here is the line of code I am using for my SavesAs in my code. I have tried xlCSV, xlCSVWindows, and xlCSVDOS as my file format, but they all do the same thing.
wb.SaveAs(aFiles(i).Replace(".xls", "B.csv"), Excel.XlFileFormat.xlCSVMSDOS, , , , False) 'saves a copy of the spreadsheet as a csv
So, is there some additional step/setting I need to do to not get the extraneuos rows to show up in the csv?
Note that if I open this newly created csv, and then click Save As, and choose csv, my procedure likes it again.
When you create a CSV from a Workbook, the CSV is generated based upon your UsedRange. Since the UsedRange can be expanded simply by having formatting applied to a cell (without any contents) this is why you are getting blank rows. (You can also get blank columns due to this issue.)
When you open the generated CSV all of those no-content cells no longer contribute to the UsedRange due to having no content or formatting (since only values are saved in CSVs).
You can correct this issue by updating your used range before the save. Here's a brief sub I wrote in VBA that would do the trick. This code would make you lose all formatting, but I figured that wasn't important since you're saving to a CSV anyway. I'll leave the conversion to VB.Net up to you.
Sub CorrectUsedRange()
Dim values
Dim usedRangeAddress As String
Dim r As Range
'Get UsedRange Address prior to deleting Range
usedRangeAddress = ActiveSheet.UsedRange.Address
'Store values of cells to array.
values = ActiveSheet.UsedRange
'Delete all cells in the sheet
ActiveSheet.Cells.Delete
'Restore values to their initial locations
Range(usedRangeAddress) = values
End Sub
Tested your code with VBA and Excel2007 - works nice.
However, I could replicate it somewhat, by formatting an empty cell below my data-cells to bold. Then I would get empty single quotes in the csv. BUT this was also the case, when I used SaveAs.
So, my suggestion would be to clear all non-data cells, then to save your file. This way you can at least exclude this point of error.
I'm afraid that may not be enough. It seems there's an Excel bug that makes even deleting the non-data cells insufficient to prevent them from being written out as empty cells when saving as csv.
http://answers.microsoft.com/en-us/office/forum/office_2010-excel/excel-bug-save-as-csv-saves-previously-deleted/2da9a8b4-50c2-49fd-a998-6b342694681e
Another way, without a script. Hit Ctrl+End . If that ends up in a row AFTER your real data, then select the rows from the first one until at least the row this ends up on, right click, and "Clear Contents".