我在sql server中有表如下
id | name | Date | ---- |----- ------------------------------------------- id1 | name1 | 24/09/2014 | ---- |----- id2 | name2 | 23/09/2014 | ---- |----- id3 | name3 | 23/09/2014 | ---- |----- id4 | name4 | 15/09/2014 | ---- |----- id5 | name5 | 01/08/2014 | ---- |----- id6 | name6 | 01/08/2013 | ---- |-----
如何在列表视图中显示
Today ---------- name 1 date :24/09/2014 yesterday --------- name2 date :23/09/2014 name3 date :23/09/2014 last week --------- ......... date :15/09/2014 two weeks ago -------------- .............. last month -------------- .............. older -------------- ..............
谢谢
答案 0 :(得分:1)
请尝试以下代码。 如果您期望得到不同的结果,请清楚地发布o / p。
SELECT concat(name ,' Date :',date ) 'Today'
FROM #tmp
WHERE DATEDIFF(d,GETDATE() ,date) = 0
SELECT concat(name ,' Date :',date ) 'yesterday'
FROM #tmp
WHERE DATEDIFF(d,GETDATE() ,date) = -1
答案 1 :(得分:0)
如果你想在SQL中使用它
select id,name,date from yourtable group by id,name,date
答案 2 :(得分:0)
你想从sqlserver获得它是完全不好的主意,你需要c#方面的一些东西。我创建了一个样本,您只需传递日期并给出结果。
begin tran
--drop table tempnew
--create table tempnew
--(id varchar(50), name varchar(100), date1 date)
--set dateformat dmy
--insert into tempnew
--values
--( 'id1' , 'name1', '24/09/2014' )
--,( 'id2' , 'name2', '23/09/2014' )
--,( 'id3' , 'name3', '23/09/2014' )
--,( 'id4' , 'name4', '22/09/2014' )
--,( 'id5' , 'name5', '15/09/2014' )
--create procedure getdataBydate
--(
-- @paraDate date = null
--)
--as
declare @paraDate date = getdate() -3
declare @lastmonthofday int
select @lastmonthofday = datepart(DD, DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)) )
select @paraDate,@lastmonthofday,* from tempnew
select name , 'date :', convert( varchar ,date1, 106) date1,
case
when @paraDate = cast ( getdate() AS DATE) then 'Today <br> ----- <br>'
when @paraDate = cast ( dateadd(dd,-1, getdate()) as DATE) then 'Yesterday <br> ----- <br>'
when @paraDate >= cast ( dateadd(dd,-14, getdate()) as DATE) then 'two weeks ago <br> ----- <br>'
when @paraDate = cast ( dateadd(dd,-@lastmonthofday, getdate()) as DATE) then 'last month <br> ----- <br>'
else 'older <br> ----- <br>'
end
from tempnew
where DATE1 <= GETDATE()
and date1 >= @paraDate
--select *
--from tempnew
--where DATE1 <= GETDATE()
--and date1 >= @paraDate
----I don't know why between not work here
----between cast( '2014-09-24' as DATE) and cast( '2014-09-15' as date)
----cast DATE1 between cast( GETDATE() AS date) and @paraDate
--drop table tempnew
rollback