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. Variables in Proceedures

Variables in Proceedures

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
2 Posts 2 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.
  • O Offline
    O Offline
    oskardiazdeleon
    wrote on last edited by
    #1

    Is is possible to add variables to functions in SQL Server. My issue is I am buildint a dashboard and I am sendint the variables directly from the dropdown menu. what I need to do is have the ability to modify the Datediff function. By changing the type. i.e datediff(@dateRange, getdate())=0 I need to have the @dateRange be a variable instead of (dd, ww, or yy). Any Ideas? thanks

    C 1 Reply Last reply
    0
    • O oskardiazdeleon

      Is is possible to add variables to functions in SQL Server. My issue is I am buildint a dashboard and I am sendint the variables directly from the dropdown menu. what I need to do is have the ability to modify the Datediff function. By changing the type. i.e datediff(@dateRange, getdate())=0 I need to have the @dateRange be a variable instead of (dd, ww, or yy). Any Ideas? thanks

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      oskardiazdeleon wrote:

      i.e datediff(@dateRange, getdate())=0

      That's not a legal call to DATEDIFF[^] See the linked documentation for what you actually need.

      oskardiazdeleon wrote:

      Any Ideas?

      CASE[^]

      CASE @dateRange WHEN 'dd' THEN DATEDIFF(dd, GETDATE(), @someOtherDate)
      WHEN 'ww' THEN DATEDIFF(ww, GETDATE(), @someOtherDate)
      WHEN 'yy' THEN DATEDIFF(yy, GETDATE(), @someOtherDate)
      ELSE 0 END

      You could then potentially wrap this in a function[^], however, since GETDATE() is non-deterministic it isn't possible to do what you want, but you could always pass GETDATE() into the function. e.g.

      CREATE FUNCTION DateDifference(@dateRange CHAR(2), @startDate DATETIME, @endDate DATETIME)
      RETURNS INT
      AS
      BEGIN
      RETURN CASE @dateRange WHEN 'dd' THEN DATEDIFF(dd, @startDate, @endDate)
      WHEN 'ww' THEN DATEDIFF(ww, @startDate, @endDate)
      WHEN 'yy' THEN DATEDIFF(yy, @startDate, @endDate)
      ELSE 0 END
      END

      It can be called like this:

      SELECT dbo.DateDifference(@dateRange, @someDate, GETDATE())

      Does this help?


      *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      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