0

저는 프로젝트에서 일하고 있는데 날짜별로 레코드를 필터링하는 데 문제가 있습니다. 나는 엔티티의 레코드를 검색하는 일반적인 방법이 있는데, 내 searchQuery는 '2014-03-15'와 같고 레코드 datetime 형식은 '2014-03-15 00 : 00 : 00.000'이고, 내 SQL 쿼리는 datetime SQL 레코드를 날짜 형식으로 만 다음 내 쿼리와 비교하십시오. 문제점은 레코드에서 genereated 날짜가 내 쿼리에 대해 eqauls가 아니므로 datepart 함수를 사용하여 문자열 결과를 연결하지만 결과는 같은 '2014- 3 15'및 방법이 좋을 것 sqlfunctions 날짜에 문자열을 캐스팅 존재하는 경우, 일치의 원인이되지는 여기에 내 코드입니다SqlFunctions가 문자열을 날짜로 변환합니다.

   var date = Expression.Convert(propExp, typeof(Nullable<DateTime>));      
       var datePart = typeof(SqlFunctions).GetMethod("DatePart", 
        new Type[] { typeof(string), typeof(Nullable<DateTime>) }); 

       var month = Expression.Call(datePart, Expression.Constant("MM"), date);     
       var toDouble = Expression.Convert(month, typeof(Nullable<double>));      
       var monthPartToString = typeof(SqlFunctions).GetMethod("StringConvert", 
        new Type[] { typeof(Nullable<double>) });     
       var monthPart = Expression.Call(monthPartToString, toDouble); 


       var dd = Expression.Call(datePart, Expression.Constant("dd"), date);      
       var toDoubledd = Expression.Convert(dd, typeof(Nullable<double>));      
       var ddPartToString = typeof(SqlFunctions).GetMethod("StringConvert", 
        new Type[] { typeof(Nullable<double>) });      
       var ddPart = Expression.Call(ddPartToString, toDoubledd); 


       var yy = Expression.Call(datePart, Expression.Constant("yyyy"), date);     
       var toDoubleyy = Expression.Convert(yy, typeof(Nullable<double>));     
       var yyPartToString = typeof(SqlFunctions).GetMethod("StringConvert", 
        new Type[] { typeof(Nullable<double>) });      
       var yyPart = Expression.Call(yyPartToString, toDoubleyy); 


       var conCat4 = typeof(string).GetMethod("Concat", 
      new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) }); 
       var conCat2 = typeof(string).GetMethod("Concat", 
        new Type[] { typeof(string), typeof(string) }); 
       var delim = Expression.Constant("-"); 

       stringProp = Expression.Call(conCat2, Expression.Call(conCat4, yyPart, delim, monthPart, delim), ddPart); 

genereated SQL은

입니다

나는 날짜를 비교하는 문자열을 사용하지 마십시오 sqlFunctions

+0

어때 이것에 대해 : http://stackoverflow.com/questions/10304373/how-convert-string-to-date-t-sql – ganders

+0

명확히하기 위해 : 당신은 datetime을 db로 저장하고 (datetime으로 저장), 그리고 당신은 "날짜"부분만을 포함하는 문자열 (db가 아닌 매개 변수로 주어진)과 이것을 비교하고 싶습니까? –

+0

그래,하지만 주요 문제는 내가 sql 함수에 의해 날짜 형식으로 날짜 문자열을 변환해야하고 어떤 일을 할 수있는 함수가 없다, 내가 쿼리를 빌드하는 반사를 사용하고 있으며,이 문제를 해결하기가 더 힘들어 – user3333426

답변

-1

날짜에이 문자열을 주조하는 방법이 필요합니다. EVER. 대신 SQL Server 2008에서는 시간 구성 요소없이 날짜 정보 만 저장하는 Date 유형이 있음을 기억하십시오. 이 같은 유형으로 비교의 양쪽을 변환 SQL을 생성해야합니다 @MyDate가 이미 명시 적으로 날짜 형식이 조회에 포함됩니다 매개 변수입니다

select * from orders where cast(CreatedOn As Date) = @MyDate 

. 절대은 쿼리에서 직접 값이 대체 된 쿼리를 생성합니다.

+0

여기 두 가지 문제가 있습니다. 1 => 나는 레코드를 저장하기 위해 날짜를 사용하고 싶지 않습니다. 다른 쿼리에서 필요합니다. 2 => 날짜로 캐스트를 사용하면 문제가 해결되지만 sqlfunction을 사용하고 있습니다. 이 함수를 사용하는 옵션 없음, sqlFunctions에 의해 날짜로 문자열을 형변환 할 방법이 필요합니다. – user3333426