如何在SQL中从日期时间中删除时间

时间:2019-02-12 10:39:52

标签: javascript php jquery sql node.js

这是我的查询:

select date(datetime) as dates,
        user_id,
        sum(CASE When status='completed' Then 1 Else 0 End ) as completed,
        sum(CASE When status='incompleted' Then 1 Else 0 End ) as incompleted,
        sum(CASE When status!='' Then 1 Else 0 End ) as total 
from routine_streak 
where user_id ='"+user_id+"' 
and datetime  between '"+start+"' and '"+end+"' 
group by user_id,date(datetime)

它将给出如下输出:

"datetime": "2019-01-30T00:00:00.000Z

但是我想要这样:

"datetime": "2019-01-30"

4 个答案:

答案 0 :(得分:0)

使用如下所示的查询

<?php 
var str = "select DATE_FORMAT(datetime,'%y-%m-%d') as dates,from routine_streak where user_id='183' group by user_id)"; 
?>

答案 1 :(得分:0)

请尝试。

 SELECT
      date_format(datetime, '%Y-%m-%d') AS dates,
      user_id,
      sum(CASE WHEN status = 'completed'
        THEN 1
          ELSE 0 END) AS completed,
      sum(CASE WHEN status = 'incompleted'
        THEN 1
          ELSE 0 END)  AS incompleted,
      sum(CASE WHEN status != ''
        THEN 1
          ELSE 0 END)  AS total
    FROM routine_streak
    WHERE user_id = '"+user_id+"'
          AND datetime BETWEEN '"+start+"' AND '"+end+"'
    GROUP BY user_id, dates

参考https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

答案 2 :(得分:0)

使用强制转换功能仅获取日期部分

cast(datetime as date)

答案 3 :(得分:0)

这是 SQL SERVER

的答案

选择convert(varchar,getdate(),111)

输出将达到预期的水平。

抱歉,要迟到。 感谢这种机会