Search code examples
sqlvarchar

Get the first space on the left of varchar SQL


I have an Nvarchar column which contains 'John Smith '

I want to divide it to 'John Smith' and ' '

Any idea how to do that? I tried the length but it don't take in consideration the left spaces


Solution

  • This function returns 2 times the string length, including the space, for an nvarchar:

    DATALENGTH(@variable)
    

    So LEFT(@variable, LEN(@variable)) and RIGHT(@variable, DATALENGTH(@variable) / 2 - LEN(@variable)) should work.

    I'm assuming from your use of nvarchar that this is SQL Server...