예를 들어서 3일전부터 오늘까지 해당하는 데이타를 조회해라고 한다면 어떻게 하면 되나요?
A 테이블
==================
pk_id | name | jumsu | date
1 aa 51 2016-05-23 10:24:00
2 bb 19 2016-07-02 09:20:00
3 cc 100 2016-08-13 17:20:00
4 dd 75 2016-09-02 04:45:00
5 ee 36 2016-09-08 13:20:00
6 ff 74 2016-09-09 2120:00
위와 같이 데이타가 있다면 이틀전부터 오늘까지의 데이타를 조회하고 싶으면 어떻게 하먄 되나요?
전 지금까지 날짜를 정해서 조회는 해봤는데 이런건 못해봐서요
부탁드리겠습니다
Comment 4
-
이리
2016.09.09 09:21
-
문상연
2016.09.09 10:08
select name, Date from tableAwhere (convert(varchar(10), Date ,126) BETWEEN convert(varchar(10), getdate() ,126) and convert(varchar(10),getdate() ,126) )위와 같이 하면 오늘날짜만 나와야 되는데 어제날짜 데이타도 나옵나다select name, Date from tableAwhere (convert(varchar(10), Date ,126) BETWEEN '2016-09-09' and '2016-09-09'위와 같이 하면 오늘꺼만 나옵니다차이가 뭔가요? -
이리
2016.09.09 11:33
;WITH TBL_TEMP1AS(SELECT 1 AS pk_id, 'aa' AS [name], 51 AS jumsu, '2016-05-23 10:24:00' AS [date] UNION ALLSELECT 2 AS pk_id, 'bb' AS [name], 19 AS jumsu, '2016-07-02 09:20:00' AS [date] UNION ALLSELECT 3 AS pk_id, 'cc' AS [name], 100 AS jumsu, '2016-08-13 17:20:00' AS [date] UNION ALLSELECT 4 AS pk_id, 'dd' AS [name], 75 AS jumsu, '2016-09-02 04:45:00 ' AS [date] UNION ALLSELECT 5 AS pk_id, 'ee' AS [name], 36 AS jumsu, '2016-09-08 13:20:00' AS [date] UNION ALLSELECT 6 AS pk_id, 'ff' AS [name], 74 AS jumsu, '2016-09-09 21:20:00' AS [date])SELECT [name], [Date]FROM TBL_TEMP1WHERE (convert(varchar(10), Date ,126) BETWEEN convert(varchar(10), getdate() ,126) and convert(varchar(10),getdate() ,126) );WITH TBL_TEMP2AS(SELECT 1 AS pk_id, 'aa' AS [name], 51 AS jumsu, '2016-05-23 10:24:00' AS [date] UNION ALLSELECT 2 AS pk_id, 'bb' AS [name], 19 AS jumsu, '2016-07-02 09:20:00' AS [date] UNION ALLSELECT 3 AS pk_id, 'cc' AS [name], 100 AS jumsu, '2016-08-13 17:20:00' AS [date] UNION ALLSELECT 4 AS pk_id, 'dd' AS [name], 75 AS jumsu, '2016-09-02 04:45:00 ' AS [date] UNION ALLSELECT 5 AS pk_id, 'ee' AS [name], 36 AS jumsu, '2016-09-08 13:20:00' AS [date] UNION ALLSELECT 6 AS pk_id, 'ff' AS [name], 74 AS jumsu, '2016-09-09 21:20:00' AS [date])SELECT [name], [Date]FROM TBL_TEMP2where (convert(varchar(10), Date ,126) BETWEEN '2016-09-09' and '2016-09-09')같은 결과가 나옵니다. -
문상연
2016.09.09 18:22
고맙습니다
DATEADD
GETDATE
BETWEEN