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 7991171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:13:12+00:00 2026-06-04T13:13:12+00:00

Is it possible to create a function in SQL Server which I could use

  • 0

Is it possible to create a function in SQL Server which I could use in any database on the server, without adding the database prefix?

For example, with this function:

CREATE FUNCTION getDays (@date date)
RETURNS INT
AS
BEGIN

RETURN CASE WHEN MONTH(@date) IN (1, 3, 5, 7, 8, 10, 12) THEN 31
            WHEN MONTH(@date) IN (4, 6, 9, 11) THEN 30
            ELSE CASE WHEN (YEAR(@date) % 4    = 0 AND
                            YEAR(@date) % 100 != 0) OR
                           (YEAR(@date) % 400  = 0)
                      THEN 29
                      ELSE 28
                 END
       END

END
  • 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-04T13:13:14+00:00Added an answer on June 4, 2026 at 1:13 pm

    You can create the function in master (or some other permanent database), and then create a synonym in the model database:

    USE model;
    GO
    CREATE SYNONYM dbo.getDays FOR master.dbo.getDays;
    

    This will create a synonym to the function in any new databases, but for existing databases (or databases attached or restored in the future) you’ll need to copy the synonym there. This will allow you to reference the object with a two-part name in any database, while only having to store one copy of the code.

    As an aside, your code could be much more concise:

      RETURN (SELECT DATEPART(DAY, DATEADD(DAY, -1, 
         DATEADD(MONTH, 1, DATEADD(DAY, 1-DAY(@date), @date)))));
    

    So from the top:

    USE [master];
    GO
    DROP FUNCTION dbo.getDays;
    GO
    CREATE FUNCTION dbo.getDays
    (
        @date DATE
    )
    RETURNS INT
    AS
    BEGIN
        RETURN (SELECT DATEPART(DAY, DATEADD(DAY, -1, 
             DATEADD(MONTH, 1, DATEADD(DAY, 1-DAY(@date), @date)))));
    END
    GO
    

    Now to create a synonym for this in each database:

    DECLARE @sql NVARCHAR(MAX) = N'';
    
    SELECT @sql += CHAR(13) + CHAR(10) 
    + 'USE ' + QUOTENAME(name) + ';
    
    IF OBJECT_ID(''dbo.getDays'', ''FN'') IS NOT NULL
      DROP FUNCTION dbo.getDays;
    
    IF OBJECT_ID(''dbo.getDays'', ''SN'') IS NOT NULL
      DROP SYNONYM dbo.getDays
    
    CREATE SYNONYM dbo.getDays FOR master.dbo.getDays;'
     FROM sys.databases WHERE name <> 'master';
    
    PRINT @sql;
    
    EXEC sp_executesql @sql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a SQL Server function which returns a nullable string?
Is the following possible in SQL Server 2000? CREATE FUNCTION getItemType (@code varchar(18)) RETURNS
Possible Duplicate: Is there a way to create a SQL Server function to "join"
Possible Duplicate: Is there a way to create a SQL Server function to "join"
Is it possible to create a function which takes a pointer to another function?
Possible Duplicate: Is there a Max function in SQL Server that takes two values
I've got a (SQL Server 2005) database where I'd like to create views on-the-fly.
Is it possible to create and use a user defined function in a report
I have a user defined function defined in SQL Server. I want create a
Possible Duplicate: Function to Calculate Median in Sql Server I have a table like

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.