Wednesday, July 21, 2010

(Few) SQL Function

Posted by Aries Wandari

Get first day of current year:
SELECT     *
FROM         tbl
WHERE     (tbl.datefield = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0))

Get last day of current year:
SELECT     *
FROM         tbl
WHERE     DATEADD(dd,-1,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0)))

Get first day of current month:
SELECT     *
FROM         tbl
WHERE     select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

Get first day of last month:
SELECT     *
FROM         tbl
WHERE     DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,-1,getdate())), 0)

Get month part of a datetime field.
DATEPART(MONTH, A.AEDAT)

Get year part of a datetime field.
DATEPART(YEAR, A.AEDAT)

Cast field to char.
CAST(aField AS char(4))

Set 2 digit literal with trailing 0.
RIGHT('0' + RTRIM(MONTH(A.AEDAT)), 2)

Get Date Part Only of Datetime Field
CONVERT(varchar(8), GETDATE(), 112)

Get Time Part Only of Datetime Field
CONVERT(varchar(8), GETDATE(), 108)

Get Yesterday Date

DATEADD(DAY,DATEDIFF(DAY,'20000102',GETDATE()),'20000101')


And many else (from shanecooper.net) 
Monday of the Current Week
select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
Sunday of the Current Week
set DATEFIRST 1
select DATEADD(dd, 1 - DATEPART(dw, getdate()), getdate())
First Day of the Year
select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
First Day of the Quarter
select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
Midnight for the Current Day
select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
Last Day of Prior Month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()  ), 0))
Last Day of Prior Year
select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()  ), 0))
Last Day of Current Month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate()  )+1, 0))
Last Day of Current Year
select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()  )+1, 0))
First Monday of the Month
select DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,6-datepart(day,getdate()),getdate()) ), 0)    

Tuesday, July 20, 2010

Instal/Uninstal Windows Service

Posted by Aries Wandari

Many customized SAP reports run slowly in SAP environment. So we make a bapi to download a mature data and store it on our sql server. The program run as service on the server and run in daily basis. We also profide small application so user can update the data on request.

To install service manually
  1. Go to directory in which installutill located (for me, in WINDOWS\Microsoft.NET\Framework\v2.0.50727).
  2. Run InstallUtil.exe with your exe as parameter. Enter code below:
    installutil abc.exe


To uninstall service manually



  • Run InstallUtil.exe with your exe as a parameter. Enter code below:


    installutil /u abc.exe

Monday, July 19, 2010

SAP Error in spool C call: spool overflow

Posted by Aries Wandari

Error in spool C call: spool overflow
t.code SPAD
menu path: Administration > Clean Spool

Add Full Trust Location on VS2008

Posted by Aries Wandari

Im developing .net application in my local computer. Because we’ll develop it in team, I have to save it in share forlder so any else developer can access. Then come this error when I run the application.

Message=”Request for the permission of type ‘System.Data.OleDb.OleDbPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.”
Its because .net dont trust developer location. Run caspol.exe (in directory C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) to add developer trust site.
D:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>caspol.exe -pp off -m -ag 1.2 –url file:w:\* FullTrust
image