在SQL Server中的冒号上拆分

时间:2010-10-02 09:18:13

标签: sql sql-server split

我在SQL表中有一个列有这样的数据:

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

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

2 个答案:

答案 0 :(得分:4)

您应该考虑将您的表放入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

答案 1 :(得分:1)

使用charindexsubstring函数:

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