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. Other Discussions
  3. The Weird and The Wonderful
  4. Silly SQL

Silly SQL

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasequestion
5 Posts 4 Posters 1 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.
  • Z Offline
    Z Offline
    ZurdoDev
    wrote on last edited by
    #1

    SELECT MONTH + MONTH(DATEADD(qq,-1,DATEADD(qq,DATEDIFF(qq,0,@Today),0))) - 1
    FROM table1 t JOIN (SELECT TOP 3 row_number() over (ORDER BY fieldA) as MONTH FROM AnyTable) B on 1 = 1

    The first MONTH is actually the aliased field name from B. Why alias a field using the same name as a built-in function? Very confusing to follow. Although, unfortunately, it does actually work. On the plus side, joining to AnyTable using Top 3 Row_Number() is a clever way to guarantee 3 rows. Guess I could also put this in the Clever Code but it was written by the same dev.

    There are only 10 types of people in the world, those who understand binary and those who don't.

    M A 2 Replies Last reply
    0
    • Z ZurdoDev

      SELECT MONTH + MONTH(DATEADD(qq,-1,DATEADD(qq,DATEDIFF(qq,0,@Today),0))) - 1
      FROM table1 t JOIN (SELECT TOP 3 row_number() over (ORDER BY fieldA) as MONTH FROM AnyTable) B on 1 = 1

      The first MONTH is actually the aliased field name from B. Why alias a field using the same name as a built-in function? Very confusing to follow. Although, unfortunately, it does actually work. On the plus side, joining to AnyTable using Top 3 Row_Number() is a clever way to guarantee 3 rows. Guess I could also put this in the Clever Code but it was written by the same dev.

      There are only 10 types of people in the world, those who understand binary and those who don't.

      M Offline
      M Offline
      Mohibur Rashid
      wrote on last edited by
      #2

      ryanb31 wrote:

      Although, unfortunately, it does actually work

      Its an unfortunate issue that you posted the code saying unfortunately its work. Did you loose your job because of this unfortunate event?

      L Z 2 Replies Last reply
      0
      • M Mohibur Rashid

        ryanb31 wrote:

        Although, unfortunately, it does actually work

        Its an unfortunate issue that you posted the code saying unfortunately its work. Did you loose your job because of this unfortunate event?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Unfortunately, he didn't lose the unfortunate job.

        1 Reply Last reply
        0
        • M Mohibur Rashid

          ryanb31 wrote:

          Although, unfortunately, it does actually work

          Its an unfortunate issue that you posted the code saying unfortunately its work. Did you loose your job because of this unfortunate event?

          Z Offline
          Z Offline
          ZurdoDev
          wrote on last edited by
          #4

          It's bad practice to name something using the same name as a built in function, even if it does work.

          1 Reply Last reply
          0
          • Z ZurdoDev

            SELECT MONTH + MONTH(DATEADD(qq,-1,DATEADD(qq,DATEDIFF(qq,0,@Today),0))) - 1
            FROM table1 t JOIN (SELECT TOP 3 row_number() over (ORDER BY fieldA) as MONTH FROM AnyTable) B on 1 = 1

            The first MONTH is actually the aliased field name from B. Why alias a field using the same name as a built-in function? Very confusing to follow. Although, unfortunately, it does actually work. On the plus side, joining to AnyTable using Top 3 Row_Number() is a clever way to guarantee 3 rows. Guess I could also put this in the Clever Code but it was written by the same dev.

            There are only 10 types of people in the world, those who understand binary and those who don't.

            A Offline
            A Offline
            Andy Missico
            wrote on last edited by
            #5

            The bad practice is failing to write the statement explicitly. Why change MONTH when it's meant to be 'month'? The shame is not using "SELECT B.[MONTH] + MONTH..." Yet, even that is minor.

            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