Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQL查询转换字符串中的日期格式?_Sql_Sql Server_Petapoco - Fatal编程技术网

如何使用SQL查询转换字符串中的日期格式?

如何使用SQL查询转换字符串中的日期格式?,sql,sql-server,petapoco,Sql,Sql Server,Petapoco,我正在使用ASP.NET MVC和petapoco。我想在视图中显示数据库表中的数据,因为它有date列,我想将该日期格式转换为varchar格式,如'dd-mm-yyyy' 我的日期列是TransactionDate 我的查询如下,我使用了convert函数,但它不起作用,我的日期列仍然以毫秒出现,如/Date152235232/ 请帮助我,并告诉我如何将表中的特定TransactionDate列转换为'dd-mm-yyyy'格式的日期 var sql = db.Query("Select C

我正在使用ASP.NET MVC和petapoco。我想在视图中显示数据库表中的数据,因为它有date列,我想将该日期格式转换为varchar格式,如
'dd-mm-yyyy'

我的日期列是
TransactionDate

我的查询如下,我使用了
convert
函数,但它不起作用,我的日期列仍然以毫秒出现,如
/Date152235232/

请帮助我,并告诉我如何将表中的特定
TransactionDate
列转换为
'dd-mm-yyyy'
格式的日期

var sql = db.Query("Select CONVERT(varchar, d.TransactionDate ,101), d.DocumentId, d.SurveyNumber, d.FullName, d.Location, documenttype.DocumentTypeName, d.VillageName, d.PlanNumber, d.OwnerName, d.ContactNumber, d.Email, d.Status, d.TransactionDate FROM Document d " +
                                $"INNER JOIN DocumentType ON d.DocumentTypeId = documenttype.DocumentTypeId");

在这里的查询中,我连接了两个表,从表中获取数据以显示它

如果您的字段是日期类型,并且希望格式化dd-mm-yyyy,那么 你应该使用

   select CONVERT (varchar(10), getdate(), 105)
 return :15-08-2018
但是如果您想要dd/mm/yyyy,那么您必须使用下面的

CONVERT(VARCHAR(20), getdate(), 103) as date_conv
  15/08/2018

有关

的更多信息,请尝试以下操作:105将为您提供dd-mm-yyyy格式:


您的查询将返回两次日期。也许这会满足您的需求:

select convert(varchar(10), d.TransactionDate, 105) as TransactionDate,
       d.DocumentId,
       d.SurveyNumber,
       d.FullName, 
       d.Location,
       dt.DocumentTypeName,
       d.VillageName,
       d.PlanNumber,
       d.OwnerName,
       d.ContactNumber, d.Email,
       d.Status
from Document d join
     DocumentType dt
     on d.DocumentTypeId = dt.DocumentTypeId;

注意原始的
TransactionDate
已从查询中删除。我还为
DocumentType

添加了一个表别名,您最好在客户机上进行日期(和数字)转换。然后,客户端可以为当前用户以最佳方式进行转换。您使用的是哪种dbms?(该查询是特定于产品的。)我使用的是sql Server,但在上面的转换中,我应该将日期的列名放在哪里function@prathamesh93您将把日期列替换为getdate()position@prathamesh93有帮助吗?如果有帮助,请接受答案
select convert(varchar(10), d.TransactionDate, 105) as TransactionDate,
       d.DocumentId,
       d.SurveyNumber,
       d.FullName, 
       d.Location,
       dt.DocumentTypeName,
       d.VillageName,
       d.PlanNumber,
       d.OwnerName,
       d.ContactNumber, d.Email,
       d.Status
from Document d join
     DocumentType dt
     on d.DocumentTypeId = dt.DocumentTypeId;