Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. date manipulation

date manipulation

Scheduled Pinned Locked Moved Database
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scottichrosaviakosmos
    wrote on last edited by
    #1

    declare @tbldate table(stdid int, sdate date, eddate date) insert into @tbldate values (100, '20100102', '20100505'), (101, '20100203', '20100302' ) i want my result set as check if day in date is 01 if not then make day as 01 and increment month my 1. i have the solution but thats in 2 temporary table and i want full solution in a single table.

    M A 2 Replies Last reply
    0
    • S scottichrosaviakosmos

      declare @tbldate table(stdid int, sdate date, eddate date) insert into @tbldate values (100, '20100102', '20100505'), (101, '20100203', '20100302' ) i want my result set as check if day in date is 01 if not then make day as 01 and increment month my 1. i have the solution but thats in 2 temporary table and i want full solution in a single table.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You mistake is that you are treating a date as a string - lesson 1: A DATE IS NOT A STRING. - learn this lesson now and it will save you a lot of pain in the future. You can use DATEADD within a loop. You still need to generate the sequential numbers (@day) 1-100, then you can do something like

      DECLARE
      @Day INT

      SET @Day = 1

      WHILE @Day < 101
      BEGIN
      SELECT DATEADD(d,@Day,GETDATE())
      SET @Day = @Day+1

      END

      Never underestimate the power of human stupidity RAH

      S 1 Reply Last reply
      0
      • M Mycroft Holmes

        You mistake is that you are treating a date as a string - lesson 1: A DATE IS NOT A STRING. - learn this lesson now and it will save you a lot of pain in the future. You can use DATEADD within a loop. You still need to generate the sequential numbers (@day) 1-100, then you can do something like

        DECLARE
        @Day INT

        SET @Day = 1

        WHILE @Day < 101
        BEGIN
        SELECT DATEADD(d,@Day,GETDATE())
        SET @Day = @Day+1

        END

        Never underestimate the power of human stupidity RAH

        S Offline
        S Offline
        scottichrosaviakosmos
        wrote on last edited by
        #3

        dear i also can do this but i by taking variable u can pass single value but i want to achieve this by query.

        M 1 Reply Last reply
        0
        • S scottichrosaviakosmos

          dear i also can do this but i by taking variable u can pass single value but i want to achieve this by query.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          scottichrosaviakosmos wrote:

          achieve this by query

          I don't think it can be done! The only work around I can think of is to create a UDF (function) which returns a table. Basically move the code into a function, have it return aresult set and use the result set to join to some thing else. I have one called GenDates and use it like

          Select * from Gendates(StartDate, Days)

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • S scottichrosaviakosmos

            declare @tbldate table(stdid int, sdate date, eddate date) insert into @tbldate values (100, '20100102', '20100505'), (101, '20100203', '20100302' ) i want my result set as check if day in date is 01 if not then make day as 01 and increment month my 1. i have the solution but thats in 2 temporary table and i want full solution in a single table.

            A Offline
            A Offline
            Alegria_Lee
            wrote on last edited by
            #5

            How about "case when" ?

            case when
            cast(right(@dates,2) as int) > 1
            then
            convert(char(8),dateadd(month,1,cast(left(@dates,6) + '01' as datetime(8))),112)
            else
            @dates
            end

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups