拆分的冒号在SQL Server

问题描述:

我在SQL表中的列这将有这样的数据:拆分的冒号在SQL Server

“学院:皇后学院”“大学:昆士兰大学的”

文本之前“:”可能会有所不同。那么,我如何才能选择“:”之前的文本和之后的文本“:(空格)”

+1

什么数据库版? – 2010-10-02 09:20:45

+0

MS SQL 2005 .... – 2010-10-02 09:22:08

你或许应该考虑把你的表到first normal form而不是存储2个数据在同一列...

;with yourtable as 
(
select 'College: Queenstown College' as col UNION ALL 
select 'University: University of Queensland' 
) 
select 
    left(col,charindex(': ',col)-1) AS InstitutionType, 
    substring(col, charindex(': ',col)+2, len(col)) AS InstitutionName 
from yourtable 
+0

非常感谢。这是作品.... – 2010-10-02 09:25:38

+0

可以在“:”之后得到文字? – 2010-10-02 09:33:45

+0

@Wee - 是的。请参阅编辑。 – 2010-10-02 09:40:42

使用charindexsubstring功能:

substring(theField, 1, charindex(':', theField) - 1)