How do I set EndDate by 3 months - tsql

I am struggling to accomplish set date if value is entered. So I wondering if someone could advise me how to do this. This is what I have done...
Declare
#StartDate DateTime,
#EndDate DateTime,
#Discount Varchar
If #Discount = 'Test123'
Begin
UPDATE tblName SET EndDate = case
When StartDate > dateadd(month,-3,getdate())
End
So what I am trying to do is if end-user enter Test123 the system will calculate that the EndDate would be (for example 11/08/2016) when the StartDate would be (for example 11/05/2016). Bearing mind before entering Testing123 the orginal EndDate has already been stored, for example 10/10/2016.
Thanks in advance for any help given.
Edit
I also continuing searching and found the following link
Here

If I understand your requirements correctly, I think you need something like this.
Declare
#StartDate DateTime,
#EndDate DateTime,
#Discount Varchar
If #Discount = 'Test123'
Begin
UPDATE tblName
SET EndDate = case
When StartDate > dateadd(month,-3,getdate())
Then dateadd(month,3,StartDate)
end
End

Related

PostgreSQL: How to define PARAMETER in query? [duplicate]

This question already has answers here:
How to declare a variable in a PostgreSQL query
(15 answers)
Closed 7 years ago.
In MSSQL, I usually use below parameter like this:
declare #StartDate as date, #EndDate as date
set #StartDate = '08/01/2015' set #EndDate = '08/15/2015'
----------- and use it here as my Date Range of Records:
Select * from Table_Name
where DateRange between #StartDate and #EndDate
How can I do this in PostgreSQL for generating reports/records?
Reference to official documentation:
http://www.postgresql.org/docs/9.1/interactive/plpgsql-structure.html
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html
Do like this
After $BODY$
DECLARE startdate date;
DECLARE enddate date;
After begin
startdate ='2015-08-01';
Enddate ='2015-08-15';
retrun query
select * from Table where daterange between startdate and endate

Date Comparison in If condition in DB2

I have two date parameters BeginDate and EndDate. I need to compare these two dates and then perform some sql select. For example, if BeginDate is lessthan or equal to EndDate, then only I need to query data. For example as below:
If #BeginDate <= #EndDate Then
select statement1
select statement2
...
End if
When I tried in DB2, I am getting an error.
Kindly suggest a sample with DB2 syntax.
In case you want to do it simple, just add the condition to both select statements:
SELECT *
FROM <table_name>
WHERE #BeginDate <= #EndDate;
Other solution would be using IF statement within PL/SQL contexts to execute SQL:
IF (#BeginDate <= #EndDate) THEN
statements
END IF;
A more complete example for inline SQL PL, would be something like:
CREATE PROCEDURE EXAMPLE_IF
(IN BeginDate DATE, IN EndDate DATE)
LANGUAGE SQL
BEGIN ATOMIC
IF (BeginDate <= EndDate)
THEN
statement1;
statement2;
END IF;
END!
Hope it's useful!

SQL Scalar UDF to get number of days in Year

I writing code to determine how many days in a year. I am trying to keep it really simple.
I found code that I think is very clean to determine a leap year. I am passing the inputted date using DATEPART(Y,#Year) to the leap year program and some how am not getting the correct results so I has to be in my SQL code to process the input date as the correct bit is returned.
Here is the code for the Leap Year:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[FN_Is_Leap_Year]
(
-- the parameters for the function here
#year int
)
RETURNS BIT
AS
BEGIN
RETURN (select case datepart(mm, dateadd(dd, 1, cast((cast(#year as varchar(4)) + '0228') as datetime)))
WHEN 2 THEN 1
ELSE 0 END)
END
Here is the code I wrote to process the input date & get the # days in a year:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[FN_Get_Days_In_Year]
(
#InputDT varchar(10)
)
RETURNS int
AS
BEGIN
DECLARE #Result int,
#Year int
Set #Result =
CASE
WHEN dbo.FN_Is_Leap_Year(Datepart(yyyy,#Year)) = 0 Then 365
WHEN dbo.FN_Is_Leap_Year(Datepart(yyyy,#Year)) = 1 Then 366
END
RETURN #Result
END
Got it working!!
GO
ALTER FUNCTION [dbo].[FN_Get_Days_In_Year]
(
#InputDT int
)
RETURNS varchar(3)
AS
BEGIN
Declare #Year int,
#RetVal bit,
#Result varchar(3)
Set #Year = datepart(yy, #InputDT)
Set #RetVal = dbo.FN_Is_Leap_Year(Datepart(yy,'2012'))
Set #Result = CASE #RetVal
WHEN 1 THEN 366
ELSE 365
End
Return #Result
END
Modified version of the above answer :
DECLARE #year INT,
#DaysInYear INT
SET #year = 2011
SELECT #DaysInYear = CASE DATEPART(mm, DATEADD(dd, 1, CAST((CAST(#year AS VARCHAR(4)) + '0228') AS DATETIME)))
WHEN 2 THEN 366 ELSE 365 END
SELECT #DaysInYear 'DaysInYear'

How to convert DATETIME to FILETIME value in T-SQL?

I need to convert a SQL Server DATETIME value to FILETIME in a T-SQL SELECT statement (on SQL Server 2000). Is there a built-in function to do this? If not, can someone help me figure out how to implement this conversion routine as a UDF (or just plain Transact-SQL)? Here is what I know:
FILETIME is 64-bit value representing the number of 100-nanosecond intervals since
January 1, 1601 (UTC) (per MSDN: FILETIME Structure).
SQL Server base time starts on 1900-01-01 00:00:00 (per SELECT CAST(0 as DATETIME)).
I found several examples showing how to convert FILETIME values to T-SQL DATETIME (I'm not 100% sure they are accurate, though), but could not find anything about reverse conversion. Even the general idea (or algorithm) would help.
Okay, I think I was able to implement this myself. Here is the function:
IF EXISTS
(
SELECT 1
FROM sysobjects
WHERE id = OBJECT_ID('[dbo].[fnDateTimeToFileTime]')
AND type = 'FN'
)
BEGIN
DROP FUNCTION [dbo].[fnDateTimeToFileTime]
END
GO
-- Create function.
CREATE FUNCTION [dbo].[fnDateTimeToFileTime]
(
#DateTime AS DATETIME
)
RETURNS
BIGINT
BEGIN
IF #DateTime IS NULL
RETURN NULL
DECLARE #MsecBetween1601And1970 BIGINT
DECLARE #MsecBetween1970AndDate BIGINT
SET #MsecBetween1601And1970 = 11644473600000
SET #MsecBetween1970AndDate =
DATEDIFF(ss, CAST('1970-01-01 00:00:00' as DATETIME), #DateTime) *
CAST(1000 AS BIGINT)
RETURN (#MsecBetween1601And1970 + #MsecBetween1970AndDate) * CAST(10000 AS BIGINT)
END
GO
IF ##ERROR = 0
GRANT EXECUTE ON [dbo].[fnDateTimeToFileTime] TO Public
GO
It seems to be accurate up to 1 second, which is okay with me (I could not make it more accurate due to data overflow). I used the TimeAndDate web tool to calculate the durations between dates.
What do you think?
2 SQL Server time era starts on
1900-01-01 00:00:00 (per SELECT CAST(0
as DATETIME).
No, that is the base date, datetime starts at 1753
run this
select cast('17800122' as datetime)
output
1780-01-22 00:00:00.000
But this is still less than filetime so you need to add that...however remember the gregorian and Julian calendars (also the reason that datetime starts at 1753)
The accepted answer work well, but will crash for date above 19 January 2038. Either use
DATEDIFF_BIG instead of DATEDIFF if you are on SQL Server 2016 or above, or use the following correction
CREATE FUNCTION [dbo].[fnDateTimeToFileTime]
(
#DateTime AS DATETIME
)
RETURNS
BIGINT
BEGIN
IF #DateTime IS NULL
RETURN NULL
DECLARE #MsecBetween1601And1970 BIGINT
DECLARE #MsecBetween1970AndDate BIGINT
DECLARE #MaxNumberDayBeforeOverflowDateDiff int;
SET #MaxNumberDayBeforeOverflowDateDiff = 24855; --SELECT DATEDIFF(day, CAST('1970-01-01 00:00:00' as DATETIME), CAST('2038-01-19 00:00:00' as DATETIME))
DECLARE #nbMaxDaysBetween1970AndDate int;
SET #nbMaxDaysBetween1970AndDate = DATEDIFF(day, CAST('1970-01-01 00:00:00' as DATETIME), #DateTime) / #MaxNumberDayBeforeOverflowDateDiff;
DECLARE #moduloResteDay int
SET #moduloResteDay = DATEDIFF(day, CAST('1970-01-01 00:00:00' as DATETIME), #DateTime) % #MaxNumberDayBeforeOverflowDateDiff;
DECLARE #nbSecondBefore19700101And20380119 bigint = 2147472000;
SET #MsecBetween1601And1970 = 11644473600000;
DECLARE #DateTimeModulo datetime;
SET #DateTimeModulo = DATEADD(day, -#nbMaxDaysBetween1970AndDate * #MaxNumberDayBeforeOverflowDateDiff, #DateTime)
SET #MsecBetween1970AndDate = CAST(CAST(#nbMaxDaysBetween1970AndDate as bigint) * #nbSecondBefore19700101And20380119 +
DATEDIFF(ss, CAST('1970-01-01 00:00:00' as DATETIME), #DateTimeModulo) as bigint)*
CAST(1000 AS BIGINT)
RETURN (#MsecBetween1601And1970 + #MsecBetween1970AndDate) * CAST(10000 AS BIGINT)
END

Multiple argument IF statement - T-SQL

How do I write an IF statement with multiple arguments in T-SQL?
Current source error:
DECLARE #StartDate AS DATETIME
DECLARE #EndDate AS DATETIME
SET #StartDate = NULL
SET #EndDate = NULL
IF (#StartDate IS NOT NULL AND #EndDate IS NOT NULL)
BEGIN
-- do some work
END
It throws the following error:
Incorrect syntax near the keyword
'AND'. Incorrect syntax near the
keyword 'AND'. Incorrect syntax near
')'.
You are doing it right. The empty code block is what is causing your issue. It's not the condition structure :)
DECLARE #StartDate AS DATETIME
DECLARE #EndDate AS DATETIME
SET #StartDate = NULL
SET #EndDate = NULL
IF (#StartDate IS NOT NULL AND #EndDate IS NOT NULL)
BEGIN
print 'yoyoyo'
END
IF (#StartDate IS NULL AND #EndDate IS NULL AND 1=1 AND 2=2)
BEGIN
print 'Oh hey there'
END
Your code is valid (with one exception). It is required to have code between BEGIN and END.
Replace
--do some work
with
print ''
I think maybe you saw "END and not "AND"
That's the way to create complex boolean expressions: combine them with AND and OR. The snippet you posted doesn't throw any error for the IF.
Seems to work fine.
If you have an empty BEGIN ... END block you might see
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'END'.
Not sure what the problem is, this seems to work just fine?
DECLARE #StartDate AS DATETIME
DECLARE #EndDate AS DATETIME
SET #StartDate = NULL
SET #EndDate = NULL
IF (#StartDate IS NOT NULL AND #EndDate IS NOT NULL)
BEGIN
Select 'This works just fine' as Msg
END
Else
BEGIN
Select 'No Lol' as Msg
END