如何只比较波斯菊数据库中的日期部分 [英] how to compare only date part in cosmos db

查看:58
本文介绍了如何只比较波斯菊数据库中的日期部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将(EmployeeId,Name,LogDate)数据存储在cosmosdb中. LogDate数据是日期时间,我们想从宇宙中获取LogDate在'2018-01-15'和'2018-01-30'之间的数据,这意味着只想比较日期部分.

We are storing (EmployeeId,Name,LogDate) data in cosmosdb. LogDate data is Datetime and we want to get data from cosmos where LogDate between '2018-01-15' and '2018-01-30', Means want to compare date part only.

推荐答案

基于

或者,您可以将DateTimes存储为Unix时间戳,即 一个数字,表示自1月1日以来经过的秒数 1970年.AzureCosmos DB的内部时间戳(_ts)属性遵循此方法.您可以使用UnixDateTimeConverter类进行序列化 日期时间为数字.

Alternatively, you can store DateTimes as Unix timestamps, that is, as a number representing the number of elapsed seconds since January 1, 1970. Azure Cosmos DB's internal Timestamp (_ts) property follows this approach. You can use the UnixDateTimeConverter class to serialize DateTimes as numbers.

因此,我建议您将DateTimes序列化为数字以与条件进行比较.

So, I suggest you serializing DateTimes as numbers to compare with the conditions.

您可以使用用户定义的函数在sql中:

You could use User Defined Function in sql:

UDF:

    function convertTime(datetime){
        datetime = datetime.replace(/-/g,'/')  
        if(datetime){
            var date = new Date(datetime);
        }else{
            var date = new Date();
        }
        time1 = date.getTime(); 
        return time1;
    }

SQL:

SELECT c.LogDate FROM c
where udf.convertTime(c.LogDate) > udf.convertTime('2018-01-15') 
and udf.convertTime(c.LogDate) < udf.convertTime('2018-02-20')

输出:

Output:

当然,您可以在代码中转换日期时间,而不是使用UDF.

Of course, you could convert the datetime in the code instead of using UDFs.

这篇关于如何只比较波斯菊数据库中的日期部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆