| Author |
Topic |
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 13:25:50
|
| Hello All, I have this in my tableCol1 Col2A12346-123123 NULLAD12319-12323 NullAPR12365-1232 NULLI needd to get the numbers after the alphabets and before hyphen and update the col2 with those numbersso I want thisCol1 Col2A12346-123123 12346AD12319-12323 12319APR12365-1232 12365some of the col1 are empty or null or of less length.Thanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 13:38:38
|
| [code]UPDATE tableSET Col2 = STUFF(LEFT(Col1,CASE WHEN CHARINDEX('-',Col1)>0 THEN CHARINDEX('-',Col1) ELSE LEN(Col1)+1 END -1),1,PATINDEX('%[0-9]%',Col1)-1,'')[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 13:48:32
|
| I am getting an errorString or binary data might be truncated.the table has lot of bad data |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 13:51:21
|
| does Col2 have enough length to hold the data? whats the data type of Col2?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 13:58:54
|
| nvarchar(5) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 14:01:30
|
I'm sure you've some values in col1 where initial numeric part is over 5 characters.what does below return?SELECT COUNT(*) FROM tableWHERE LEN(STUFF(LEFT(Col1,CASE WHEN CHARINDEX('-',Col1)>0 THEN CHARINDEX('-',Col1) ELSE LEN(Col1)+1 END -1),1,PATINDEX('%[0-9]%',Col1)-1,''))>5------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 14:05:04
|
| i changed the col2 to integer and now i am getting converion failedand the value is13867ER021747is it possible that I only consider the values that has hypen in it. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 14:07:55
|
| I think your data is not following a consistent format so unless you determine how all data can come you cant have single logic to handle them all!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 14:10:32
|
| the query you gave me returned 3107 records.I just need the query that has consistent length, I mean the right data otheriwse I want to return null.Is it possible to do that so for 13867ER021747, I want the col2 to be null rather than parsing it correctly. |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 14:15:44
|
| is it possible to ignore those 3107 records |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 14:29:25
|
| [code]UPDATE tableSET Col2 = STUFF(LEFT(Col1,CASE WHEN CHARINDEX('-',Col1)>0 THEN CHARINDEX('-',Col1) ELSE LEN(Col1)+1 END -1),1,PATINDEX('%[0-9]%',Col1)-1,'')WHERE LEN(STUFF(LEFT(Col1,CASE WHEN CHARINDEX('-',Col1)>0 THEN CHARINDEX('-',Col1) ELSE LEN(Col1)+1 END -1),1,PATINDEX('%[0-9]%',Col1)-1,''))<=5[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 14:38:20
|
| Ok, I talked to the DBA. I just need to look for those records that has hyphen in it and I just need 5 characters before hyphen, those characters can be alphanumeric.so for this case123ABC-34234I need23ABCany help will be appreacited. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 14:56:04
|
then this is enoughUPDATE tableSET Col2 =RIGHT(LEFT(Col1,CASE WHEN CHARINDEX('-',Col1)>0 THEN CHARINDEX('-',Col1) ELSE LEN(Col1)+1 END -1),5)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 15:27:05
|
| I just want NULL in col2 if there is no hyphen in col1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 15:30:10
|
You really want spoonfed results dont you? I thought you could derive it for yourself!UPDATE tableSET Col2 =CASE WHEN CHARINDEX('-',Col1)>0 THEN RIGHT(LEFT(Col1,CHARINDEX('-',Col1)-1),5) ELSE NULL END------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2012-02-14 : 15:49:46
|
| Yes, I could have derived myself. I actually posted the question and then tried doing myself. I guess I wasn't confident enough that I can make it work. Thanks again for all your help!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 16:02:34
|
No problemyou're welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|