|
I have a tab delmited text file So of the records span over 2 lines. So when I import into Management studio It puts a bunch of SPACES in between. I am trying to create a function that that will eliminate the MULTIPLE SPACES and just provide a single space:' Below IS an example Notice how "This is a test" is spread across 2 lines: I need a function that makes it read 'This is a test' in the database. How it imports I would need to use the following select statement to get a result: Any suggestions on a function that can accomplish this?
(comments are locked)
|
|
SSMS doesn't do word wrap, so if it's showing like that then it probably contains a line-feed. You can replace them like this:
(comments are locked)
|
|
Kev, as usual, is completely right. But I think there are a couple of things I could add. For one, sometimes you will just find CR or LF as the end of line character instead of both together. This would be somewhat unusual in Windows, but it shows up occassionally if you are working in a multi-operating system environment or using multi-operating system tools like SciTE. If you want to cover this possibility you could use: Also, depending on the workflow it may be worth making adjustments like this prior to importing the file into SQL. SQL's text manipulation tools are sufficient for many things, but notoriously ineloquent when compared to more general purpose langauges like C# or Python. +1 Tim: I'm sure Mrs Kev would disagree that 'as usual' I'm right :) !!
Jun 14 '12 at 08:19 PM
Kev Riley ♦♦
Heh. Well, I'm confident you are usually right about SQL at least. In anything else, I'll defer to her ;)
Jun 18 '12 at 07:07 PM
TimothyAWiseman
(comments are locked)
|
|
If the data in the field term contains carriage-return and line-feed characters, then you can remove them using REPLACE. For example, after importing your data you could run this command:
(comments are locked)
|
|
How you are importing the tab delimited file? If you are pasting it into SSMS and than processing it in some way, than that's not an optimal way. If you use BCP or SSIS (Import/Export wizard which will create a SSIS for you), then you should not have any problems with importing that file and the text should not be spread in multiple lines in the destination table.
(comments are locked)
|
|
Try it..... CREATE FUNCTION [dbo].[UFN_RemoveMltSpace](@pi_StrData VARCHAR(MAX)) RETURNS VARCHAR(MAX) AS BEGIN DECLARE @li_Pos INT ,@li_ChrLastPos INT ,@li_ChrPos INT ,@li_FNOutput VARCHAR(MAX)='' DECLARE @li_tblRmvSpace TABLE(ID INT,ColVal VARCHAR(MAX)) END GO SELECT dbo.[UFN_RemoveMltSpace]('this is Gosh no. There's just no need for a While Loop here. Use nested REPLACEs instead.
Jun 16 '12 at 02:15 AM
Jeff Moden
(comments are locked)
|

