简体   繁体   中英

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)

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).

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!!!):

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
( 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.

The ANSI standard functionality is:

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

Postgres supports this standard syntax.

If you want a string, you can use to_char() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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