更正TSQL查询以比较日期的语法

时间:2016-09-29 20:19:18

标签: tsql datetime

我正在寻找在TSQL WHERE子句中测试的正确语法 datetime2(7)类型等于另一个。

WHERE (CAST(modifiedDate AS DATETIME) = '9/29/2016 3:24:24 PM')

我也试过

WHERE (CAST(modifiedDate AS DATETIME) LIKE '9/29/2016 3:24:24 PM')

WHERE (CAST(modifiedDate AS datetime2) = CAST('09/29/2016 3:24:24 PM' AS datetime2))

我相信我的右侧声明不正确,但这是数据库中的确切值。

我正在寻找与该日期时间戳匹配的所有记录。

要明确我确实试图搜索其他结果..'

enter image description here

我认为这对于本网站的搜索结果来说有点不稳定。

数据库字段类型......

enter image description here

2 个答案:

答案 0 :(得分:2)

要比较DATETIME值,您需要DATETIME值进行比较。您可以使用TSQL import java.util.Arrays; import java.util.Random; public class Main { private static Step[] steps = new Step[]{ new Step(2, 5), new Step(5, 3), new Step(10, 2) }; private static final int[] pokestops = new int[calcLength(steps)]; private static int calcLength(Step[] steps) { int total = 0; for (Step step : steps) { total += step.maxCount * step.size; } return total; } public static void main(String[] args) { Random rand = new Random(); for (int i = 0; i < pokestops.length; i++) { pokestops[i] = Math.abs(rand.nextInt() % 5) + 1; } System.out.println(Arrays.toString(pokestops)); int[] initialCounts = new int[steps.length]; for (int i = 0; i < steps.length; i++) { initialCounts[i] = steps[i].maxCount; } Counts counts = new Counts(initialCounts); Tree base = new Tree(0, null, counts); System.out.println(Tree.max.currentTotal); } static class Tree { final int pos; final Tree parent; private final int currentTotal; static Tree max = null; Tree[] children = new Tree[steps.length*2]; public Tree(int pos, Tree parent, Counts counts) { this.pos = pos; this.parent = parent; if (pos < 0 || pos >= pokestops.length || counts.exceeded()) { currentTotal = -1; } else { int tmp = parent == null ? 0 : parent.currentTotal; this.currentTotal = tmp + pokestops[pos]; if (max == null || max.currentTotal < currentTotal) max = this; for (int i = 0; i < steps.length; i++) { children[i] = new Tree(pos + steps[i].size, this, counts.decrement(i)); // uncomment to allow forward-back traversal: //children[2*i] = new Tree(pos - steps[i].size, this, counts.decrement(i)); } } } } static class Counts { int[] counts; public Counts(int[] counts) { int[] tmp = new int[counts.length]; System.arraycopy(counts, 0, tmp, 0, counts.length); this.counts = tmp; } public Counts decrement(int i) { int[] tmp = new int[counts.length]; System.arraycopy(counts, 0, tmp, 0, counts.length); tmp[i] -= 1; return new Counts(tmp); } public boolean exceeded() { for (int count : counts) { if (count < 0) return true; } return false; } } static class Step { int size; int maxCount; public Step(int size, int maxCount) { this.size = size; this.maxCount = maxCount; } } } 函数将字符串转换为DATETIME数据类型。例如:

CONVERT

请注意,第三个参数是&#34; style&#34;。上面的示例使用样式20,ODBC规范样式YYYY-MM-DD HH:MI:SS(24小时制)。还有其他几种样式,也许你找到一个与你的字符串格式匹配的样式。 (如果找不到匹配项,那么您需要使用一些字符串操作函数将字符串重新格式化为样式可用的格式。)

作为第二个参数,您可以使用字符串文字(如上例所示),或者您可以使用对CHAR或VARCHAR列的引用。

参考:CAST and CONVERT (Transact-SQL)

在另一个但相关的注释中:为什么日期时间值在数据库中存储为字符串,而不是DATETIME数据类型?

如果列的数据类型为CONVERT(DATETIME, '2016-09-28 15:34:00', 20) ,那么我认为您希望比较DATETIME2(7)数据类型。

如果我们做&#34;等于&#34;比较,这将是一个完全匹配,包括小数秒。如果要匹配给定秒的DATETIME2(7)值,可以使用范围比较:

DATETIME2(7)

请注意,与DATETIME2进行比较的字符串文字格式为 WHERE t.my_col_datetime2_7 >= '2016-09-29 15:24:24' AND t.my_col_datetime2_7 < '2016-09-29 15:24:25' (24小时制),可选小数秒YYYY-MM-DD HH:MI:SS

参考:Supported String Literal Formats for datetime2

答案 1 :(得分:0)

不确定您遇到问题的地方。我想这取决于你如何存储你的日期值。如果它是datetime2,这很好。

class Profile(object):

    @property
        def account_owner(self):
            owner_data_from_ledger = self.account.ledger.data.get('owner', None)
            owner_data_from_profile = self.data.get('owner', None)

            owner_data = owner_data_from_ledger if owner_data_from_ledger else owner_data_from_profile

            if owner_data:
                return Human(owner_data)
            return None