. , .
Common Table, , charindex() .
, () .
use [Northwind]
go
;
with cteContactsOrders
as
(
SELECT
Contactname as FullName,
Substring(IsNull(Contactname, ''), 1, 4) as FirstFour,
Charindex(' ', IsNull(Contactname, '')) as Pos,
Phone,
Orderid,
Orderdate
FROM
customers as c
INNER JOIN
orders as o
ON
c.Customerid = o.Customerid
)
select
co.*,
case
when Pos > 0 then substring(FullName, 1, Pos-1)
when Pos = 0 and len(ltrim(rtrim(FullName))) > 0 then FullName
else ''
end as FirstName,
case
when Pos > 0 then substring(FullName, Pos+1, len(FullName) - Pos)
else ''
end as LastName
from
cteContactsOrders co
SQL Server 2014 CTP2.
