Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have created a table

CREATE TABLE myTable(
DateID INT PRIMARY KEY NOT NULL,
myDate DATE)

Then I want to populate the table with data from a staging table using SSIS. The problem is that I don't understand how to generate DateID based on the incoming value from a staging table. For example: after the staging table inserted 2020-12-12, I want my DateID to become 20201212 and so on.

I tried to google this issue but didn't find anything related to my case. (But I don't deny that I did it badly). How can I do it?

question from:https://stackoverflow.com/questions/65601231/populate-surrogate-datekey-based-on-date-in-a-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
321 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use a persisted computed column:

CREATE TABLE myTable (
     DateID AS (YEAR(myDate) * 10000 + MONTH(myDate) * 100 + DAY(myDate)) PERSISTED PRIMARY KEY,
     myDate DATE
);

Here is a db<>fiddle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...