CREATE TABLE #tmpTbl (m VARCHAR(100))
INSERT INTO #tmpTbl VALUES
(',[Undergraduate1]')
,(',[Undergraduate10]')
,(',[Undergraduate11]')
;
GO
select LEFT(m, PATINDEX('%[0-9]%', m)-1) as a,
SUBSTRING(m, PATINDEX('%[0-9]%', m), LEN(m)) as b from #tmpTbl
drop table #tmpTbl
如果给出上面的tmptable和select语句,结果如下:
a | b
-------------------------------------------------------
,[Undergraduate | 1]
,[Undergraduate | 10]
,[Undergraduate | 11]
但我希望它是这样的。
a | b
-------------------------------------------------------
,[Undergraduate | 1
,[Undergraduate | 10
,[Undergraduate | 11
我怎样才能实现这一目标?我尝试了很多与PATINDEX,LEFT,RIGHT,SUBSTRING,LEN的组合。但是不能在B栏中找到正确的
答案 0 :(得分:8)
您可以使用替换删除]。狡猾,但它实现了你想要的东西
select LEFT(m, PATINDEX('%[0-9]%', m)-1) as a,
REPLACE(SUBSTRING(m, PATINDEX('%[0-9]%', m), LEN(m)),']','') as b from #tmpTbl
替代方法:反转字符串,子串以删除第一个字符,反向返回
select LEFT(m, PATINDEX('%[0-9]%', m)-1) as a,
REVERSE(SUBSTRING(REVERSE(SUBSTRING(m, PATINDEX('%[0-9]%', m), LEN(m))),2,LEN(M))) as b from #tmpTbl
答案 1 :(得分:3)
您可以使用REPLACE
将]
替换为''
select LEFT(m, PATINDEX('%[0-9]%', m)-1) as a,
REPLACE(SUBSTRING(m, PATINDEX('%[0-9]%', m), LEN(m)), ']', '') as b from #tmpTbl
答案 2 :(得分:3)
我倾向于将stuff()
用于此目的:
select replace(stuff(m, 1, patindex(m, '%[0-9]%'), ''), ']', '')
答案 3 :(得分:2)
这是一种替代方法,可以删除任何文本,只留下数字。
static class Node{
int data;
Node next;
Node(int num){
data=num;
next=null;
}
}
// Function for merging two sorted linked list
public void merge(Linkedlist list1,Linkedlist list2){
Linkedlist l=new Linkedlist();
Node new_node1=new Node(0);
Node new_node=new_node1; //This line of code is my doubt!
while(list1.head!=null || list2.head!=null){
if(list1.head!=null && list2.head!=null){
if(list1.head.data<=list2.head.data ){
new_node.next=list1.head; // what difference it makes by using new_node.next instead of new_node1
list1.head=list1.head.next;
}
else{
new_node.next=list2.head;
list2.head=list2.head.next;
}
}
else if(list1.head==null){
new_node.next=list2.head;
list2.head=list2.head.next;
}
else if(list2.head==null){
new_node.next=list1.head;
list1.head=list1.head.next;
}
new_node=new_node.next;
//new_node1=new_node1.next;
}
l.printlist(new_node1);
}