1
在学校网站中,我想让管理员在他们出生时根据日期范围对学生进行过滤。在我tblStudent日期存储为字符串,所以我不能使用:在数据库表tblStudent
在SQL Server 2012中转换字符串日期到日期类型
SELECT ts.Name from tblStudent ts WHERE ts.BirthDay>'1367/01/31' AND ts.BirthDay<'1377/01/31'
我已保存日期(贾拉利格式)。我需要根据日期进行比较。所以我需要将日期字符串转换为sql server中的日期类型。为了这个目的,我用:
SELECT convert(date,tblStudent.BirthDay) from tblStudent
然而,27周后的结果与下面的错误
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
我在tblStudent表中的下列日期字符串停止。
1379/09/01
1375/04/20
1378/03/02
1378/03/21
1378/04/18
1378/04/18
1378/05/05
1375/04/20
1379/01/03
1378/03/01
1370/09/09
1378/03/22
1375/09/15
1379/09/01
1379/09/10
1375/04/08
1375/05/06
1370/09/09
1379/10/10
1375/04/10
1375/11/01
1375/04/04
1375/08/11
1375/05/05
1376/09/19
1375/12/12
1376/01/13
1375/15/10
1375/04/14
1375/04/04
1375/05/14
1374/11/11
1375/05/30
1375/05/14
1377/12/13
1377/02/31
1377/12/14
1377/01/13
1375/05/31
1377/11/05
1377/07/05
1375/05/31
1377/03/01
1377/04/01
1377/05/02
1377/05/04
1377/03/03
1377/01/14
1377/05/30
1377/04/31
1375/05/30
1376/06/12
1375/12/10
1377/08/14
1377/03/04
1375/04/08
1375/07/18
1375/08/09
1375/09/12
1375/11/12
1376/12/12
1375/01/02
1375/05/09
1375/04/09
1376/01/01
1375/01/30
1377/04/04
1375/05/23
1375/05/01
1377/02/01
1367/12/05
1375/05/31
1373/03/29
1373/03/03
1375/05/05
有没有办法将这些字符串日期转换为日期类型,然后将它们与某些查询进行比较?例如,这样的查询可以是:
SELECT ts.Name from tblStudent ts where ts.BirthDay>'1375/05/31'
很酷,这适用于数据库中的数据。我想知道我可以使用什么C#函数将用户输入的'yyyy/mm/dd'日期格式转换为yyyymmdd int – JasonStack 2014-09-04 12:11:07
@misaq检查我编辑的解决方案。 – 2014-09-04 12:17:56