SQL Select - Populate empty column rows

Is it possible to have SQL Select populate columns from other columns conditionally?  For example; I have 1 table with four columns as follows;

Current Week Folder Name   Current-Product-Count              Prior Week Folder Name   Prior-Product-Count
Abrasives                                            98                                                         Abrasives                 100
                                                                                                                           Filters                            9
Conditioner                                    2100                                      
Plugs                                                    31                                                         Plugs                           31

What I want is to populate the empty column rows with either the current or prior week column so the file looks like this:

Current Week Folder Name   Current-Product-Count              Prior Week Folder Name   Prior-Product-Count
Abrasives                                            98                                                         Abrasives                 100
Filters                                                    0                                                         Filters                            9
Conditioner                                    2100                                                        Conditioner                  0
Plugs                                                    31                                                         Plugs                           31

As you can see I want to fill in the blank columns with the value the prior or current week,
tmajor99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
You can use either the CASE statement, or a combination of NULLIF and ISNULL to do this.
NULLIF & ISNULL Example
SELECT ISNULL(NULLIF(RTRIM([Current Week Folder Name]), ''), [Prior Week Folder Name]) AS [Current Week Folder Name], 
ISNULL(NULLIF(Current-Product-Count, ''), Prior-Product-Count) AS Current-Product-Count
FROM <your table>

Open in new window

CASE Example
SELECT CASE WHEN [Current Week Folder Name] IS NULL OR RTRIM([Current Week Folder Name]) = '' THEN [Prior Week Folder Name] ELSE [Current Week Folder Name] END AS [Current Week Folder Name], 
CASE WHEN Current-Product-Count IS NULL OR RTRIM(Current-Product-Count) = '' THEN Prior-Product-Count ELSE Current-Product-Count END AS Current-Product-Count
FROM <your table>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Christal CISSPCommented:
Looks like the coalesce function is in order here:

select coalesce([Current Week Folder Name],[Prior Week Folder Name]  as ([Current Week Folder Name]
, coalesce( [Current-Product-Count],0) [Current-Product-Count]
, coalesce( [Prior Week Folder Name],[Current Week Folder Name]) [Prior Week Folder Name]
, coalesce( [Prior-Product-Count],0) [Prior-Product-Count]
ste5anSenior DeveloperCommented:
Do you really have such a table? Cause when not, then a pivot may be an option:

DECLARE @Sample TABLE
    (
      FolderName VARCHAR(255) ,
      ProductCount INT ,
      WeekNo INT
    );

INSERT  INTO @Sample
VALUES  ( 'Abrasives', 98, 15 ),
        ( 'Conditioner', 2100, 15 ),
        ( 'Plugs', 31, 15 ),
        ( 'Abrasives', 100, 14 ),
        ( 'Filters', 9, 14 ),
        ( 'Plugs', 31, 14 );

DECLARE @CurrentWeekNo INT = 15;

WITH    CurrentWeek
          AS ( SELECT   S.FolderName ,
                        S.ProductCount
               FROM     @Sample S
               WHERE    S.WeekNo = @CurrentWeekNo
             ),
        PreviousWeek
          AS ( SELECT   S.FolderName ,
                        S.ProductCount
               FROM     @Sample S
               WHERE    S.WeekNo = @CurrentWeekNo - 1
             )
    SELECT  *
    FROM    CurrentWeek C
            FULL OUTER JOIN PreviousWeek P ON P.FolderName = C.FolderName;

SELECT  P.FolderName ,
        ISNULL(P.[15], 0) AS CurrentWeekCount ,
        ISNULL(P.[14], 0) AS PreviosWeekCount
FROM    @Sample S PIVOT ( SUM(S.ProductCount) FOR S.WeekNo IN ( [15], [14] ) ) P;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.