简体   繁体   English

使用不同的转换为日期格式的SQL查询

[英]SQL query using distinct convert to date format

Is it possible to convert distinct to dateformat month ? 是否可以将非重复转换为dateformat月?

Column Default Value: 列默认值:

datee value = | tuesday May 6, 2014 |  
              | Monday March 6,2014 |  
              | tuesday June 6, 2014|

My current SQL query: 我当前的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  |

尝试

Select distinct DATENAME(MONTH, datee) as dtall from dbalarmnotice.alamnotice order by datee asc;

Without knowing what SQL you are using. 不知道您正在使用什么SQL。

You can use the following for MSSQL 您可以将以下内容用于MSSQL

SELECT distinct datename(mm,datee) as 'dtall' FROM dbalarmnotice.alamnotice order by datee asc;

or without distinct (Better option) 有无(更好的选择)

SELECT datename(mm,datee) AS 'dtall' FROM Dbalarmnotice.alamnotice order by datee asc;

I would use an better name than "datee" too. 我也会使用比“ datee”更好的名称。

James 詹姆士

Because in comment you mentioned that you column type is VARCHAR(45) , may be will be better to use string functions . 因为在评论中您提到列类型为VARCHAR(45) ,所以使用字符串函数可能会更好。
Of course this will work only if name of the month is between first and second space -character in the date value 当然,仅当月份名称在日期值的第一个和第二个space之间时才有效

SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(datee, ' ', 2), ' ', -1) AS MName
FROM dbalarmnotice.alamnotice

Another approach will be convert string to date and then using MONTHNAME function: 另一种方法是将字符串转换为日期,然后使用MONTHNAME函数:

SELECT DISTINCT MONTHNAME(STR_TO_DATE(city, '%W %M %d,%Y')) AS MName
FROM dbalarmnotice.alamnotice

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

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