本文介绍了如何将两个给定日期与来自 sql server 中日期时间字段的日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让用户输入了诸如 CreatedONCreatedEND 之类的日期时间字段,这些字段具有诸如 (29-02-2013) 和 (12-04-2013) 之类的日期时间格式(这些只是样本值)

I have got user entered Datetime fields like CreatedON and CreatedEND and these are having datetime formats like (29-02-2013) and (12-04-2013) (these are sample values only)

我在 sql server 中有一个列,如 CreatedDatetime 和这样的值 2013-09-03 和 2013-02-03(这些只是示例值)现在我需要检查 CreatedDatetime 字段是否在 CreatedONCreatedEND 两个给定日期之间...

I have got one column in sql server like CreatedDatetime and values like this 2013-09-03 and 2013-02-03 (these are sample values only)Now I need to check whether the CreatedDatetime field is in between CreatedON and CreatedEND two given dates...

我如何将来自数据库的日期与这两个值进行比较..这个比较只需要在sql server中进行

How can i compare the date coming from database with these two values..this comparison need to be done in sql server only

有人会为此提出任何想法和解决方案吗?非常感谢提前..

Would any one suggest any ideas and solutions for this ..Many thanks In advance..

   DECLARE  @return_value int

EXEC    @return_value = [dbo].[tp_SelectTransactionHistorySearch]
        @OffSetRowNo = 1,
        @FetchRowNo = 1,
        @StatusSelection = N's',
        @isReviewed = NULL,
        @ProjectCaseNumber = NULL,
        @CostPageNumber = NULL,
        @TransactionTypeChange = NULL,
        @DescriptionChange = NULL,
        @TrasactionCreateOnBeginDate = N'19-03-2013',
        @TransactionCreatedOnEndDate = N'20-03-2013',
        @TransactionUpdatedOnBeginDate = N'10-04-2013',
        @TransactionUpdateOnEndDate = N'11-04-2013',
        @ItemID = NULL

SELECT  'Return Value' = @return_value

GO

错误: 消息 8114,级别 16,状态 5,过程 tp_SelectTransactionHistorySearch,第 0 行将数据类型 nvarchar 转换为日期时间时出错.

ERROR : Msg 8114, Level 16, State 5, Procedure tp_SelectTransactionHistorySearch, Line 0 Error converting data type nvarchar to datetime.

推荐答案

你可以像这样尝试使用 CAST:-

You can try using CAST like this:-

(CreatedDatetime >= CAST(CreatedON AS DATE)) AND (CreatedDatetime < CAST(CreatedEND AS DATE))

这篇关于如何将两个给定日期与来自 sql server 中日期时间字段的日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:14