Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Date getting inserted as 1900-01-01

Hello Experts,
Any suggestions on the SP where the date is getting inserted as 1900-01-01 when it is suppose to be blank. Thanks in advance.

USE [xyz]
GO
10:57:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO






ALTER PROCEDURE [dbo].[xyz_InsertUpdate_MOT] (
  
  @Registration				NVARCHAR(255)	= NULL
, @RefNo					NVARCHAR(255)	= NULL
, @RefMOT				    NVARCHAR(255)	=NULL
, @ReportedBy			    NVARCHAR(255)   =NULL
, @Garage				    NVARCHAR(255)
, @Notes				    NVARCHAR(max)	= NULL
, @Vehicle				    NVARCHAR(200)	= NULL
, @VehColor				    NVARCHAR(200)	= NULL
, @Make						NVARCHAR(50)	= NULL
, @Model				    NVARCHAR(50)	= NULL
, @IsNotActive 				NVARCHAR(2)		= NULL	
, @IsDeleted                NVARCHAR(2)		= NULL	
, @MOTRef1				    NVARCHAR(255)
, @MOTRef2				    NVARCHAR(255)
, @MOTRef3				    NVARCHAR(255)
, @Milage				    NVARCHAR(255)
, @MilageRef1				NVARCHAR(255)
, @MilageRef2				NVARCHAR(255)
, @MilageRef3				NVARCHAR(255)
, @AccountName				NVARCHAR(200)	= NULL
, @MachineName				NVARCHAR(200)	= NULL
, @NewID					INT						OUTPUT
) 

AS 

BEGIN

CREATE TABLE #action (
		action varchar(10)
		)



-- UserID will be used to look for the user in the XYZ_Users table.
DECLARE @UserID INT
-- Check to see if we have either an AccountName or Machine name and then check the users table !

IF @AccountName IS NOT NULL
BEGIN
	SELECT @UserID = ID
	FROM XYZ_Users
	WHERE AccountName = @AccountName

	IF @UserID IS NULL
	BEGIN
		INSERT INTO XYZ_Users (AccountName) VALUES (@AccountName)
		SET @UserID = @@IDENTITY
	END
END
ELSE
BEGIN
	SELECT @UserID = ID
	FROM XYZ_Users
	WHERE MachineName = @MachineName
		AND AccountName IS NULL

	IF @UserID IS NULL
	BEGIN
		INSERT INTO XYZ_Users (MachineName) VALUES (@MachineName)
		SET @UserID = @@IDENTITY
	END

END


/*

.

*/

	MERGE MOT AS target  
	USING (SELECT @RefNo, @Registration) AS source (RefNo,Registration )  
		ON (target.RefNo = source.RefNo And  target.registration = source.registration)  
	WHEN MATCHED THEN 
			

				UPDATE 
		SET  ReportedBy=@ReportedBy,Vehicle=@Vehicle,VehColor=@VehColor,IsNotActive=@IsNotActive
						,Garage=@Garage,Registration=@Registration,Make=@Make,Model=@Model
						, [MOTRef]  = CASE when @RefMOT= '' then NULL else CAST(@RefMOT  AS  nvarchar(20)) end 
						, [MOTRef1]  = CASE when @MOTRef1= '' then NULL else CAST(@MOTRef1  AS  nvarchar(20)) end 
						, [MOTRef2]  = CASE when @MOTRef2= '' then NULL else CAST(@MOTRef2  AS  nvarchar(20)) end 
						, [MOTRef3]  = CASE when @MOTRef3= '' then NULL else CAST(@MOTRef3  AS  nvarchar(20)) end 
						,Milage=@Milage
						,MilageRef1=@MilageRef1,MilageRef2=@MilageRef2,MilageRef3=@MilageRef3,
						Notes=@Notes, IsDeleted = NULL,LoggedBy=suser_sname()
		    
		 


	
	
	WHEN NOT MATCHED THEN  
	
	
		INSERT ( Registration, ReportedBy,Vehicle,Garage,Notes,Make,Model,VehColor, IsNotActive,LoggedDate,LoggedBy,MOTREF,MOTRef1,MOTRef2,MOTRef3,Milage,MilageRef1,MilageRef2,MilageRef3)  
		VALUES (@Registration,@ReportedBy,@Vehicle,@Garage,@Notes,@Make,@Model,@VehColor,@IsNotActive,getdate(),suser_sname(),@RefMOT,@MOTRef1,@MOTRef2,@MOTRef3,@Milage,@MilageRef1,@MilageRef2,@MilageRef3)

	
	
	OUTPUT $action
	INTO #action;

	IF EXISTS (SELECT 1 FROM #action WHERE action = 'UPDATE')
		SET @NewID = 0
	ELSE
	SET @NewID = 1


END

Open in new window

Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

make sure that your field in DB allows nulls then on insert make sure you use NULL not empty string

ziolko
Avatar of RIAS

ASKER

Thanks, It isset to  NULL.


Avatar of RIAS

ASKER

Any suggestions on how to modify the above SP?

are you sure [MotRef] is correct syntax and your case statement properly sets NULL?
Avatar of RIAS

ASKER

Thanks, The above SP is not setting the Case  for which I need your advise. I have set allow Null in the Table Design (Database).


Avatar of RIAS

ASKER

This need a bit of tweaking 

INSERT ( Registration, ReportedBy,Vehicle,Garage,Notes,Make,Model,VehColor, IsNotActive,LoggedDate,LoggedBy,MOTREF,MOTRef1,MOTRef2,MOTRef3,Milage,MilageRef1,MilageRef2,MilageRef3)  
		VALUES (@Registration,@ReportedBy,@Vehicle,@Garage,@Notes,@Make,@Model,@VehColor,@IsNotActive,getdate(),suser_sname(),@RefMOT,@MOTRef1,@MOTRef2,@MOTRef3,@Milage,@MilageRef1,@MilageRef2,@MilageRef3)

 
	

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

OK, got it, will try passing NULL from my visual studio code and get back 

Avatar of RIAS

ASKER

Thanks.