1

在尝试了各种处理方法后,我无法在动态 sql 语句中正确获取日期格式。我需要一些有关日期格式的帮助。

我有一个发布日期字段,它以 yyyy-mm-dd 格式存储在 sql db 中。它在 asp 网页中显示为 mm-dd-yyyy ..我需要在 dd-mm-yyyy 中显示日期格式..有什么办法吗?

这是我的 sql 查询:

SELECT CR.Name AS country,C.Name,C.[Name 2],C.[E-Mail],[Pre-Assigned No_], [Posting Date], SIH.[Currency Code], SIH.[Shipment Method Code],[Total Boxes],[ARE Net Wt],[ARE Gr_Wt], SIH.[Shipping Agent Code], SIH.[Payment Terms Code] FROM [Sales Invoice Header] SIH 
INNER JOIN [Country_Region] CR ON CR.Code = SIH.[Country of Final Destination] 
INNER JOIN [Customer] C ON C.No_ = SIH.[Sell-to Customer No_] WHERE SIH.No_ = 'PEXP1213-540'
4

2 回答 2

2

无需替换直接使用106105即可得到想要的格式
SELECT convert(varchar, getdate(), 106)——dd mon yyyy

所以你的查询看起来像

select  CR.Name as country,C.Name,C.[Name 2],C.[E-Mail],[Pre-Assigned No_], 
convert(varchar,[Posting Date], 106) AS [Posting Date], SIH.[Currency Code], SIH.[Shipment Method Code],
[Total Boxes],[ARE Net Wt],[ARE Gr_Wt], SIH.[Shipping Agent Code], SIH.[Payment Terms Code] from [Sales Invoice Header] SIH 
inner join [Country_Region] CR On CR.Code = SIH.[Country of Final Destination] 
inner join [Customer] C on C.No_ = SIH.[Sell-to Customer No_] where SIH.No_ = 'PEXP1213-540'

也可以在codeBehind上做:

DateTime time = DateTime.Now;             // Use current time
string format = "dd mm yyyy";            // Use this format
Console.WriteLine(time.ToString(format));
于 2012-12-18T06:54:04.757 回答
0

请尝试使用此查询

select  CR.Name as country,C.Name,C.[Name 2],C.[E-Mail],[Pre-Assigned No_], 
replace(convert(varchar(12),[Posting Date], 103), '/','-') AS [Posting Date], SIH.[Currency Code], SIH.[Shipment Method Code],
[Total Boxes],[ARE Net Wt],[ARE Gr_Wt], SIH.[Shipping Agent Code], SIH.[Payment Terms Code] from [Sales Invoice Header] SIH 
inner join [Country_Region] CR On CR.Code = SIH.[Country of Final Destination] 
inner join [Customer] C on C.No_ = SIH.[Sell-to Customer No_] where SIH.No_ = 'PEXP1213-540'

注意:这里的 [Posting Date] 列是字符串,所以只需要显示。

于 2012-12-18T06:36:45.893 回答