2013-07-23 11 views
0

我有一個存儲過程如下。在我的sales_order表中有一個日期列CP_Created_On。在某些日期的表格中,我插入了01-01-1900。現在我正在從表中選擇記錄。當某些行的日期爲01-01-1900時,我想選擇NULL。我以這種方式需要它,因爲我通過DATASET將記錄導出到Excel。請幫忙在SP的select語句中選擇null時日期是01-01-1900

謝謝。

集ANSI_NULLS ON 設定QUOTED_IDENTIFIER ON 去

ALTER PROCEDURE [dbo].[Sale_OrderPlan_View_Excel_test] --'EM00164466' 
    -- Add the parameters for the stored procedure here 
     -- Add the parameters for the stored procedure here 
(
    @SO_AM varchar(30) 
) 
AS 
BEGIN 
    SET NOCOUNT ON; 
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; 

declare @Cnt int 
CREATE TABLE #Tab 
(SYNC_BY VARCHAR(20)) 
INSERT INTO #Tab 
(SYNC_BY) 
select SYNC_BY from dbo.SYNC_Master where 
MONTH(SYNC_Last_Sync_Date)=MONTH(GETDATE()) and YEAR(SYNC_Last_Sync_Date)=YEAR(GETDATE()) 

set @Cnt=(select count(*) from #Tab) 
drop table #Tab 

-- ACCEPTING INPUT AS "title_code" HAVING VALUE (-1) 

      SELECT SO_Code as 'Form10 Code', 
        SO_ProfitCentre as'Profit Centre', 
--     SO_DivCode as 'SO_DivCode', 
        SO_SalesOffice as 'Sales Office', 
        SO_F_Customer as 'F-Customer Code', 
        SO_CustName as 'F-Customer Name', 
        SO_L_Customer as 'L-Customer', 
        SO_RefDoc  as 'Ref Doc', 
        SO_invoiceNo  as 'Invoice No', 
        SO_DocType  as 'Doc Type', 
        SO_DocDesc  as 'Doc Desc', 
        SO_AccDocNo  as 'Acc Doc No', 
--     SO_SplGLindicator as 'Spl GL indicator', 
        SO_SplGLDesc  as 'Spl GL Desc', 
        convert(varchar,SO_DocDate,105) as 'Doc Date', 
        SO_OutstandingAmt as 'Outstanding Amt', 
        SO_CreditBal  as 'Credit Balance', 
        SO_PureAdvances as 'Pure Advances', 
        SO_UnadjustableCrBal as 'Uneducable Cr Bal', 
        convert(varchar,SO_BaseDate,105) as 'Base Date', 
        SO_LegacyInvoiceRef as 'Legacy Invoice Ref', 
        SO_WBS as 'WBS', 
        tbl_Region_Master.REGIONNAME as 'Region', 
        tbl_Branch_Master.BRANCHNAME as 'Branch', 
        dbo.TBL_DIVISIONGROUP.GROUPNAME as 'Div Group', 
        SO_SubDiv  as 'Sub Div', 
        SO_OrderType  as 'Order Type', 
        SO_Usage   as 'Usage', 
        SO_AgeInDays  as 'Age In Days', 
        SO_LYCY   as 'LY/CY', 
        convert(varchar,SO_invoiceDt,105) as 'Invoice Date', 
        SO_InvoiceAmt as 'Invoice Amt', 
        SO_SalesOrderNO  as 'Sales Order NO', 
        SO_OrderValue as 'Order Value', 
        SO_PoNo as 'Po No', 
        convert(varchar,SO_PoDate,105) as 'Po Date', 
        Dl.NAME1+' '+Dl.NAME2 as 'Dealer Associate', 
        SO_Write_Off as 'Write Off', 
        SO_Adjustments as 'Adjustments', 
        SO_CreditNote as 'CreditNote', 
        SO_OtherDeduction as 'Other Deduction', 
        SO_TDS as 'TDS', 
        SO_NetCollectable as 'NetCollectable', 
        SO_CurrentMnth as 'Current Month', 
        SO_NextMnth  as 'Next Month', 
        SO_NexttoNextMnth as 'Next to Next Month', 
        E1.NAME1+' '+E1.NAME2 as 'SDE', 
        tbl_EmpMaster.NAME1+' '+tbl_EmpMaster.NAME2 as 'SME', 
        E2.NAME1+' '+E2.NAME2 as 'AM', 
        E3.NAME1+' '+E3.NAME2  as 'RM', 
        E4.NAME1+' '+E4.NAME2 as 'AIH', 
        E5.NAME1+' '+E5.NAME2 as 'COMEXE', 
        SO_BUSFIN as 'BUSFIN',    
        SO_CF as 'CF',     
        SO_PlanCode as 'Plan Code', 
        SO_WriteOff_Reason as 'WriteOff Reason', 
        SO_Adjustments_Reason as 'Adjustments Reason', 
        SO_CreditNote_Reason as 'CreditNote Reason', 
        SO_OtherDeduction_Reason as 'Other Deduction Reason', 
        convert(varchar,CP_Created_On,105) as 'Created On', 
        E7.NAME1+' '+E7.NAME2 as 'Approved By' 

      FROM dbo.tbl_Sale_Order 
      left join dbo.tbl_Region_Master on tbl_Sale_Order.SO_Region=dbo.tbl_Region_Master.REGION_ID 
      left join dbo.tbl_Branch_Master on tbl_Sale_Order.SO_Branch=dbo.tbl_Branch_Master.SALES_BRANCH_CODE 
      left join dbo.TBL_DIVISIONGROUP on tbl_Sale_Order.SO_DivGroup=dbo.TBL_DIVISIONGROUP.GROUPCODE 
      left join dbo.tbl_EmpMaster E1 on tbl_Sale_Order.SO_SDE=E1.PORTALUSERNAME 
      left join tbl_EmpMaster on tbl_Sale_Order.SO_SME=tbl_EmpMaster.PORTALUSERNAME 
      left join tbl_EmpMaster E2 on tbl_Sale_Order.SO_AM=E2.PORTALUSERNAME 
      left join tbl_EmpMaster E3 on tbl_Sale_Order.SO_RM=E3.PORTALUSERNAME 
      left join tbl_EmpMaster E4 on tbl_Sale_Order.SO_AIH=E4.PORTALUSERNAME 
      left join tbl_EmpMaster E5 on tbl_Sale_Order.SO_COMEXE=E5.PORTALUSERNAME 
--   left join tbl_EmpMaster E6 on tbl_Sale_Order.CP_Created_By=E6.PORTALUSERNAME 
      left join tbl_EmpMaster E7 on tbl_Sale_Order.Approved_By=E7.PORTALUSERNAME 
      left join dbo.tbl_Dealer_Master Dl on tbl_Sale_Order.SO_Dealer_Associate=Dl.DEALERCODE 
      WHERE 
       [email protected]_AM and SO_OutstandingAmt>0 and SO_CreditBal=0 and @Cnt>0 and month(Creation_date)=Month(getdate()) and year(Creation_date)=year(getdate()) 


END 
+0

谷歌,我想這就是你需要 –

+0

我想這樣做什麼,但它並沒有幫助 「時,選擇的情況下」。這是我寫 情況下 \t \t \t \t \t當CP_Created_On = 1900年1月1日 \t \t \t \t \t然後' \t \t \t \t \t當CP_Created_On!= 1900年1月1日 \t \t \t \t \t然後CP_Created_On \t \t \t結束爲'C關於'\t, – Ankur

+0

您必須將日期包裝到適當的sql日期中。我現在不在電腦後面,但我認爲你的語法不對。 (當其他情況下),但我看到其他人正在幫助語法.. –

回答

1

也許:

SELECT CASE 
     WHEN cp_created_on = '19000101' THEN NULL 
     ELSE cp_created_on 
     END AS CP_Created_On 
FROM dbo.TableName 

Demo

+0

真棒..!非常感謝..!!那工作..! – Ankur

2

NullIF()是你的朋友在這裏!

DECLARE @d date = '1900-01-01'; 

SELECT @d As d 
    , NullIf(@d, '1900-01-01') 
    , NullIf(Current_Timestamp, '1900-01-01') 
相關問題