SQL Server 2014 export CSV file issues - sql

I have searched for an answer on this, but have come up short. When exporting a table from SSMS, from time to time some of the larger files will have issues where something like an address will trigger a CR/LF and spread a record like an address across multiple rows in the exported CSV file. If I copy an paste the record directly from SSMS and paste it into another program such as word, it will do the same thing. I cant exactly put my thumb down on what is happening here. Other records will export correctly, and then all the sudden one of the records will come up like below...
Looks something like this:
1|"Apartment Katha
2|Flat No 9999 Garia Place
3|West Bengal"
But I need it like this
1|"Apartment Katha Flat No 9999 Garia Place West Bengal"
I use Unicode and " as the text qualifier.

CR and LF in SQL server are Char(10) and Char(13) Try to take out these characters from your records, and see if it works:
SELECT REPLACE(REPLACE(#str, CHAR(13), ' '), CHAR(10), ' ')

Related

Copy a file and rename based upon SQL query using PowerShell?

I need to move around 12000 documents from one directory to another using PowerShell, however, I also need to rename each document based upon a SQL query which strips out any spaces, removes the file extension etc.
I've written the SQL for it (as shown below), but I'm not sure how/if it's possible to do the above using PS?
'Author' + CAST(AuthorId as varchar) + '_' + REPLACE(REVERSE(SUBSTRING(REVERSE(originalFilename), CHARINDEX('.', REVERSE(originalFilename)) + 1, 999)), ' ','')
Thanks for any help

Removing special characters in sql

I am new to SQL and just know basic insert, update, and delete syntax.
I have an excel file, that I imported into the SQL server, but somehow, it brought in weird symbols and characters.
When I checked the excel file ,cleared all formatting, and re-uploaded, it would still show up, not sure how to clean it up.
Is there an easy replace syntax that you can suggest for me to use to do a global cleanup?
Sample values inside the columns are:
Lisa Hettinger  Cherry Creek Prop
Lisa J Hernandez
I would need to remove the weird ┬ and á characters.
If all you have are the " " characters, you can try using the REPLACE command like this:
SELECT REPLACE(N'Lisa J Hernandez', N' ', N' ')
I suspect the source code page has " " as its space character.
Update: To update the values in the entire column, you can use
UPDATE [MyTableName] SET [MyColName] = REPLACE([MyColName], N' ', N' ');

sql server conditional replace of csv data

I have a csv file that I am trying to import using BULK INSERT. The problem is that there is a field in the file that will be quoted (with double quotes) if a comma exists within the text (not quoted if no comma exists). The existence of the extra comma is causing SQL Server to throw errors because of an incorrect number of columns during the insert.
Here is a sample data set:
928 Riata Dr,Magnolia,TX,77354,4/15/2014
22 Roberts Ave.,McKinney,TX,75069,4/15/2014
"5531 Trinity Place, #22",San Antonio,TX,78212,4/15/2014
As you can see, the third row contains a comma within the address field, thus the address field is quoted. Since the BULK INSERT command is throwing errors because of this, I'm assuming I will need to scrub the file contents before attempting to load it.
Unless someone has a better solution
To scrub the file contents I will need to open the file (with SQL), read in the contents, and do a conditional replacement of the internal comma (found within the quotes). Since that comma doesn't really need to exist, I can just replace it with '' (blank).
Then, I can handle the quotes separately after the data gets imported with an update statement to replace any other characters I don't want.
I think the logic is sound, the problem is the syntax. I can't seem to find any syntax related to REGEX in SQL Server (Booo Microsoft). Which means I would need some other way to determine if the comma appears within quotes, and replace it if so.
Any thoughts, Suggestions, Code, etc.?
Thanks in advance.
This sounds too simple on the face of it, but if you can just replace the commas, can you open the csv in, say, Excel or OpenOffice Calc, and then do a find replace (commas with nothing)? I just tried with a csv of mine and it worked fine. The csv remains properly delimited.
Maybe I am missing something that prevents this, such as Excel opening this with extra cells due to the comma, in which case my answer is stupid. But it would make more sense to handle this in a spreadsheet app rather than after opening with SQL.
You may have to try delimiting with something other than commas, such as tabs or etc. I've had to do this with SQL imports before. In many cases you can save as a tab delimited txt file and upload to SQL.
Note that using Excel for this type of thing can be its own problem. For help with Excel and tab delimited SQL imports, see my answer here.

BCP to CSV file with commas in the data

I have a BCP process that is calling a stored procedure. Typically I have been executing this stored procedure and copying the data to an Excel sheet that I specifed all columns as text and saved this as a CSV.
I need to automate this work and have been playing with the BCP command but so far have an issues. The data I have has commas in it which shifts data to the left. Is this something I can overcome with a format file or something of the sort?
I would rather not quote these in the output of the proc itself.
The BCP command has the -t switch which specifies the field terminator. In your case, it is a comma or CHR(44).
http://technet.microsoft.com/en-us/library/ms162802.aspx
To prevent MS Excel from having issues opening the file, enclose all text fields in the QUERY with double quotes "" or CHR(34).
Here is a sample query from Adventure Works.
-- Enclose text w/ possible commas in quotes
select
char(34) + AddressLine1 + char(34) as fmt_address_line1,
char(34) + City + char(34) as fmt_city,
PostalCode as postal_code
from
[AdventureWorks2012].[Person].[Address]
This should allow you to open the file in MS Excel w/o any issues.

export query results into CSV file issue in SQL Server

I am using SQL Server 2008 Enterprise. I want to export the query result into csv file from SQL Server Management Studio. The issue is, the default csv file is comma (',') separated for each result column in each exported row, and I want to make it '\t' separated since comma exists in some of the result column values.
Any quick solutions?
thanks in advance,
George
If you really want the \t delimiter write a query like this
select
Cast(PersonCode as varchar(50)) + '\t'
+ Cast(PersonReference as varchar(50))
from People
The casts are just incase you aren't working with varchar types, but you don't need them if you are. Run this query with results to text and just paste the result into notepad or similar.
When you click "Save as" and you are prompted to enter a filename, drop down the box below the filename and select "Text (Tab delimited)"
It saves only the 1st record as Tab delimited.
I can't figur eout, how it will select all rows from query analyzer results section.