Previous Monday & previous Sunday’s date based on today’s date

Easy: –start of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0) –end of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6) EDIT: The below will handle the Sunday date issue. DECLARE @input varchar(10) –SET @input=”9/9/2012″ — simulates a Sunday SET @input = GETDATE() –start of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, CASE DATEPART(dw,@input) WHEN 1 … Read more

SQL select * from column where year = 2010

select * from mytable where year(Columnx) = 2010 Regarding index usage (answering Simon’s comment): if you have an index on Columnx, SQLServer WON’T use it if you use the function “year” (or any other function). There are two possible solutions for it, one is doing the search by interval like Columnx>=’01012010′ and Columnx<=’31122010′ and another … Read more

How to group by a Calculated Field

Sure, just add the same calculation to the GROUP BY clause: select dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 – datepart(weekday, mwspp.DateDue))), sum(mwspp.QtyRequired) from manufacturingweekshortagepartpurchasing mwspp where mwspp.buildScheduleSimID = 10109 and mwspp.partID = 8366 group by dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 – datepart(weekday, mwspp.DateDue))) order by dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 – datepart(weekday, mwspp.DateDue))) … Read more

Re-order columns of table in Oracle

Since the release of Oracle 12c it is now easier to rearrange columns logically. Oracle 12c added support for making columns invisible and that feature can be used to rearrange columns logically. Quote from the documentation on invisible columns: When you make an invisible column visible, the column is included in the table’s column order … Read more