Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-04-18 : 01:18:53
|
| if i had many variables like 12-232and 232131-232and wanted only that left of the - symbol from that variable how could i do that? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 01:28:00
|
| select col1, left(col1, charindex('-', col1) - 1)from table1Peter LarssonHelsingborg, Sweden |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-04-18 : 01:30:59
|
| worked thanx |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-04-18 : 02:03:20
|
| what if i had 12123aand324242cand wanted anything left of a non numeric character, and maybe also in another situation how would i store the character that apears |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-04-18 : 04:31:35
|
| It is a immediate solution....check it is feasible for u...put the characters/special characters in table and do the comparisonDeclare @tt table (char varchar(1))insert @ttselect 'a' unionselect 'b' unionselect 'c' unionselect 'd' unionselect 'e' unionselect 'f' unionselect 'g' unionselect 'h' unionselect 'i' unionselect 'j' unionselect 'k' unionselect 'l' unionselect 'm' unionselect 'n' unionselect 'o' unionselect 'p' unionselect 'q' unionselect 'r' unionselect 's' unionselect 't' unionselect 'u' unionselect 'v' unionselect 'w' unionselect 'x' unionselect 'y' unionselect 'z' unionselect '-'Declare @aa table (id varchar(10))insert @aa select '1234a841' unionselect '872134u' unionselect '4789' unionselect '13T678' unionselect '8902-688'select id, col1 from (Select distinct id, (case when charindex(char, lower(id)) > 0 then Left(id, (charindex(char, lower(id)) -1)) when IsNumeric(id)= 1 then id end) as col1 from @aa, @tt ) as b where b.col1 is not null |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 04:34:51
|
This is easier-- prepare sample dataDeclare @aa table (id varchar(10))insert @aa select '1234a841' unionselect '872134u' unionselect '4789' unionselect '13T678' unionselect '8902-688'-- show the expected outputselect id, case when pos = 0 then id else left(id, pos - 1) end as [newid]from ( select id, patindex('%[^0-9]%', id) as pos from @aa ) as xPeter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|