T-SQL Find a date in multiple date ranges - sql-server

I have three tables that I need to get information from. One table has a code that I need to insert into another table. A third table ties these together.
Table one has a person ID number, a session number, a transaction date and a code. Some of the codes in this table are blank, so they need to be updated.
Table two has the same information as table one with an extra field of PaymentType.
Table three has the same data as table one with a PaymentType number, a date and a code to update to table one where the code is null.
The TransactionDate in table one need to match the Date or within date ranges in table three.
If the dates in table three are 2015-05-03, 2015-05-14 and 2015-05-30, how can I find a specific date within either of those ranges? If the date in table one is 2015-05-10, I need to find which date range it is in so that I can update table three with the proper code.
CREATE TABLE TableThree (
PersonNumber VARCHAR(8) NOT NULL
,SessionNumber VARCHAR(3) NOT NULL
,transaction_Dates DATE NOT NULL
,Code VARCHAR(3) NOT NULL
);
INSERT INTO
TableThree (PersonNumber, SessionNumber, transaction_Dates, Code)
VALUES
('ABC03854','590','2015-12-21','AAA')
,('ABC03862','591','2015-12-10','AAA')
,('ABC03862','591','2015-12-21','BBB')
,('ABC03893','595','2015-12-02','FFF')
,('ABC03893','595','2015-12-30','AAA')
,('ABC03970','597','2015-12-21','MMM')
,('ABC03976','598','2015-12-10','FFF')
,('ABC08931','519','2015-12-10','FFF')
,('ABC08931','519','2015-12-21','AAA')
,('ABC08931','519','2015-12-30','MMM');
CREATE TABLE mytable (
PersonNumber VARCHAR(8) NOT NULL
,SessionNumber INTEGER NOT NULL
,transaction_date VARCHAR(24) NOT NULL
,CODE VARCHAR(5) NOT NULL
);
INSERT INTO
mytable (PersonNumber,SessionNumber,transaction_date,CODE)
VALUES
('ABC03854',590,'2014-12-23 00:00:00.000',NULL)
,('ABC03862',591,'2014-12-16 00:00:00.000',NULL)
,('ABC03893',595,'2014-12-16 00:00:00.000',NULL)
,('ABC08931',519,'2015-12-22 00:00:00.000',NULL);

Related

Add month column in SQL Server

How to add a column month? The month must be extracted from a date column in the same table. I know that this extracts the month but how to add it please
SELECT
MONTH(columndate) AS month
FROM
TableTIME;
DROP TABLE IF EXISTS dbo.Temp;
CREATE TABLE dbo.Temp
(
Temp_ID TINYINT IDENTITY(1,1) NOT NULL PRIMARY KEY,
TempDate DATE NOT NULL DEFAULT GETDATE()
)
GO
INSERT dbo.Temp(TempDate)
VALUES(GETDATE()),('20120430');
GO
ALTER TABLE dbo.Temp
ADD TempMonth AS DATEPART(MM,Tempdate) PERSISTED
GO
SELECT * FROM dbo.Temp;
you only need add a int column, you also can add check rule on this column, For example:
ALTER TABLE dbo.MyTable ADD mn INT CHECK(mn BETWEEN 1 AND 12)
If you really need to have the month column, just add an integer column to the table and do and update with the extracted month:
update TableTIME set monthcolumn = MONTH(columndate)
It's not clear what is the purpose of that but, if this month will never change, I suggest you to select the MONTH(columndate) only when you need it or the full date and extract the month in the business logic.

Unable to change varchar column to date type

I have a table in my SQL Server 2017 database that contains a varchar(10) column whose values all conform precisely to date. Here are some sample values, but there are many more:
2014-06-30
2004-05-06
2014-06-30
2001-08-18
2004-05-06
2009-06-30
2001-08-18
2004-05-06
2009-06-30
2001-08-18
2004-05-06
I am trying to convert this column to a date:
alter table SourceTable1 alter column END_DATE date;
...but I always get this error:
Conversion failed when converting date and/or time from character
string.
I have examined the data and 100% of the column values conform to the date type. How can I change the column to date type?
I did a simple test and it worked:
create table #t (t varchar(10))
insert into #t values ('2001-08-18')
alter table #t alter column t date
Another with something that looks like date but it isn't (got the same error as you):
create table #tt (t varchar(10))
insert into #tt values ('2001-08-32')
alter table #tt alter column t date
For sure you have a row that contains a varchar(10) that is not convertable. You can find this values by using a select like this:
select try_cast(yourColumn as date) as tryCastyourColumn, yourColumn
from Yourtable
where try_cast(yourColumn as date) is null
I agree with M. Kanarkowski that your examples seem fine, but you probably are missing something. Since you have the 2017 version, run this to check what's wrong:
select END_DATE
from SourceTable1
where END_DATE is not null and try_convert(date,END_DATE) is null

Create partition function based on join with other table

I have 2 tables:
TableA
(
Id uniqueidentifier,
Date datetime
)
TabelB
(
Body XML,
KeyA uniqueidentifier
)
And I want to create partition for both tables based on date field in TableA.
I checked documentation but can't find any clue about it. Is it possible to do?

INSERT INTO, but retain blank columns?

In a stored procedure I'm creating...
IF OBJECT_ID('tempdb..##SUBOSummary') IS NOT NULL
/*Then it exists*/
DROP TABLE ##SUBOSummary
CREATE TABLE ##SUBOSummary
(
--RowID int not null identity(1,1) primary key,
RowNum int IDENTITY(1,1) NOT NULL,
Dept nvarchar(25) NOT NULL,
Last24 int,
WTD int,
MoTD int,
YTD int
)
I then want to add a series of data from another view, and I'll then fill the rest of the data rows in seperately:
INSERT INTO ##SUBOSummary SELECT DISTINCT vw_EmployeeDepartment.Dept FROM vw_EmployeeDepartment
I'm getting the following error:
Column name or number of supplied values does not match table definition.
I'm hazarding a guess that this is because I'm only populating a single field by the insert statement.
Is there a better way to acheive this?
The error, in truth, is quite clear here:
Column name or number of supplied values does not match table definition.
In your INSERT statement you're implying you want to insert values into all the columns (as you're not specifying which one), yet you're only supplying one column in your SELECT, so where does that value go? SQL Server has no idea.
You need to supply your column(s) in your INSERT clause:
INSERT INTO ##SUBOSummary (Dept)
SELECT DISTINCT vw_EmployeeDepartment.Dept
FROM vw_EmployeeDepartment;
On a different note, however, don't use a Global Temporary Table, they often perform very poorly.

Recursive stored procedure - insert sql

I am trying to generate a recursive stored procedure for the insert of a table of some hierarchical data.
I have three tables with the following design
Table A
Id. uniqueidentifier
Name
Acronym
Table B
Id. uniqueidentifier
ParentId uniqueidentifier (references TableA)
ChildId. uniqueidentifier (references TableA)
Datestart datetime
Dateend. datetime
Table C
Id uniqueidentifier
Datestart datetime
Dateend datetime
AId uniqueidentifier (references TableA)
Left int
Right int
I am trying to do an insert into TableC with the values from the other two tables. TableA has the master data, and TableB has the parent child associations mentioned as per the date range.
As in as per quarter the parent child relationship can change, hence the date start and date end columns.
I want to write a recursive stored procedure with common table expression to do the inserts by combining the table A and B. I have tried searching for help online but most of the link has parent child relationship mentioned in the same table but not like my scenario.
Please let me know if somebody can help me.

Resources