Program Club

SQL Server에서 날짜 만 사용하여 DATETIME 필드를 쿼리하는 방법은 무엇입니까?

proclub 2020. 10. 28. 21:17
반응형

SQL Server에서 날짜 만 사용하여 DATETIME 필드를 쿼리하는 방법은 무엇입니까?


간단한 질문이지만 아직 풀 수 없습니다 ...

다음과 같이 DATETIME 필드가있는 테이블 TEST가 있습니다.

ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000

내가 필요한 것은 아래 쿼리 가이 행과 날짜 = 03/19/2014 인 모든 행을 반환하는 것입니다.

select * from test where date = '03/19/2014';

그러나 행을 반환하지 않습니다. 작업하는 유일한 방법은 시간도 지정하는 것입니다.

select * from test where date = '03/19/2014 20:03:02.000';

미리 감사드립니다!


범위 또는 DateDiff 함수 사용

 select * from test 
 where date between '03/19/2014' and '03/19/2014 23:59:59'

또는

 select * from test 
 where datediff(day, date, '03/19/2014') = 0

다른 옵션은 다음과 같습니다.

  1. 데이터베이스 스키마를 제어 할 수 있고 시간 데이터가 필요하지 않은 경우 제거하십시오.

  2. 또는 유지해야하는 경우 날짜 값의 시간 부분이 제거 된 계산 된 열 속성을 추가합니다.

Alter table Test Add DateOnly As DateAdd(day, datediff(day, 0, date), 0)

또는 최신 버전의 SQL Server에서 ...

Alter table Test Add DateOnly As Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)

그런 다음 쿼리를 다음과 같이 작성할 수 있습니다.

select * from test 
where DateOnly = '03/19/2014'

간단한 대답;

select * from test where cast ([date] as date) = '03/19/2014';

MySQL 5.6을 사용하고 있으며 날짜 시간에서 날짜 부분 만 추출하는 DATE 함수가 있습니다. 따라서 질문에 대한 간단한 해결책은-

 select * from test where DATE(date) = '2014-03-19';

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html


select * from test 
where date between '03/19/2014' and '03/19/2014 23:59:59'

이것은 정말 나쁜 대답입니다. 두 가지 이유가 있습니다.

1. 23.59.59.700 등과 같은 시간에 어떤 일이 발생합니까? 23:59:59보다 큰 시간과 다음 날이 있습니다.

2. 동작은 데이터 유형에 따라 다릅니다. 쿼리는 datetime / date / datetime2 유형에 대해 다르게 작동합니다.

23 : 59 : 59.999로 테스트하면 날짜 유형에 따라 반올림이 다르기 때문에 더 나빠집니다.

select convert (varchar(40),convert(date      , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime  , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))

-날짜 값은 '잘게 썬 것'입니다. -datetime의 경우 값은 다음 날짜로 반올림됩니다. (가장 가까운 값). -datetime2의 경우 값이 정확합니다.


이것은 MS SQL 서버에서 나를 위해 작동합니다.

select * from test
where 
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;

이 시도

 select * from test where Convert(varchar, date,111)= '03/19/2014'

당신은 이것을 시도 할 수 있습니다

select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';

You can use this approach which truncates the time part:

select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)

-- Reverse the date format
-- this false:
    select * from test where date = '28/10/2015'
-- this true:
    select * from test where date = '2015/10/28'

Simply use this in your WHERE clause.

The "SubmitDate" portion below is the column name, so insert your own.

This will return only the "Year" portion of the results, omitting the mins etc.

Where datepart(year, SubmitDate) = '2017'

select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'

"col1" is name of the column with date and time
<name of the column> here you can change name as desired


select *
  from invoice
 where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));

There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.

This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.

select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');

Test this query.

SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key' 
                         ORDER BY is_date DESC, is_time DESC

select * from invoice where TRANS_DATE_D>= to_date  ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date  ('20171031115959','YYYYMMDDHH24MISS');

SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018'  and  DATEPART(day,[TIMESTAMP]) = '16' and  DATEPART(month,[TIMESTAMP]) = '11'

참고URL : https://stackoverflow.com/questions/22506930/how-to-query-datetime-field-using-only-date-in-sql-server

반응형