Get data in different column

  • Whats the best way to put this in 3 different columns ?

    Select DATEPART(WW,GETDATE()) - 1 as Lastweek

    Select DATEPART(WW,GETDATE()) - 2 as LastWeek1

    Select DATEPART(WW,GETDATE()) - 3 as Lastweek2

  • sharonsql2013 (4/21/2015)


    Whats the best way to put this in 3 different columns ?

    Select DATEPART(WW,GETDATE()) - 1 as Lastweek

    Select DATEPART(WW,GETDATE()) - 2 as LastWeek1

    Select DATEPART(WW,GETDATE()) - 3 as Lastweek2

    Simple:

    isnert into table_name(col1,col2,col3)

    Select DATEPART(WW,GETDATE()) - 1

    , DATEPART(WW,GETDATE()) - 2

    , DATEPART(WW,GETDATE()) - 3

    Igor Micev,My blog: www.igormicev.com

  • I see...

    Create Table ##DateTable

    (

    Lastweek Int

    ,Lastweek1 Int

    ,Lastweek2 Int

    )

    Insert into ##DateTable(Lastweek,Lastweek1,Lastweek2)

    Values (DATEPART(WW,GETDATE()) - 1 ,DATEPART(WW,GETDATE()) - 2, DATEPART(WW,GETDATE()) - 3 )

    Select * from ##DateTable

    Thanks

  • SELECT LastWeek=d-1,LastWeek1=d-2,LastWeek2=d-3

    FROM (SELECT DATEPART(ww, GETDATE())) a (d);

    Edit: Do you understand why I suggest this instead of what has come before?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply