Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

SQL: display get date in a @variable

Hello experts,

I call the following .sql through a  SQLCMD.

DECLARE @vThreshold_PRC NUMERIC(18,2) = $(V_THRESHOLD_PRC),
        @vCurr_PRC NUMERIC(18,2),
		@vMessage NVARCHAR(500),
		@vCpt_sync INT,
		@vCpt_all INT

BEGIN 
	SELECT @vCurr_PRC = CAST(100.00 * (cpt_sync / cpt_all ) AS numeric(18,2)), @vCpt_sync = cpt_sync, @vCpt_all = cpt_all
	FROM (SELECT COUNT(*) * 1.00 cpt_sync FROM $(V_LNKSERVER)[$(V_DBNAME)].[$(V_SCHNAME)].[$(V_TBLNAME)]) S,
		 (SELECT COUNT(*) * 1.00 cpt_all  FROM $(V_LNKSERVER)[$(V_DBNAME)].[$(V_SCHNAME)].[$(V_TBLNAME)_INTERNAL]) I

	IF @vCurr_PRC > @vThreshold_PRC
	BEGIN
		SET @vMessage =  ': Le nombre de projets a transmettre (' + CAST(COALESCE(@vCpt_sync, 0) AS NVARCHAR(50)) + 
		                ') sur un total de ' + CAST(COALESCE(@vCpt_all, 0) AS NVARCHAR(50)) + ' projets, est superieur au pourcentage de projets maximum pouvant etre transmis (' + 
						CAST(COALESCE(@vThreshold_PRC, 0) AS NVARCHAR(50)) + ') : pourcentage courant calcule = ' + CAST(COALESCE(@vCurr_PRC, 0) AS NVARCHAR(50))
		  --PRINT @vMessage
		  RAISERROR(@vMessage,16,1)
	END
	
END
GO

Open in new window


I would like to add at the beginning of @message getdate with with the following format:  2018-06-15 23:54:42.013

SET @vMessage = getdate () +  ':

How should I proceed?

If you have questions, please contact me.
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Not abundantly familiar with SQLCMD scripting, but to pull off concatenating a date with a string you'll have to convert the date to a string so it's an apples-to-apples comparison.  Something like..
SET @vMessage = CAST(getdate () as varchar(25)) +  ': 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of John_Vidmar
John_Vidmar
Flag of Canada 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
Hi,

Yes, John is right. You need to use Style 121 to convert it into your desired form.

Regards,
Pratik
Avatar of Luis Diaz

ASKER

Thank you, John Vidmar proposal works.