问题
Possible Duplicate:
Splitting one column into two columns in SQL Server CE
I am doing a project with VS2010 using C#.
I have a local database (.sdf file). Here is the example content of my database:
Column1
Frodo Baggins
Samwise Gamgee
Peregrin Took
Meriadoc Brandybuck
.
.
.
What I am trying to do is split names and surnames into two different columns, like this:
Names Surnames
Frodo Baggins
Samwise Gamgee
Peregrin Took
Meriadoc Brandybuck
. .
. .
. .
Since I'm using SQL Server CE 3.5 Edition, LEFT, MID functions didn't work for me. So, how would I do that?
回答1:
try this:
DECLARE @YourTable table (Column1 varchar(50))
INSERT @YourTable VALUES ('Frodo Baggins')
INSERT @YourTable VALUES ('Samwise Gamgee')
INSERT @YourTable VALUES ('Peregrin Took')
INSERT @YourTable VALUES ('Meriadoc Brandybuck')
INSERT @YourTable VALUES ('aa')
INSERT @YourTable VALUES ('aa bb cc')
SELECT
LEFT(Column1,CHARINDEX(' ',Column1)) AS Names
,RIGHT(Column1,LEN(Column1)-CHARINDEX(' ',Column1)) AS Surnames
FROM @YourTable
--both queries produce same output
SELECT
SUBSTRING(Column1, 0, CHARINDEX(' ', Column1))
,SUBSTRING(Column1, CHARINDEX(' ',Column1) + 1, LEN(Column1))
FROM @YourTable
OUTPUT:
Names Surnames
----------- -------------
Frodo Baggins
Samwise Gamgee
Peregrin Took
Meriadoc Brandybuck
aa
aa bb cc
(6 row(s) affected)
来源:https://stackoverflow.com/questions/7985147/dividing-one-column-into-two-columns-in-sql