Saturday, June 28, 2014

How to format datetime & date in MS Sql Server



– MSSQL getdate returns current system date and time in standard internal format
SELECT convert(varchar, getdate(), 100) – mon dd yyyy hh:mmAM (or PM)
                                        – Oct  2 2008 11:01AM          
SELECT convert(varchar, getdate(), 101) – mm/dd/yyyy 10/02/2008       
SELECT convert(varchar, getdate(), 102) – yyyy.mm.dd – 2008.10.02           
SELECT convert(varchar, getdate(), 103) – dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) – dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) – dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) – dd mon yyyy
SELECT convert(varchar, getdate(), 107) – mon dd, yyyy
SELECT convert(varchar, getdate(), 108) – hh:mm:ss
SELECT convert(varchar, getdate(), 109) – mon dd yyyy hh:mm:ss:mmmAM (or PM)
                                        – Oct  2 2008 11:02:44:013AM   
SELECT convert(varchar, getdate(), 110) – mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) – yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) – yyyymmdd
SELECT convert(varchar, getdate(), 113) – dd mon yyyy hh:mm:ss:mmm
                                        – 02 Oct 2008 11:02:07:577     
SELECT convert(varchar, getdate(), 114) – hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) – yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) – yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) – yyyy-mm-ddThh:mm:ss.mmm
                                        – 2008-10-02T10:52:47.513

– SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), ‘/’, ‘ ‘) – yyyy mm dd
SELECT convert(varchar(7), getdate(), 126)                 – yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8)          – mon yyyy

SQL statement that selects a column from a table and converts the results to a comma separated string?

I have a column EmpID in Employee table as shown below


EmpID
E01
E03
E05
E09

Solution:

DECLARE @results VARCHAR(500)

SELECT 
    @results = COALESCE(@results + ',', '') + CONVERT(VARCHAR(12),EmpID)
FROM
    Employee
ORDER BY
    EmpID

SELECT @results AS RESULTS

RESULTS
E01,E03,E05,E09


Tuesday, June 24, 2014

How to change Purchase Order from 'Closed' status to 'Open'

Sometimes our clients need to open a closed Purchase Order document in SAP B1
       
In order to achieve this we make changes:
1. At row level  LineStatus to open(POR1) and
 2.At header level DocStatus to open(OPOR)

 The following vb.net code shows it


Dim rs As SAPbobsCOM.Recordset = sCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)Dim PODocNum As String = XXXXDim str As String = 

           "UPDATE T0 " & _
           "SET T0.LineStatus = 'O' , " & _
           "    T0.TargetType='-1' " & _
           "FROM DBO.POR1 T0 " & _
           "INNER JOIN DBO.OPOR T1 " & _ 
           "ON T0.DocEntry=T1.DocEntry " & _
           "WHERE T1.DocNum = '" & PODocNum & "'"
 rs.DoQuery(str)
 str = Nothingstr = "UPDATE T0 " & _
      "SET T0.DocStatus = 'O' " & _
      "FROM DBO.OPOR T0 " & _
      "WHERE T0.DocNum = '" + PODocNum.ToString + "'" 
rs.DoQuery(str)