English 中文(简体)
如何将日期(月/年)与另一个日期(月/年)进行比较
原标题:How to compare date (month/year) with another date (month/year)

• 如何比较信通厅中的日期部分:

I wrote the following query but I get a syntax error:

select id,name from h_history 
 where ( h_date::DATETIME YEAR TO MONTH >= 2/2012 )
   and ( h_date::DATETIME YEAR TO MONTH <= 1/2013 )

1. 与年份和月份比较的大麻

如何做到这一点?

最佳回答

如果你重新使用DATETIME线路,你需要正确制定这些数值。 原文照发:

select id,name from h_history 
 where ( h_date::DATETIME YEAR TO MONTH >= 2/2012 )
   and ( h_date::DATETIME YEAR TO MONTH <= 1/2013 )

<代码>2/2012是一种等于0的立体分类,迄今为止,没有从ger化物中提取任何含蓄的成分,反之亦然。

你可以写:

-- Query 1
SELECT id, name
  FROM h_history 
 WHERE (h_date::DATETIME YEAR TO MONTH >= DATETIME(2012-02) YEAR TO MONTH)
   AND (h_date::DATETIME YEAR TO MONTH <= DATETIME(2013-01) YEAR TO MONTH)

这ver而准确。 你们可以使用一个捷径:

-- Query 2
SELECT id, name
  FROM h_history 
 WHERE (h_date::DATETIME YEAR TO MONTH >=  2012-02 )
   AND (h_date::DATETIME YEAR TO MONTH <=  2013-01 )

然而,由于<代码>h_date<>/code> 是日报,而不是“日报”,因此还有其他选择。 YEAR, MONTH, 录音通知, 排日职能从国名中提取明显部分(如果你通过国名监督署履行这一职能,则国名监督署将受到胁迫,然后处理)。 唯一完全由当地独立的DATE建筑商是MDY功能,需要3个论点,即月、日和年。 所有扼杀性代表都须由当地人解释,因此,在一切时间都赢得工作。

也可以做:

-- Query 3
SELECT id, name
  FROM h_history 
 WHERE (h_date >= MDY(2,  1, 2012))
   AND (h_date <= MDY(1, 31, 2013))

或:

-- Query 4
SELECT id, name
  FROM h_history 
 WHERE ((YEAR(h_date) = 2012 AND MONTH(h_date) >= 2) OR YEAR(h_date) >= 2013)
   AND ((YEAR(h_date) = 2013 AND MONTH(h_date) <= 1) OR YEAR(h_date) <  2013)

或:

-- Query 5
SELECT id, name
  FROM h_history 
 WHERE (YEAR(h_date) * 100 + MONTH(h_date)) >= 201202
   AND (YEAR(h_date) * 100 + MONTH(h_date)) <= 201301

鉴于这一选择,我或许会把表2作为简明扼要但准确的,或或许是表5,但所有问询都可用。

如果h_date 是某种DATETIME类型的一个栏目,而且你需要比较部分的DATETIME,你可以使用(如Query 1所示)的投放或文稿功能。 这往往ver。

问题回答

Assuming that h_date is a proper date field, why wouldn t

SELECT 
      id
    , name
FROM h_history 
WHERE h_date >=  02/01/2012  and h_date <=  01/31/2013 

你们的工作?

询问如下:

SELECT
  id,name 
FROM h_history 
WHERE
  h_date >= MDY(2, 1, 2012)
  AND h_date < MDY(2, 1, 2013)
select * from table t where MONTH(t.date) = 12




相关问题
Weird Time Issue in Python

Problem with using times in Python. Terminal > Python >>> calendar.timegm(datetime.datetime.now().utctimetuple()) 1258449380 This time indicates GMT: Tue, 17 Nov 2009 09:16:20 GMT Eclipse ...

How does a .NET process get the culture information?

I have a Windows service running (C#, .NET 2.0) on Windows Server 2003 R2. In one server the System.Threading.Thread.CurrentThread.CurrentCulture is {en-AU} and in the other {en-US}. This has caused a ...

Best way to get maximum Date value in java?

I m writing a bit of logic that requires treating null dates as meaning forever in the future (the date in question is an expiration date, which may or may not exist). Instead of putting in special ...

Date format in SQL Server

Short Question: What s the best date format to use in SQL Server? Long Explanation: We re converting our database from mysql to SQL Server. In mysql we always used int(11) to avoid the daylight ...

Converting asp.net/sql web app to be ime zone savy

Our current app stores all dates via the server s datetime. I m thinking we need to update all datetime values in the database over to UTC time. Now for displaying these dates back to the user, I ...

Check if string is of SortableDateTimePattern format

Is there any way I can easily check if a string conforms to the SortableDateTimePattern ("s"), or do I need to write a regular expression? I ve got a form where users can input a copyright date (as a ...

DateTime in PropertyGrid in .Net

I m currently working on some DateTime properties in a PropertyGrid in c#.Net. Im using the default drop-down datetime picker. My question: Is there any way to show the full date with the time? By ...

热门标签