User GETDATE() to put current date into SQL variable - sql

I'm trying to get the current date into a variable inside a SQL stored procedure using the following commands
DECLARE #LastChangeDate as date
SET #LastChangeDate = SELECT GETDATE()
This gives me the following error: "Incorrect Syntax near 'SELECT'"
This is the first stored procedure I've ever written, so I'm unfamiliar with how variables work inside SQL.

You don't need the SELECT
DECLARE #LastChangeDate as date
SET #LastChangeDate = GetDate()

Just use GetDate() not Select GetDate()
DECLARE #LastChangeDate as date
SET #LastChangeDate = GETDATE()
but if it's SQL Server, you can also initialize in same step as declaration...
DECLARE #LastChangeDate date = getDate()

DECLARE #LastChangeDate as date
SET #LastChangeDate = GETDATE()

SELECT #LastChangeDate = GETDATE()

You can also use CURRENT_TIMESTAMP for this.
According to BOL CURRENT_TIMESTAMP is the ANSI SQL euivalent to GETDATE()
DECLARE #LastChangeDate AS DATE;
SET #LastChangeDate = CURRENT_TIMESTAMP;

Related

How to pass GETDATE (Only date) as a parameter in sql server?

I want to pass the system date (GETDATE) only as a parameter in my SQL stored procedure. But I am getting an error while executing the procedure.
SQL query:
ALTER PROCEDURE ST_PRO_GETUSER
#D DATETIME = GETDATE --// passing GETDATE as a parameter.
AS BEGIN
select case when branch in ('A25','B10','C10')
then 'BR B1' Else 'BR B2'
end As [COLLECTION],FIXDATE
from MAIN_COUNTER where TDATE=#D --//Just want to pass date only
group by COLLECTION,FIXDATE
END
EXEC KK_SP_GETUSER_DIV
Error:
Conversion failed when converting date and/or time from character string.
What I have to do for it?
To pass as a parameter you just declare a variable and pass it in:
DECLARE #DATE DATETIME = GETDATE();
EXEC ST_PRO_GETUSER #DATE;
And if you want the date only, change the datatype of your parameter to a date and then do:
DECLARE #DATE DATE = GETDATE();
EXEC ST_PRO_GETUSER #DATE;
But part of your question seems to actually be asking how to specify a default parameter value. You can't use a function for the default value, so instead do:
CREATE PROCEDURE ST_PRO_GETUSER
(
#Date DATETIME = null
-- Change to DATE datatype if you don't want a time component.
-- #Date DATE = null
)
AS
BEGIN
SET NOCOUNT ON;
-- Default the #Date here is its null.
-- Note this doesn't handle the case when the caller wants to pass in null.
SET #Date = COALESCE(#Date,GETDATE());
-- SP Body
RETURN 0;
END
Solved My Self
ALTER PROCEDURE ST_PRO_GETUSER
#Date datetime = null
as
IF #Date is null
SET #Date = getdate() --// passing GETDATE as a parameter.
BEGIN
select case when branch in ('A25','B10','C10')
then 'BR B1' Else 'BR B2'
end As [COLLECTION],FIXDATE
from MAIN_COUNTER where TDATE=#D --//Just want to pass date only
group by COLLECTION,FIXDATE
END
EXEC ST_PRO_GETUSER
GETDATE() is the correct syntax, not GETDATE.

How to pass parameter as date by subtracting days to a SQL stored procedure?

I have a stored procedure which takes two parameters, #FromDate and #ToDate. But the #fromDate should be GetDate()-90. How can I pass the parameter value?
ALTER PROCEDURE spMyProcedure
#FromDate DATE,
#ToDate DATE
AS
BEGIN
// SQL statements
END
I want to execute it like this:
EXEC spMyProcedure #FromDate = GETDATE() - 60, #ToDate = GETDATE()
But it throws an error.
You can not do an alter/math/etc on a value passing to a stored procedure. You can declare a date above the stored procedure and then pass that value like this:
DECLARE #PassDate AS DATETIME
SET #PassDate = GETDATE() - 60
EXEC spMyProcedure #FromDate = #PassDate, #ToDate = GETDATE()
I suspect you are working with SQL Server if so, then use dateadd() function inside the stored procedure
set #FromDate = dateadd(day, -90, getdate())

Column Set to 0 Instead of Date Stored Procedure

I am trying to set a column to the current date (in the form dd/mm/yyyy) in a stored procedure, however the column simply sets to 0. The code is as below:
USE [DBDataOne]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
declare #dateone varchar(max)
declare #dodate varchar(max)
select #dateone=convert(varchar,GETDATE(),103 )
select #dodate='Update [dbo].[tabfget] set DATEIN='+#dateone
exec (#dodate)
If datein is stored properly (as a date/time), then you don't need to worry about conversion.
Also, you don't need dynamic SQL for this. Just:
Update [dbo].[tabfget]
set DATEIN = cast(getdate() as date);
If -- horror of horrors -- you are storing dates as strings instead of the proper format, then you should fix the database. If that is not possible, you can do:
Update [dbo].[tabfget]
set DATEIN = convert(varchar(10), GETDATE(), 103);
However, if dates have to be stored as strings, then you should always use an ISO-standard format, such as YYYY-MM-DD.
Why can't you directly do like
Update [dbo].[tabfget] set DATEIN = #dateone
(OR)
Update [dbo].[tabfget] set DATEIN = convert(varchar,GETDATE(),103 )
Yes, the format/style 103 should get you / instead of - like
select GETDATE()
will result in 2015-05-29 20:43:38.547
select CONVERT(varchar(15), GETDATE(), 103)
Will result in 29/05/2015
Try this:
declare #dateone varchar(max)
declare #dodate varchar(max)
select #dateone=convert(varchar,GETDATE(),103 )
select #dodate='Update [dbo].[tabfget] set DATEIN='+quotename(#dateone,char(39));
Exec(#dodate);
You definitely should do the update without dynamic sql. But the reason for the zero is that you're not quoting the date as a literal and it gets evaluated as integer division. (Format 103 is mm/dd/yyyy.)

HH:MM:SS:Msec to HH:MM:SS in stored procedure

I have a stored procedure which update a table based on such calculation and the calculation is done as column name (Calendatedate) - (Current System Date Time) and update this information to a column (TimeSpent) and display the value in Hh:Mm:SS:Msec format.
The query is working fine but I want to update it in such a way so that the time spent should be only HH:MM:SS format. Please help me that how I remove that Msec from the time spent.
CREATE procedure St_Proc_UpdateTimeSpent
#timeEntryID int,
#status int output
as begin
set nocount on;
declare #Date dateTime;
set #Date=GETDATE();
update Production set TimeSpent=(SELECT CONVERT(VARCHAR(20),DateAdd(SS,Datediff(ss,CalendarDate, #Date)%(60*60*24),0),114)),
IsTaskCompleted=1
where productionTimeEntryID=#timeEntryID
set #status=1;
return #status;
end
You can just use style 108 instead of 114 in the CONVERT function to get only the hh:mm:ss:
CREATE PROCEDURE dbo.St_Proc_UpdateTimeSpent
#timeEntryID int,
#status int output
AS BEGIN
SET NOCOUNT ON;
DECLARE #Date DATETIME;
SET #Date = GETDATE();
UPDATE dbo.Production
SET TimeSpent = CONVERT(VARCHAR(20), DATEADD(SS, DATEDIFF(ss, CalendarDate, #Date)%(60*60*24),0), 108),
IsTaskCompleted = 1
WHERE
productionTimeEntryID = #timeEntryID
SET #status = 1;
RETURN #status;
END
See the excellent MSDN documentation on CAST and CONVERT for a comprehensive list of all supported styles when converting DATETIME to VARCHAR (and back)
BTW: SQL Server 2008 also introduced a TIME datatype which would probably be a better fit than a VARCHAR to store your TimeSpent values ... check it out!

ms sql call stored procedure with date time parameter

how do one call a stored procedure that has date input .
spName getDate()
does not work.
the question is about calling within ms sql managment studio.
SQL Server 2008
declare #d date = getdate() /*Or datetime looking at the title*/
exec spName #d
Earlier Versions
declare #d datetime
set #d = getdate()
exec spName #d