作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表 tine_by_day
并且我知道如何在 oracle
中使用 TO_CHAR
函数,但是如果我在MySQL
?
MySQL
有没有TO_CHAR()
的转换函数?
我已经尝试过 date_format
而不是 to_char
但我没有得到足够的结果。
SELECT
to_char(t.the_date,'mm-DD-YYYY') Date,
SUM(sf7.unit_sales) UnitSales,
SUM(sf7.store_sales) StoreSales,
SUM(sf7.store_cost) StoreCost
FROM time_by_day t INNER JOIN sales_fact_1997 sf7 ON t.time_id=sf7.time_id
WHERE
to_char(t.the_date,'YYYY-MM-DD')>='2012-01-01'
AND
to_char(t.the_date,'YYYY-MM-DD')<='2012-01-07'
GROUP BY t.the_date
ORDER BY t.the_date
最佳答案
在 SQL Server 中,您通常会使用 convert()
函数,它远不如 to_char()
方便。对于您的查询,您只需要在 select
子句中使用它:
SELECT convert(varchar(10), t.the_date, 110) as Date,
SUM(sf7.unit_sales) as UnitSales,
SUM(sf7.store_sales) as StoreSales,
SUM(sf7.store_cost) as StoreCost
FROM time_by_day t INNER JOIN
sales_fact_1997 sf7
ON t.time_id = sf7.time_id
WHERE t.the_date >='2012-01-01' AND
t.the_date <= '2012-01-07'
GROUP BY t.the_date
ORDER BY t.the_date;
SQL Server 通常将 ISO 标准 YYYY-MM-DD 视为日期并自动进行转换。唉,有一个特殊的国际化设置将其视为 YYYY-DD-MM。无论此类设置如何,都应正确解释以下内容(尽管我会使用上述形式):
WHERE t.the_date >= cast('20120101' as date) AND
t.the_date <= cast('20120107' as date)
编辑:
在 MySQL 中,您只需使用 date_format()
:
SELECT date_format(t.the_date, '%m-%d-%Y') as Date,
SUM(sf7.unit_sales) as UnitSales,
SUM(sf7.store_sales) as StoreSales,
SUM(sf7.store_cost) as StoreCost
FROM time_by_day t INNER JOIN
sales_fact_1997 sf7
ON t.time_id = sf7.time_id
WHERE t.the_date >= date('2012-01-01') AND
t.the_date <= date('2012-01-07')
GROUP BY t.the_date
ORDER BY t.the_date;
关于mysql - 如何在 MySQL 中使用 to_char 函数功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241574/
我是一名优秀的程序员,十分优秀!