简体   繁体   English

选择DATEPART,DATENAME还是其他? SQL

[英]SELECT DATEPART, DATENAME or other? SQL

I am creating a new query where I would like to add three additional columns (same format): 我正在创建一个新查询,我想在其中添加三个附加列(相同格式):

Week Number (as 08) / Month (as February) / Year (as 2019) 周数(截至08)/月(截至2月)/年(截至2019)

SELECT hd.F91 AS [PO Number],
hd.F1032 AS [Trs Number],
hd.F76 AS [Order Date],
hd.F27 AS [Vendor ID],
hd.F334 AS [Vendor Name],
hd.F1127 AS Admin,
hd.F1068 AS State,
hd.F1067 AS Status,
tl.F65 AS Total,
DATEPART(wk,[Order Date]) AS [Week Number],
DATENAME('month',[Order Date]) AS Month,
DATENAME('year',[Order Date]) AS Year,

The rest of the data is being pulled from our system but I tried to get these three columns based on an already existing column called Order Date (date format). 其余数据正在从我们的系统中提取,但是我尝试根据称为Order Date(日期格式)的现有列获取这三列。

Unfortunately, the error below showed up. 不幸的是,下面的错误出现了。 All suggestions to try would be welcome. 欢迎所有尝试的建议。 Thanks! 谢谢!

在此处输入图片说明

These functions work in mysql (as was the tag of the question before edited!!!): 这些功能可以在mysql中使用(在编辑之前是问题的标签!!!):

WEEK([Order Date], 1) AS [Week Number],
MONTHNAME([Order Date]) AS Month,
YEAR([Order Date]) AS Year,

change the argument 1 in WEEK according to your needs 根据需要更改WEEK的参数1
( https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week ) https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

试试这个在postgreSQL to_char([order date],'Month')中对我有用

THIS QUESTION WAS ORIGINALLY TAGGED FOR POSTGRES. 这个问题最初是为POSTGRES标签的。

The ANSI standard functionality is: ANSI标准功能是:

extract(week from "Order Date")
extract(month from "Order Date")
extract(year from "Order Date")

Postgres supports this standard syntax. Postgres支持此标准语法。

If you want a string, you can use to_char() : 如果需要字符串,可以使用to_char()

to_char("Order Date", 'WW')
to_char("Order Date", 'YYYY')
to_char("Order Date", 'Month')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM