是否可以将distinct转换为dateformat month?
列默认值:
datee value = | tuesday May 6, 2014 |
| Monday March 6,2014 |
| tuesday June 6, 2014|
我当前的SQL查询:
Select distinct(datee) as dtall from dbalarmnotice.alamnotice order by datee asc;
Output = | tuesday May 6, 2014 |
| Monday March 6,2014 |
| tuesday June 6, 2014|
Expected Output = | May |
| March |
| June |
答案 0 :(得分:2)
试
Select distinct DATENAME(MONTH, datee) as dtall from dbalarmnotice.alamnotice order by datee asc;
答案 1 :(得分:0)
不知道您使用的是什么SQL。
您可以将以下内容用于MSSQL
SELECT distinct datename(mm,datee) as 'dtall' FROM dbalarmnotice.alamnotice order by datee asc;
或没有明显的(更好的选择)
SELECT datename(mm,datee) AS 'dtall' FROM Dbalarmnotice.alamnotice order by datee asc;
我会使用比" datee"更好的名字。太。
詹姆斯
答案 2 :(得分:0)
因为在评论中提到您的列类型为VARCHAR(45)
,所以最好使用string functions。
当然,只有当月份名称在第一个和第二个space
之间时,这才有效 - 日期值中的字符
SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(datee, ' ', 2), ' ', -1) AS MName
FROM dbalarmnotice.alamnotice
另一种方法是将字符串转换为日期,然后使用MONTHNAME
函数:
SELECT DISTINCT MONTHNAME(STR_TO_DATE(city, '%W %M %d,%Y')) AS MName
FROM dbalarmnotice.alamnotice