Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I split a sentence in SQL Server 2005
    primarykey
    data
    text
    <p>I have a column named Address in a table. I have values such as</p> <pre><code>12-15 Hardley Street 2A-C Hardley Street A-2c Hardley Street A-B Hardley Street </code></pre> <p>I am required to keep the hyphen(-) intact in the first three rows.</p> <p>i.e. If there is a number in the word that contains the hyphen(-), I should keep it, else I should replace with a space.</p> <p>So the column should be replaced as below</p> <pre><code>12-15 Hardley Street 2A-C Hardley Street A-2c Hardley Street A B Hardley Street </code></pre> <p>My small brain cannot comprehend this. Any help pls.</p> <p>p.s: I would also like to add that it is not necessary that the first word will contain the hyphen.</p> <p>The data can be as </p> <pre><code>Hardley Street 2A-C </code></pre> <p>or</p> <pre><code>2A-C </code></pre> <p>Thanks one and all for your reply and comments.</p> <p>There has been one more pre-requisite, I am supposed to do in an update query.</p> <pre><code>declare @data nvarchar(200) set @data='12-12 ORANGE-RED' select @data= case left(@data,charindex(' ',@data)) when '' then CASE PATINDEX ('%[0-9]%',@data) when 0 then replace(@data,'-',' ') else @data END else CASE PATINDEX ('%[0-9]%',left(@data,charindex(' ',@data))) when 0 then replace(left(@data,charindex(' ',@data)),'-',' ')+'-- LEFT MOST WORD REPLACE' else CASE charindex (' ',substring(@data,charindex(' ',@data)+1,len(@data))) WHEN 0 THEN CASE PATINDEX ('%[0-9]%',substring(@data,charindex(' ',@data)+1,len(@data))) when 0 then left(@data,charindex(' ',@data))+ replace(substring(@data,charindex(' ',@data)+1,len(@data)),'-',' ') +'--RIGHT MOST REPLACE' else @data + '--struggling here' END END END end where @data like '%-%' select @data </code></pre> <p>I have tried the above I will be updating the table as</p> <pre><code>update tblname set @columnName= --lines of coding where @columnName like '%-%' </code></pre> <p>I am not able to solve this, and to add to my woes, the data will be inconsistent.</p> <p>I assumed the data to be of two words, such as</p> <pre><code>2A-C Hardley Street A-2c Hardley Street A B Hardley Street </code></pre> <p>But the client said that he will be having data as</p> <pre><code> Hardley Street 22-23 BO'NESS A-2c Hardley Street Hardley Street 12B Stratford-upon-avon </code></pre> <p>I cannot create a function, the reason being we are going to update a table and format it. If I create a function that I should call it for every other row, which is time consuming.</p> <p>Kindly do not suggest SSIS as that has already been ruled out.</p> <p>Any ideas will be very helpful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload