Wednesday, May 20, 2009

Date Formats in SQL Server

---Yesterday 
select dateadd(d,-1,getdate()) as yesterday

--First Day of Current Week
select dateadd(wk,datediff(wk,0,getdate()),0) as [First Day of Current Week]

--Last Day of Current Week
select dateadd(wk,datediff(wk,0,getdate()),6) as [Last Day of Current Week]

--First Day of Last Week
select dateadd(wk,datediff(wk,7,getdate()),0) as [First Day of Last Week]

--Last Day of Last Week
select dateadd(wk,datediff(wk,7,getdate()),6) as [Last Day of Last Week]

--First Day of Current Month
select dateadd(mm,datediff(mm,0,getdate()),0) as [First Day of Current Month]

--Last Day of Current Month
select dateadd(ms,- 3,dateadd(mm,0,dateadd(mm,datediff(mm,0,getdate())+1,0))) as [Last Day of Current Month]

--First Day of Last Month
select dateadd(mm,-1,dateadd(mm,datediff(mm,0,getdate()),0)) as [First Day of Last Month]

-- First day of next month
SELECT dateadd(m, datediff(m, 0,getdate())+1, 0) AS MonthStart

-- Last day of next month
SELECT dateadd(m,datediff(m, 0, dateadd(m, +2 ,getdate()))+1, -1) AS MonthEnd

--Last Day of Last Month
select dateadd(ms,-3,dateadd(mm,0,dateadd(mm,datediff(mm,0,getdate()),0))) as [Last Day of Last Month]

--First Day of Current Year
select dateadd(yy,datediff(yy,0,getdate()),0) as [First Day of Current Year]

--Last Day of Current Year
select dateadd(ms,-3,dateadd(yy,0,dateadd(yy,datediff(yy,0,getdate())+1,0))) as [Last Day of Current Year]

--First Day of Last Year
select dateadd(yy,-1,dateadd(yy,datediff(yy,0,getdate()),0)) as [First Day of Last Year]

--Last Day of Last Year
select dateadd(ms,-3,dateadd(yy,0,dateadd(yy,datediff(yy,0,getdate()),0))) as [Last Day of Last Year]

select month('09/06/2008')
select year('09/06/2008')
select getutcdate()
select getdate()
select day('09/06/2008')
select datepart(dd,'09/06/2008')
select datename(yyyy,'09/06/2008')
select datediff(dd,'09/06/2008','09/18/2008')
select dateadd(dd,60,'09/06/2008')

Tuesday, March 31, 2009

Limiting the File Upload Size in ASP.NET

By default, the maximum size of a file to be uploaded to a server using the ASP.NET FileUpload control is 4MB. You cannot upload anything that is larger than this limit.

To change this size limit, you have to make some changes in the application's web.config:    

    

   

maxRequestLength - Attribute limits the file upload size for ASP.NET application. This limit can be used to prevent denial of service attacks (DOS) caused by users posting large files to the server. The size specified is in kilobytes. As mentioned earlier, the default is "4096" (4 MB). Max value is "1048576" (1 GB) for .NET Framework 1.0/1.1 and "2097151" (2 GB) for .NET Framework 2.0.

executionTimeout - Attribute indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by the application. The executionTimeout value should always be longer than the amount of time that the upload process can take. 

Saturday, February 28, 2009

How Get Start and End days of a week/month?

DECLARE @SchDate datetime
SET @SchDate  = getdate()

-- Gets Week Start Day
SELECT  DATEADD(wk, DATEDIFF(wk, 6, @SchDate), 6) AS WeekStart
-- Gets Week End day
SELECT DATEADD(wk, DATEDIFF(wk, 5, @SchDate), 5) AS WeekEnd
-- Gets Month Start Day
SELECT dateadd(m, datediff(m, 0, @SchDate), 0) AS MonthStart
-- Gets Month End Day
SELECT dateadd(m, datediff(m, 0, dateadd(m, 1 ,@SchDate)), -1) AS MonthEnd

Friday, February 27, 2009

TSQL List of table and size in SQL database

I found this on searching for list of tables.

SELECT
X.[name],
REPLACE(CONVERT(varchar, CONVERT(money, X.[rows]), 1), '.00', '') AS [rows],
REPLACE(CONVERT(varchar, CONVERT(money, X.[reserved]), 1), '.00', '') AS [reserved],
REPLACE(CONVERT(varchar, CONVERT(money, X.[data]), 1), '.00', '') AS [data],
REPLACE(CONVERT(varchar, CONVERT(money, X.[index_size]), 1), '.00', '') AS [index_size],
REPLACE(CONVERT(varchar, CONVERT(money, X.[unused]), 1), '.00', '') AS [unused]
FROM
(SELECT
CAST(object_name(id) AS varchar(50)) AS [name],
SUM(CASE WHEN indid < 2 THEN CONVERT(bigint, [rows]) END) AS [rows],
SUM(CONVERT(bigint, reserved)) * 8 AS reserved,
SUM(CONVERT(bigint, dpages)) * 8 AS data,
SUM(CONVERT(bigint, used) - CONVERT(bigint, dpages)) * 8 AS index_size,
SUM(CONVERT(bigint, reserved) - CONVERT(bigint, used)) * 8 AS unused
FROM sysindexes WITH (NOLOCK)
WHERE sysindexes.indid IN (0, 1, 255)
AND sysindexes.id > 100
AND object_name(sysindexes.id) <> 'dtproperties'
GROUP BY sysindexes.id WITH ROLLUP) AS X
ORDER BY X.[name]