Splitting one column into two columns in SQL Server CE

前端 未结 2 1789
情歌与酒
情歌与酒 2021-01-26 03:55

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



        
相关标签:
2条回答
  • 2021-01-26 04:40

    You can combine CHARINDEX and SUBSTRING functions. Both of them are supported in CE according to msdn.

    Example:

    SELECT 
        SUBSTRING(a, 0, CHARINDEX(' ', a)), 
        SUBSTRING(a, CHARINDEX(' ',a) + 1, LEN(a)) 
    FROM
        (SELECT 'Random Joe' as a) t
    
    0 讨论(0)
  • 2021-01-26 04:59

    This should work.

    SELECT SUBSTRING([Name], 0, CHARINDEX(' ', [Name], 0)) [FirstName],
           SUBSTRING([Name], CHARINDEX(' ', [Name], 0) + 1, LEN([Name])) [LastName]
    

    EDIT:

    SELECT SUBSTRING([Name], 0, CHARINDEX(' ', [Name], 0)) [FirstName],
           SUBSTRING([Name], CHARINDEX(' ', [Name], 0) + 1, LEN([Name])) [LastName] 
    FROM
    (
        SELECT 'Ned Stark' [Name] UNION 
        SELECT 'Tyrion Lannister' [Name] UNION
        SELECT 'Daenerys Targaryen' [Name] UNION
        SELECT 'Robert Baratheon' [Name]
    ) n
    

    Just replace the part after from with you table and the column [Name] with you column.

    0 讨论(0)
提交回复
热议问题