get last date in tsql - sql

i show you my logic is getting error only when month is December
declare #startDate datetime
declare #endDate datetime
set #startDate = convert(varchar(2),#month)+'/1/'+ convert(varchar(4),#year)
set #endDate = dateadd(DD,-1,(convert(varchar(2),#month+1)+'/1/'+convert(varchar(4),#year)))
while(#startDate < #endDate+1)
begin
insert into #tempday
select #startDate
set #startDate = dateadd(day, 1, #startDate )
end
please help

Once you have #startdate, use:
set #enddate = dateadd(day,-1,dateadd(month,1,#startdate))
And I don't think you mean plsql...
There are other things you could think about too, such as not using a while-loop. Why not query a table that has plenty of rows (such as sys.all_columns) and use:
insert #tempday
select top (datediff(day,#startdate,#enddate)+1)
dateadd(day,row_number() over (order by (select 1))-1,#startdate)
from sys.all_columns;

In case of December #month+1 will get you 13 which is not a valid month number

Related

Setting SQL variables for First day of month and 1 year before

Possibly a dumb question because I'm totally new to SQL (I mean never touched it before today 😟) but I'm adjusting a report in a program we use, so it always gets values for the year ending last month (we run it at the beginning of every month).
So, I want #EndDate to be either 12:59pm of the end of last month, or 00:00am of first of this month, and I want #BeginDate to be same minus 1 year. My code is getting a 'syntax error near keyword SET' - I barely even know what this means, and the past hour on google hasn't helped.
Code:
DECLARE #BeginDate DateTime;
DECLARE #EndDate DateTime;
SET #EndDate = CONVERT(Datetime, CONVERT(date, DATEFROMPARTS(YEAR(GetDate()), MONTH(GetDate()),1)), -- Beginning of this month at 00:00 AM
SET #BeginDate = DATEADD(yy, -1, #EndDate) -- a year before the End date
This is all I've added to the existing report, so if it doesn't work completely independently it won't work at all. Over to you guys ... any suggestions? I'm guessing it's a simple, dumb mistake?
You could try formating it to datetime
DECLARE #EndDate DateTime, #BeginDate DateTime;
SET #EndDate = FORMAT(GetDate(), 'yyyy-MM-01 00:00:00.000')
SET #BeginDate = DATEADD(yy, -1, #EndDate)
Can you try the following. You are trying to set '#enddate' without initially declaring:
DECLARE #EndDate DateTime,#BeginDate DateTime;
select #EndDate = convert(datetime,datefromparts(year(getdate()),month(getdate()),1))
select #BeginDate = DATEADD(yy, -1, #EndDate)
select #EndDate,#BeginDate

How to select a date range without using a from table clause?

I want to write a basic sql query where it outputs all dates between 3-15 months from current date:
DATEADD(month, 3, GETDATE()) AND (DATEADD(month, 15, GETDATE()))
The catch is though there is no table to select from. I just want to perform a basic SELECT to get the list of dates. Is this possible?
Following will help you to pull all dates between two dates.
DECLARE #Date1 DATE, #Date2 DATE
SET #Date1 = '2015-05-28'
SET #Date2 = '2015-06-30'
SELECT DATEADD(DAY,number+1,#Date1) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(DAY,number+1,#Date1) < #Date2
Although there already is an accepted answer, I propose an alternative that doesn't depend on undocumented and unsupported system tables.
declare #Date1 date = '2015-05-28';
declare #Date2 date = '2015-06-30';
declare #Dates table (Date Date);
while #Date1 <= #Date2
begin
insert into #Dates (Date) values (#Date1);
set #Date1 = DateAdd(Day, 1, #Date1);
end
select * from #Dates;

Get the Date Difference Between two dates excluding fridays in SQL Server

I have the following two dates :
startDate = '2017-04-04' and endDate = '2017-04-12'
I would like to find the total count of days between these two dates excluding 'Fridays' using SQL Server.
Any Help from the SQL Server Experts will be highly appreciated!! Thanks in Advance!!
You can use DATENAME to check for 'Friday' do this, something like this :
DECLARE #startDate DATETIME
DECLARE #endDate DATETIME
SET #startDate = '2017-04-04'
SET #endDate = '2017-04-12'
Declare #count int
set #count=0
while #startDate < #endDate
Begin
IF DATENAME(dw, #startDate) <> 'Friday'
SET #count = #count + 1
SET #startDate = DateAdd(d, 1, #startDate)
END
select #count
This will result in :
7
just add below clause to your query
select count(*) from table
where startDate >= '2017-01-12' and endDate <= '2017-04-27'
and datename(WEEKDAY,startdate))<>'Friday'

SQL YEAR(GETDATE())

I'm currently working on a query that returns records from within a date range. The ideal is for the start date to be everything from 2015 onward. In theory, shouldn't each of the three SET statements below set the variable to 2015*?
DECLARE #startDate datetime;
SET #startDate = '20150101';
SET #startDate = YEAR(GETDATE());
SET #startDate = DATEPART(yyyy,GETDATE());
Only the first one, the hardcoded date, behaves as expected. The other two return ALL records that are being queried through. Am I missing something here?
*EDIT: I apologize for how unclear I was with that initially. Basically, #startDate should be set to 01-01-XXXX, where XXXX is whatever year today's date is a part of. It's being compared against another DATETIME variable. I hope that clarifies things.
The answer to your question is "No". The variable #StartDate is date time. This doesn't make sense:
set #startDate = 2015
It doesn't make sense. An integer that looks like a year is not a date.
If you want the first day of the year, you can do:
set #startDate = dateadd(day,
1 - datepart(dayofyear, getdate()),
cast(getdate() as date)
) as FirstDayOfYear
I think this would work (for SQL-Server):
SET #startDate = cast(YEAR(GETDATE()) as varchar(4))
SET #startDate = cast(DATEPART(yyyy,GETDATE()) as varchar(4))
This will show you what's happening:
DECLARE #startDate datetime
SET #startDate = '20150101'
select #startdate
SET #startDate = YEAR(GETDATE())
select #startdate
SET #startDate = cast(YEAR(GETDATE()) as varchar(4))
select #startdate
SET #startDate = DATEPART(yyyy,GETDATE())
select #startdate
SET #startDate = cast(DATEPART(yyyy,GETDATE()) as varchar(4))
select #startdate
YEAR(GETDATE()) and DATEPART(yyyy,GETDATE()); will return just the year part of the date, so if you ran them today you would get 2015 back, not the date 2015-01-01 as you seem to want.
If you want to force the date value to the beginning of the current year, one way would be:
SET #startDate = YEAR(GETDATE()) + '-01-01';

Having issues with dates in SQL

I'm writing a report that needs to collect data each day, between 0900hs and 1700hs.
I thought it would be fine as follows:
cast(convert(char(8),t.trxtime,112)as time)
between CONVERT(VARCHAR(5),getdate(),108) >= '09:00'
and CONVERT(VARCHAR(5),getdate(),108) < '17:00'
....BUT no cigar.
Thank you!!!
Hmmm, you could just use datepart():
where datepart(hour, t.trxtime) between 9 and 16 and
cast(t.trxtime as date) = cast(getdate() as date)
I'm not sure if the date comparison is actually necessary.
You could do something like this (assuming you mean actually for the current date, and not for every date in a range:
declare #startDate datetime
declare #endDate datetime
select #startDate = '2014-11-03 09:00:00',
#endDate = '2014-11-03 17:00:00'
select *
from table
where myDate between #startDate and #endDate
if you did mean between 0900 and 1700 for each day, you could do:
declare #startDate datetime
declare #endDate datetime
select #startDate = '2014-10-03',
#endDate = '2014-11-03' -- note i'm still limiting it to a range of ~1 month
select *
from table
where myDate between #startDate and #endDate
and datepart(hour, myDate) between 9 and 17