English 中文(简体)
如何在全名栏上打上最后名字?
原标题:How to order by last name on a full name column?

我有一张表格,一栏名为“全面国家”,其中包括“约翰·史密斯”。

怎样才能将数据按正式Name一栏的最后一个名字排列?

就象“Laurence John Fishburne”这样的一个长期名称而言,我要将数据按“Fishburne”一词排列。

因此,姓名按顺序排列。

  • First Name
  • Middle Names
  • Last Name

我正在使用微软服务器2005年。

最佳回答

如果有疑问,它会:

Select
*
From
Users
Order By
LTrim(Reverse(Left(Reverse(FullName), CharIndex(   , Reverse(FullName))))) Asc,
FullName Asc -- Fall-over for when there isn t a last name
问题回答

我想做的是:

SELECT FullName
FROM TABLE
ORDER BY REVERSE(SUBSTRING(REVERSE(FullName), 0, CHARINDEX(   , REVERSE(FullName)))) 

您不计算每当你想管理问询时的最后名字,就可有一个计算栏,把实际价值维持在一栏,可以像你这样使用。

ALTER TABLE Customer
    ADD LastName AS 
        RIGHT(FullName, CHARINDEX(   , REVERSE(FullName)) - 1) PERSISTED

这比照你的客户表增加最后一栏,利用指定的职能计算最后名称的价值,并将之储存在实物表中。 最终所需的关键词是将存储到磁盘上,否则每当盘问时将计算。

Edit: To deal with values with no spaces:

ALTER TABLE Customer
    ADD LastName AS 
    case when CHARINDEX(   , REVERSE(FullName)) > 0
    then RIGHT(FullName, CHARINDEX(   , REVERSE(FullName)) - 1) 
    else
    FullName
    end
    PERSISTED

虽然你可以履行这一职能,因为你将确定本案发生的情况。 我要表明,可以使用案件陈述。 您还可能希望把所有产出途径引入同样的类型,以避免出现类型的不匹配。

为此,它利用最低数量的职能,在“全美”范围内找到最后的空间,这将有助于履约:

DECLARE @YourTable table (FullNamevarchar(30))

INSERT @YourTable VALUES ( Harry Smith );
INSERT @YourTable VALUES ( John Davis );
INSERT @YourTable VALUES ( Allision Thomas Williams );

SELECT
    FullName
        ,RIGHT(FullName, CHARINDEX(   , REVERSE(FullName)) - 1) AS SortBy
    FROM @YourTable
    ORDER BY RIGHT(FullName, CHARINDEX(   , REVERSE(FullName)) - 1)

OUTPUT:

FullName                       SortBy
------------------------------ ------------------------------
John Davis                     Davis
Harry Smith                    Smith
Allision Thomas Williams       Williams

(3 row(s) affected)

为了取得最佳业绩和最准确的成绩,你需要将名称分为第一名称、中Name和上Name地区。 只有数据输入的用户才能真正理解“ReparName”究竟是哪一部分。

<<>质量>

SELECT stringrowname FROM tablename 
ORDER BY SUBSTRING_INDEX((stringrowname)," ",-1);

它将以最后的措辞恢复扼杀令。

create table foo(fullname varchar(100))
go

insert into foo values ( Harry Smith );
insert into foo values ( John Davis );
insert into foo values ( Allision Thomas Williams );

SELECT fullname
     , REVERSE(left(REVERSE(fullname), charindex(   ,REVERSE(fullname))-1))
  FROM foo
ORDER BY REVERSE(left(REVERSE(fullname), charindex(   ,REVERSE(fullname))-1))

产出:

fullname                   (No column name)
John Davis                  Davis
Harry Smith                 Smith
Allision Thomas Williams    Williams

这样做是为了:

create table #tmpTable
(
    ID int identity(1,1) primary key,
    FullName varchar(100) not null
);

insert into #tmpTable(FullName) values( Big John Sansom );
insert into #tmpTable(FullName) values( Mike Douglas Reid );
insert into #tmpTable(FullName) values( First Second Last );
insert into #tmpTable(FullName) values( JustOneTokenForName );

select 
    FullName,
    LastName = case 
        when CHARINDEX(FullName,   ) = 0 THEN FullName
        else RIGHT(FullName, CHARINDEX(   , REVERSE(FullName)) - 1)
    end
from #tmpTable
order by LastName

drop table #tmpTable

这确实取决于如何储存姓名。 假定“Last, First Middle”你可以做这样的事情。

order by substring(0, charindex( , , FullName))

你可能不得不 with,但我认为应该工作。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签