查找仅包含特定日期和月份的数据

时间:2015-12-11 16:43:06

标签: sql date sql-server-2012

我正在尝试查找具有日期和月份的特定where子句的数据,但是我收到错误,任何人都可以帮我这个吗?

select *
from my_data
where date BETWEEN '11-20' AND '12-15'
  

MS SQL Server Management Studio

我收到错误

  

从字符串

转换日期和/或时间时转换失败

3 个答案:

答案 0 :(得分:3)

大多数数据库都支持提取日期组件的功能。所以,做你想做的事情的一种方法是将值转换为数字并进行比较,如下所示:

Dec 12, 2015 12:49:35 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.3.Final}
Dec 12, 2015 12:49:35 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 12, 2015 12:49:35 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 12, 2015 12:49:36 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
Dec 12, 2015 12:49:36 AM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Dec 12, 2015 12:49:36 AM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Dec 12, 2015 12:49:36 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Dec 12, 2015 12:49:36 AM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'regionServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:311)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.dreamcorps.sample.controller.MyController.main(MyController.java:18)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:508)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:486)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:615)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
    ... 13 more
Caused by: java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:67)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:142)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:89)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:71)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    ... 28 more


Is there a way to make the AbstractBaseService work as I envision above so that I don't have to create the one Service class with the same implementation as AbstractBaseService for each of my entities (Region, Province, User, Vehicle.... etc)?

提取日期部分的功能因数据库而异,因此您的数据库可能有不同的方法来执行此操作。

答案 1 :(得分:1)

转换失败,因为您没有指定年份。如果您要指定&#39; 11-20-2015&#39;您的查询只会插入您需要的任何年份。

SELECT *
FROM my_data
WHERE date BETWEEN '11-20-2015' AND '12-15-2015'

或者,如果您想要多年来该日期范围内的数据,我会使用while循环在#表中插入信息,然后从该表中读取,具体取决于数据量这可能很快或者这里是一个示例

DECLARE @mindatestart date, @mindateend date, @maxdatestart date

SET @mindatestart = '11-20-2010'
SET @mindateend = '12-15-2010'
SET @maxdatestart = '11-20-2015'

SELECT top 0 *, year = ' '
INTO #mydata
FROM my_data

WHILE @mindatestart < @maxdatestart
    BEGIN
        INSERT INTO #mydata
        SELECT *, YEAR(@mindatestart) 
        FROM my_data
        where date between @mindatestart and @mindateend

        SET @mindatestart = DATEADD(Year, 1, @mindatestart)
        SET @mindateend = DATEADD(Year, 1, @mindateend)
    END 

这将循环并插入2010-2015这些日期范围的数据,并在末尾添加一个额外的列,以便您可以调用数据并按年份排序,如果您愿意这样

    SELECT * FROM #mydata order by YEAR

希望这有一部分有帮助!

以下评论

SELECT *
FROM my_data
WHERE DAY(RIGHT(date, 5)) between DAY(11-20) and DAY(12-15) 

原因&#39; 11-20&#39;不起作用是因为它是一个字符串,这就是你必须在&#39;之间输入它的原因。 &#39; Month()函数的作用是在()之间放置任何内容并将其转换为整数。这就是为什么你在第一个答案中没有得到任何回报的原因,&#39; -Year&#39;从表日期字段添加到数值中,您可以通过使用这些查询从11-20转换您的值

SELECT MONTH(11-20) --Returns 12
SELECT MONTH(11-20-2015) -- Returns 6
SELECT MONTH(11-20-2014) -- Returns 6

使用RIGHT(日期,5)你只能得到月 - 日,那么你就可以约会那天的日期值(正确(日期,5))你应该得到理论上应该属于那些日期范围的东西,尽管但是,我不确定数据的准确程度,而且在原始查询中不添加额外的8个字符需要做很多工作。

答案 2 :(得分:1)

由于您只关心月份和日期,而不是年份,因此您需要使用DATEPART来分割日期。试试这个:

select *
from my_data
WHERE 1=1
AND (DATEPART(m, date) >= 11 AND DATEPART(d,date) >= 20) 
AND (DATEPART(m, date) <= 12 AND DATEPART(d,date) <= 15)