2

我想要做的是:在出生日期之后对数据库中的人进行排序(nvarchar,而不是日期类型)日期是这种字符串格式:dd-MM-yyyy 和排序,我的意思是返回所有人的列表,但出生日期后排序,以填充一个datagridview。

我有一列包含出生日期,即 nvarchar。我想在 sql 查询中将日、月和年存储在 3 个变量中,并测试它们以使它们按顺序排列。

我尝试的是:

SELECT SUBSTRING(date_of_birth,0,2) FROM people AS zi;
SUBSTRING(date_of_birth,3,2) AS luna
SUBSTRING(date_of_birth,6,4) AS an
SELECT [id], [specie], [sex], [date_of_birth], [greutate] FROM [people] WHERE

但我真的不知道从现在开始如何弄清楚......

4

2 回答 2

4
SELECT
  [id], [specie], [sex], [date_of_birth], [greutate],
  SUBSTRING(date_of_birth,6,4)  AS [an],
  SUBSTRING(date_of_birth,3,2)  AS [luna],
  SUBSTRING(date_of_birth,0,2)  AS [zi]
FROM
  [people]
ORDER BY
  SUBSTRING(date_of_birth,6,4),
  SUBSTRING(date_of_birth,3,2),
  SUBSTRING(date_of_birth,0,2)

许多 RDBMS 也允许这样做...

SELECT
  [id], [specie], [sex], [date_of_birth], [greutate],
  SUBSTRING(date_of_birth,6,4)  AS [an],
  SUBSTRING(date_of_birth,3,2)  AS [luna],
  SUBSTRING(date_of_birth,0,2)  AS [zi]
FROM
  [people]
ORDER BY
  [an], [luna], [zi]
于 2013-08-28T20:48:51.057 回答
0

试试这个..

.

SELECT
[id], [specie], [sex], [date_of_birth], [greutate]
From
[People]
ORDER BY
Convert(DateTime, date_of_birth, 105) Asc

.

.

通过将该列转换为 DateTime 类型 105,您可以将其转换为“dd-MM-yyyy”格式

按原样粘贴此代码并尝试..

于 2013-08-28T21:55:46.470 回答