Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:44:44+00:00 2026-06-15T16:44:44+00:00

How can I move a column of dates like below. User Date ==== ====

  • 0

How can I move a column of dates like below.

User              Date
====              ====
001               2012/12/01 09:00
001               2012/12/01 11:00
001               2012/12/01 12:00
001               2012/12/01 13:00
001..             ...        

and list them like below.

User  date         time1   time2   time3  time4    time5  
001   2012/12/01   09:00   11:00   12:00  13:00    14:00   

Thanks Bluefeet for the solution. I tested the script and got the following result.

2   2012/11/04  09:00:00.000    NULL    NULL    NULL    NULL    NULL    NULL
2   2012/11/08  NULL    09:00:00.000    18:00:00.000    NULL    NULL    NULL    NULL
2   2012/11/09  NULL    NULL    NULL    09:00:00.000    18:00:00.000    NULL    NULL
2   2012/11/10  NULL    NULL    NULL    NULL    NULL    09:00:00.000    18:00:00.000

The time seems to skip to the last null column for the same user at the next date? is there a way for me to arrange them in order starting from the first column for the next date for the same user?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:44:45+00:00Added an answer on June 15, 2026 at 4:44 pm

    You did not specify what RDBMS you are using but you previously tagged questions with a sql server tag so I will assume that is the database.

    You can perform this operation using the PIVOT function in SQL Server. There are two ways that you can do this either static where you hard-code all of the values or dynamic where the values are determined at run-time.

    Static Pivot:

    select *
    from 
    (
      select [user], convert(varchar(10), date, 111) date,
        right(convert(varchar(50), date, 121), 12) time
        , 'time'+cast(row_number() over(partition by [user], convert(varchar(10), date, 111) order by date) as varchar(10)) rn
      from yourtable
    ) src
    pivot
    (
      max(time)
      for rn in (time1, time2, time3, time4)
    ) piv
    

    See SQL Fiddle with Demo

    Dynamic Pivot:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME('time'+cast(row_number() over(partition by [user], convert(varchar(10), date, 111) order by date) as varchar(10))) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT [user], date,' + @cols + ' from 
                 (
                    select [user], convert(varchar(10), date, 111) date,
                      right(convert(varchar(50), date, 121), 12) time
                      , ''time''+cast(row_number() over(partition by [user], convert(varchar(10), date, 111) order by date) as varchar(10)) rn
                    from yourtable
                ) x
                pivot 
                (
                    max(time)
                    for rn in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Aggregate/CASE version:

    If for some reason you do not want to use the PIVOT function, then you can also use an aggregate function with a CASE statement.

    select [user], date,
      max(case when rn = 1 then time end) Time1,
      max(case when rn = 2 then time end) Time2,
      max(case when rn = 3 then time end) Time3,
      max(case when rn = 4 then time end) Time4
    from
    (
      select [user], convert(varchar(10), date, 111) date,
        right(convert(varchar(50), date, 121), 12) time
        , row_number() over(partition by [user], convert(varchar(10), date, 111) order by date)) rn
      from yourtable
    ) src
    group by [user], date
    

    See SQL Fiddle with Demo

    All three produce the same result:

    | USER |       DATE |        TIME1 |        TIME2 |        TIME3 |        TIME4 |
    ---------------------------------------------------------------------------------
    |  001 | 2012/12/01 | 09:00:00.000 | 11:00:00.000 | 12:00:00.000 | 13:00:00.000 |
    |  001 | 2012/12/02 | 09:00:00.000 | 11:00:00.000 | 12:00:00.000 | 13:00:00.000 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I can move date from Hive to MySQL?
How can I programmatically make an NSView so that the user can move its
I am encountering an odd issue whereby I can move items from one list
I have to make a user control where you can move it, but i
I'm currently trying to create a desktop like homepage where using can move around
I'm trying to create a 3 column page with jQuery where users can move
I can move my pointer up and down one line with my arrow key
In Vim, ^ can move the cursor to the first word (non white space)
I have an image that I want to move. I can move the element
In one of my iPhone projects, I have three views that you can move

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.