I have a table called clients
and I'm trying to split the value which contains underscores that is one column into multiple columns and I'm also trying to create a column that calculates the age of the
person.
Here is how the table looks like:
USERID Vendors (dobyr) login source
10bta yes 1976 yes google_hope
25cwd yes 1986 yes google_hln_1045
45tyj no 1990 no google_hln_4345
645io no 1960 no google
The goal is to have that look like this:
USERID Vendors (dobyr) login Source1 Source2 Source3 Age
10bta yes 1976 yes google hope null 44
25cwd yes 1986 yes google hln 1045 34
45tyj no 1992 no google hln 4345 28
645io no 1960 no google null null 30
Unfortunately, when using my code google
doesn't stay in source1
.
This is the code that I used:
select *, datepart(year, CURRENT_TIMESTAMP)-dobyr AS Age,
parsename(replace(source,'_','.'),1) AS source_1
,parsename(replace(source,'_','.'),2) AS source_2
,parsename(replace(source,'_','.'),3) AS source_3
,parsename(replace(source,'_','.'),4) AS source_4
FROM clients
Unfortunately using the code above I get a table like this where all google
aren't in column1
, but get something like this:
USERID Vendors (dobyr) login Source1 Source2 Source3 Age
10bta yes 1976 yes google hope null 44
25cwd yes 1986 yes hln google 1045 34
45tyj no 1992 no 4345 hln google 28
645io no 1960 no null null google 30