Procedure to get dates in between - sql-server

I have a SQL Server database that contains a timesheet table. This table is used to store roles for employees. Before I perform an insert I check to see of an employee has any preexisting roles in the selected days.
Here’s a stored procedure that returns the count of pre-existing roles
set #retVal=(select count(fk_RoleID) from dbo.TimesheetTable where
(#startdate >= CAST((
STR( YEAR( StartDate ) ) + '/' +
STR( MONTH( StartDate ) ) + '/' +
STR( DAY( StartDate ) )
)
AS DATE
))-- AND EndDate <= '2012-08-30')
and
(#enddate < CAST(
(
STR( YEAR( EndDate ) ) + '/' +
STR( MONTH( EndDate ) ) + '/' +
STR( DAY( EndDate ) )
)
AS DATE
))
and fk_PersonnelID=#personnelID)
return #retVal
The following are the records for one employee..
pk_ID fk_PersonnelID fk_RoleID StartDate EndDate dateCreated
62 1 26 2012-10-01 2012-10-02 2012-10-25 15:55:12.940
81 1 20 2012-10-04 2012-10-06 2012-10-30 14:50:28.300
If I try to do an insert of where the start date is 2012-10-05 and end date is 2012-10-11, the query fails to trap the startdate ..and the insert occurs
What am I doing wrong?

You overlap test is incorrect.
The test should be "both starts cometh before the opposite ends". Remember it, it's easy.
SELECT #retVal = CASE WHEN EXISTS (
select *
from dbo.TimesheetTable
where StartDate <= #EndDate
and #StartDate <= EndDate
and fk_PersonnelID=#personnelID) THEN 1 ELSE 0 END
To test for existence, switch to using EXISTS which will short-circuit and give you a result as soon as a result is found instead of COUNTing all matches.

You have a logical error in your date comparison inequalities because the date interval ranges I think you are attempting to match are overlapping - not properly contained - as your code seems to assume.
i.e. 10/4 - 10/6 strictly overlaps 10/5- 10/11 while your code implies that the parameter interval has to lie completely within the data row
s interval.

You shouldn't need to parse the StartDate and EndDate columns to get the results desired.
SELECT #retVal = COUNT(fk_RoleID)
FROM dbo.TimesheetTable AS tt
WHERE #startDate <= tt.EndDate
AND #endDate >= tt.StartDate
AND fk_PersonnelId = #personnelID;
return #retVal;
#retVal here would return a number greater than 0 for inputs that already have a schedule in the database.

Related

How to get current date - 7 days of data in Snowflake

The field has a date value in this format : 20230215 : YYYYMMDD
I want to select only those records where date is a date in last 7 days.
Is there a way to achieve this in Snowflake with date format?
WITH
CTE
AS
(
SELECT
CASE
WHEN (DATECOL = '' AND DATECOL is NULL and DATECOL = 'NULL') THEN '3000-01-01'
ELSE TO_CHAR(TO_DATE(TO_CHAR(DATECOL), 'YYYYMMDD'), 'YYYY-MM-DD')
END AS DATECOL_FINAL
FROM TABLE
)
SELECT * FROM CTE WHERE DATECOL_FINAL > current_date () - 7
This code does not work, error : Can't parse ' ' as date with format 'YYYYMMDD'
The DATECOL is varchar (8)
I think you want. I'll leave it up to you to decide on > vs >=
try_to_date(col) > current_date()-7
If you want to include empty strings and literal nulls, modify that to
try_to_date(col) > current_date()-7 or try_to_date(col) is null
Below code solved the issue in Snowflake
WITH
CTE_DATE
AS
(
SELECT
CASE
WHEN (DATECOL <> '' AND DATECOL is NOT NULL and DATECOL <> 'NULL') THEN TO_CHAR(TO_DATE(TO_CHAR(DATECOL), 'YYYYMMDD'), 'YYYY-MM-DD')
ELSE '3000-01-01'
END AS TEST_DATE,*
FROM TABLENAME WHERE LEN (DATECOL) <> 1
)
SELECT * FROM CTE_DATE WHERE TEST_DATE > CURRENT_DATE()-7 ORDER BY TEST_DATE ASC

Check if date falls within Month and Year range

If a user selects a range such as:
Start: November 2016
End: September 2017,
I want to include all results that fall within the range of 2016-11-01 to 2017-09-30.
I tried concatenating together the year, month, and day, however the issue comes that not all months have the same last day. While I know all months start on day 01, a month's end day can be 28, 29, 30, or 31.
Is there a way to do this without constructing the date? SqlServer 2008 doesn't have the EOMONTH function, and I feel like anything more complex than that is not the right solution. I would like to avoid this:
WHERE
DateCol >= '2016' + '-' + '11' + '-01' AND
DateCol <= '2017' + '-' + '09' + '-30'
It really seems to me that the easiest and best answer is to go from the first of the beginning month to the first of the month after the ending month, and make the second comparison not inclusive.
In other words, instead of this:
WHERE
DateCol >= '2016' + '-' + '11' + '-01' AND
DateCol <= '2017' + '-' + '09' + '-30'
simply this:
WHERE
DateCol >= '2016' + '-' + '11' + '-01' AND
DateCol < '2017' + '-' + '10' + '-01'
There is a faster way to do so :
DECLARE #minDate DATE
DECLARE #maxDate DATE
SET #minDate = XXXXX
SET #maxDate = YYYYY
-- Get the first day of the month minDate.
SET #minDate = CONVERT(datetime,CONVERT(varchar(6),#minDate,112)+'01',112)
-- Get the last day of the month minDate.
SET #maxDate = CONVERT(datetime,CONVERT(varchar(6),#maxDate,112)+'01',112)
SET #maxDate = DATEADD(day, -1, DATEADD(month, 1, #maxDate))
SELECT * FROM myTABLE WHERE DateCol >= #minDate AND DateCol <= #maxDate
Or :
SELECT * FROM myTABLE
WHERE DateCol >= CONVERT(datetime,CONVERT(varchar(6),XXXXX,112)+'01',112)
AND DateCol <= DATEADD(day, -1, DATEADD(month, 1, CONVERT(datetime,CONVERT(varchar(6),YYYYY,112)+'01',112)))
Use syntax like CONVERT(datetime,'20170930',112) or CONVERT(datetime,'09-30-2017',110) for XXXXX and YYYYY rather than '2017-09-30' that use SQL Server implicit convertion from char to datetime (rely on the server configuration : can be hazardous!!!)).
Using this syntax is faster because #minDate and #maxDate do not need any evaluation. So that indexes can be used directly...
Otherwise a scalar function that will simulate the eomonth() behaviour could be usefull...
You could use following select statement to get last date of any month (and any year) by passing a field or date to it:
DECLARE #dtDate DATE
SET #dtDate = '09/25/2016'
SELECT CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate)+1,0)) AS DATE) AS LastDay_AnyMonth
Please provide some example data and desired result and I will update my answer further.
Here you go:
DECLARE #YourTable TABLE (YourData DATE);
INSERT INTO #YourTable VALUES
('2016-11-01'),
('2016-09-05'),
('2017-03-03'),
('2017-11-11'),
('2017-12-14'),
('2017-09-30');
WITH CTE AS (
SELECT YourData
FROM #YourTable
WHERE YEAR(YourData) =2016 AND MONTH (YourData) >= 11
)
SELECT YourData
FROM #YourTable
WHERE YEAR(YourData) =2017 AND MONTH (YourData) <= 9
UNION ALL
SELECT YourData
FROM CTE;
There is no need to know the end of the month (28 or 30 or 31).
For 2008, you can simply convert the string
Example
Select Date1=convert(date,'November 2016')
,Date2=dateadd(DAY,-1,dateadd(MONTH,1,convert(date,'September 2017')))
Returns
Date1 Date2
2016-11-01 2017-09-30
So the WHERE would be somthing like this
...
Where DateCol between convert(date,'November 2016')
and dateadd(DAY,-1,dateadd(MONTH,1,convert(date,'September 2017')))
A useful construct for performing date-based operations is to make use of a Date Dimension table. You are creating a lookup table that is populated with a lot of information about dates over a large span of time. You can then query the table based on the information that you do have. The table is small enough so that it does not impose significant performance concerns.
In your particular case, you have the month and year. You would plug that into the date dimension table to get the first of the month from the beginning month and the last of the month from the ending month. You now have a time range to search over without any complex logic or calculations on the fly.
Aaron Bertrand explains it in depth here: https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/

Get the amount of days in a month - mdx

In TSQL I can do this to get the amount of days in some month:
declare #date as datetime
set #date = '2015-02-06'
select datediff(day, dateadd(day, 1-day(#date), #date),
dateadd(month, 1, dateadd(day, 1-day(#date), #date)))
How can I get the same functionality in MDX?
P.S. I need the result to be an int.
If you have a year-month-date hierarchy in place, it could be done in the below fashion:
WITH MEMBER NumOfDaysInMonth AS
DateDiff(
"d",
HEAD(DESCENDANTS([Date].[Calendar Date].CURRENTMEMBER, 1)).ITEM(0).MEMBER_CAPTION, //Gets the first date of the month
TAIL(DESCENDANTS([Date].[Calendar Date].CURRENTMEMBER, 1)).ITEM(0).MEMBER_CAPTION //Gets the last date of the month
) + 1
You just need to to pass the month's value in the slicer. The calculated member will do the rest.
SELECT NumOfMonths ON 0
FROM [YourCube]
WHERE ([Date].[Calendar Date].[Month].&[Dec-2015])
This is the method we use:
WITH
MEMBER [MEASURES].[NumOfDaysInMonth] AS
IIF
(
VBA!Isdate([Date].[Calendar].CurrentMember.Name)
,Datepart
("D"
,
Dateadd
("M"
,1
,Cdate
(
Cstr(VBA!Month([Date].[Calendar].CurrentMember.Name)) + "-01-"
+
Cstr(VBA!Year([Date].[Calendar].CurrentMember.Name))
)
)
- 1
)
,''
)
SELECT
NON EMPTY
{[MEASURES].[NumOfDaysInMonth]} ON 0
,NON EMPTY
{
[Date].[Calendar].[All]
,[Date].[Calendar].[Calendar Year].&[2005]
,[Date].[Calendar].[Calendar Semester].&[2008]&[2]
,[Date].[Calendar].[Month].&[2006]&[3]
,[Date].[Calendar].[Date].&[20060214]
,[Date].[Calendar].[Month].&[2007]&[11]
} ON 1
FROM [Adventure Works];
The above returns the following:

Convert Date Stored as NUMERIC to DATETIME

I am currently working on a query that needs to calculate the difference in days between two different dates. I've had issues with our DATE columns before, because they are all being stored as numeric columns which is a complete pain.
I tried using CONVERT as I had done in the past to try and get the different pieces of the DATETIME string built, but I am not having any luck.
The commented line --convert(datetime,) is where I am having the issue. Basically, I need to convert PO_DATE and LINE_DOCK_DATE to a format that is usable, so I can calculate the difference between the two in days.
USE BWDW
GO
SELECT
[ITEM_NO]
,[ITEM_DESC]
,[HEADER_DUE_DATE]
,[BWDW].[dbo].[DS_tblDimWhs].WHS_SHORT_NAME AS 'Warehouse'
,[BWDW].[dbo].[DS_tblFactPODtl].[PO_NO] AS 'PO NUMBER'
,[BWDW].[dbo].[DS_tblFactPODtl].[PO_DATE] AS 'Start'
,[BWDW].[dbo].[DS_tblFactPODtl].[PO_STATUS] AS 'Status'
,[BWDW].[dbo].[DS_tblFactPODtl].[LINE_DOCK_DATE] AS 'End'
--,(SELECT CONVERT(DATETIME, CONVERT(CHAR(8), [BWDW].[dbo].[DS_tblFactPODtl].[PO_DATE])) FROM dbo.DS_tblFactPODtl)
FROM [BWDW].[dbo].[DS_tblFactPODtl]
INNER JOIN [BWDW].[dbo].[DS_tblDimWhs] ON [BWDW].[dbo].[DS_tblFactPODtl].WAREHOUSE = [BWDW].[dbo].[DS_tblDimWhs].WAREHOUSE
INNER JOIN [BWDW].[dbo].[DS_tblFactPO] ON [BWDW].[dbo].[DS_tblFactPODtl].PO_NO = [BWDW]. [dbo].[DS_tblFactPO].PO_NO
WHERE [BWDW].[dbo].[DS_tblFactPODtl].[PO_STATUS] = 'Closed'
AND [BWDW].[dbo].[DS_tblFactPODtl].[LINE_DOCK_DATE] <> 0
I have a snippet I saved from a previous project I worked on that needed to only display results from today through another year. That had a bunch of CAST and CONVERTS in it, but I tried the same methodology with no success.
In the long run, I want to add a column to each database table to contain a proper datetime column that is usable in the future... but that is another story. I have read numerous posts on stackoverflow that talk about converting to NUMERIC and such, but nothing out of a NUMERIC back to DATETIME.
Example data:
Start | End | Difference
--------------------------------
20110501 | 20111019 | 171
20120109 | 20120116 | 7
20120404 | 20120911 | 160
Just trying to calculate the difference..
MODIFIED PER AARON:
SELECT
FPODtl.[ITEM_NO] AS [Item]
,FPODtl.[ITEM_DESC] AS [Description]
,D.WHS_SHORT_NAME AS [Warehouse]
,FPODtl.[PO_NO] AS [PO NUMBER]
,FPODtl.[PO_DATE] AS [Start]
,FPODtl.[PO_STATUS] AS [Status]
,FPODtl.[LINE_DOCK_DATE] AS [End]
,DATEDIFF
(
DAY,
CASE WHEN ISDATE(CONVERT(CHAR(8), FPODtl.PO_DATE)) = 1
THEN CONVERT(DATETIME, CONVERT(CHAR(8), FPODtl.PO_DATE)) END,
CASE WHEN ISDATE(CONVERT(CHAR(8), FPODtl.[LINE_DOCK_DATE])) = 1
THEN CONVERT(DATETIME, CONVERT(CHAR(8), FPODtl.[LINE_DOCK_DATE])) END
)
FROM [dbo].[DS_tblFactPODtl] AS FPODtl
INNER JOIN [dbo].[DS_tblDimWhs] AS D
ON FPODtl.WAREHOUSE = D.WAREHOUSE
INNER JOIN [dbo].[DS_tblFactPO] AS FPO
ON FPODtl.PO_NO = FPO.PO_NO
WHERE FPODtl.[PO_STATUS] = 'Closed'
AND FPODtl.[LINE_DOCK_DATE] <> 0;
DECLARE #x NUMERIC(10,0);
SET #x = 20110501;
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), #x));
Result:
2011-05-01 00:00:00.000
To compare two:
DECLARE #x NUMERIC(10,0), #y NUMERIC(10,0);
SELECT #x = 20110501, #y = 20111019;
SELECT DATEDIFF
(
DAY,
CONVERT(DATETIME, CONVERT(CHAR(8), #x)),
CONVERT(DATETIME, CONVERT(CHAR(8), #y))
);
Result:
171
More importantly, fix the table. Stop storing dates as numbers. Store them as dates. If you get errors with this conversion, it's because your poor data choice has allowed bad data into the table. You can get around that - potentially - by writing the old version of TRY_CONVERT():
SELECT DATEDIFF
(
DAY,
CASE WHEN ISDATE(col1)=1 THEN CONVERT(DATETIME, col1) END,
CASE WHEN ISDATE(col2)=1 THEN CONVERT(DATETIME, col2) END
)
FROM
(
SELECT
col1 = CONVERT(CHAR(8), col1),
col2 = CONVERT(CHAR(8), col2)
FROM dbo.table
) AS x;
This will produce nulls for any row where there is garbage in either column. Here is a modification to your original query:
SELECT
[ITEM_NO] -- what table does this come from?
,[ITEM_DESC] -- what table does this come from?
,[HEADER_DUE_DATE] -- what table does this come from?
,D.WHS_SHORT_NAME AS [Warehouse] -- don't use single quotes for aliases!
,FPODtl.[PO_NO] AS [PO NUMBER]
,FPODtl.[PO_DATE] AS [Start]
,FPODtl.[PO_STATUS] AS [Status]
,FPODtl.[LINE_DOCK_DATE] AS [End]
,DATEDIFF
(
DAY,
CASE WHEN ISDATE(CONVERT(CHAR(8), FPODtl.PO_DATE)) = 1
THEN CONVERT(DATETIME, CONVERT(CHAR(8), FPODtl.PO_DATE)) END,
CASE WHEN ISDATE(CONVERT(CHAR(8), FPODtl.[LINE_DOCK_DATE])) = 1
THEN CONVERT(DATETIME, CONVERT(CHAR(8), FPODtl.[LINE_DOCK_DATE])) END
)
FROM [dbo].[DS_tblFactPODtl] AS FPODtl
INNER JOIN [dbo].[DS_tblDimWhs] AS D
ON FPODtl.WAREHOUSE = D.WAREHOUSE
INNER JOIN [dbo].[DS_tblFactPO] AS FPO
ON FPODtl.PO_NO = FPO.PO_NO
WHERE FPODtl.[PO_STATUS] = 'Closed'
AND FPODtl.[LINE_DOCK_DATE] <> 0;
If the date stored as a number is like this: 20130226 for today, then the simpler way to convert to DATE or DATETIME would be:
SELECT CONVERT(DATETIME,CONVERT(VARCHAR(8),NumberDate),112)
Here is a quick formula to create a date from parts :
DateAdd( Month, (( #Year - 1900 ) * 12 ) + #Month - 1, #Day - 1 )
Simply use substrings from your original field to extract #Year, #Month and #Day. For instance, if you have a numeric like 19531231 for december 31th, 1953, you could do :
DateAdd( Month, (( SubString(Cast(DateField As Varchar(8)), 1, 4) - 1900 ) * 12 ) +
SubString(Cast(DateField As Varchar(8)), 5, 2) - 1,
SubString(Cast(DateField As Varchar(8)), 7, 2) - 1 )

Updating Value Where Date

I'm looking to update the SOLUTION_ID where the CREATED_DATE is Greater Than or Equal To the given date.
What I have so far is updating all the records in the Table. CREATED_DATE has a smalldatetime datatype.
UPDATE [Database].[dbo].[TB__TABLE]
SET SOLUTION_ID = 1
WHERE CAST(CREATED_DATE AS datetime) >= 2011-05-08
Help greatly appreciated!
You want to quote the date;
WHERE CREATED_DATE >= '2011-05-08'
(unquoted 2011-05-08 is treated as a mathematical expression that evaluates to 1998)
Casting of column with smalldatetime is not required. Please see below the sample
create table #temp
(
dat smalldatetime,
Solution_ID int
)
insert into #temp(dat, Solution_ID)values(1, GETDATE())
insert into #temp(dat, Solution_ID)values(2, GETDATE()+1)
insert into #temp(dat, Solution_ID)values(3, GETDATE()+2)
select * from #temp
where dat >= 'Your date'
Update #temp
Set Solution_ID = 4
Where dat >= 'Your date'
UPDATE [Database].[dbo].[TB__TABLE]
SET SOLUTION_ID = 1
WHERE CAST(CREATED_DATE AS datetime) >= '2011-05-08'
No need for the cast at all.
And your query won't work exactly as you expect in that 2011 - 5 - 8 = 1998 which in sql server is 22/6/1905...
>= '2011-05-08'
Use the DATEDIFF function
DATEDIFF(dd, #GIVEN_DATE, CREATED_DATE) >= 0
This will return the number of days difference from your given date and your created date. When the function returns zero or greater, then the created_date is greater than the given date. No need for casting.
UPDATE [Database].[dbo].[TB__TABLE]
SET SOLUTION_ID = 1
WHERE DATEDIFF(dd, #GIVEN_DATE, CREATED_DATE) >= 0

Resources