Inline table-valued function without a parameter











up vote
0
down vote

favorite












I am still pretty new to Azure platform, and learning SQL language. I have a question about creating a user-defined inline Table-valued function (without need a parameter) on Azure platform. Since accessing to Azure platform on my SQL Server Management Studio, whenever I create a log file by using the system built-in function GetDate(), it shows the date and time in UTC time zone, but I want my current time zone (Easter Time Zone) which is 4 hours behind of UTC. So, I am wondering if anyone can please guide me how I can create a inline-table-valued function that does not need any parameter, and able to use it as GetDate() function by simply running as below:



            SELECT GetDate() 


This is my attempt and I know this is not correct, but at least I tried.



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[Azure_EST_GetDate()] (
)

RETURNS TABLE
AS
RETURN
(

SELECT DATEADD(hh,-4,GETDATE()) AS 'handate'
)
GO


Thanks in advance,










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am still pretty new to Azure platform, and learning SQL language. I have a question about creating a user-defined inline Table-valued function (without need a parameter) on Azure platform. Since accessing to Azure platform on my SQL Server Management Studio, whenever I create a log file by using the system built-in function GetDate(), it shows the date and time in UTC time zone, but I want my current time zone (Easter Time Zone) which is 4 hours behind of UTC. So, I am wondering if anyone can please guide me how I can create a inline-table-valued function that does not need any parameter, and able to use it as GetDate() function by simply running as below:



                SELECT GetDate() 


    This is my attempt and I know this is not correct, but at least I tried.



    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    CREATE FUNCTION [dbo].[Azure_EST_GetDate()] (
    )

    RETURNS TABLE
    AS
    RETURN
    (

    SELECT DATEADD(hh,-4,GETDATE()) AS 'handate'
    )
    GO


    Thanks in advance,










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am still pretty new to Azure platform, and learning SQL language. I have a question about creating a user-defined inline Table-valued function (without need a parameter) on Azure platform. Since accessing to Azure platform on my SQL Server Management Studio, whenever I create a log file by using the system built-in function GetDate(), it shows the date and time in UTC time zone, but I want my current time zone (Easter Time Zone) which is 4 hours behind of UTC. So, I am wondering if anyone can please guide me how I can create a inline-table-valued function that does not need any parameter, and able to use it as GetDate() function by simply running as below:



                  SELECT GetDate() 


      This is my attempt and I know this is not correct, but at least I tried.



      SET ANSI_NULLS ON
      GO
      SET QUOTED_IDENTIFIER ON
      GO

      CREATE FUNCTION [dbo].[Azure_EST_GetDate()] (
      )

      RETURNS TABLE
      AS
      RETURN
      (

      SELECT DATEADD(hh,-4,GETDATE()) AS 'handate'
      )
      GO


      Thanks in advance,










      share|improve this question













      I am still pretty new to Azure platform, and learning SQL language. I have a question about creating a user-defined inline Table-valued function (without need a parameter) on Azure platform. Since accessing to Azure platform on my SQL Server Management Studio, whenever I create a log file by using the system built-in function GetDate(), it shows the date and time in UTC time zone, but I want my current time zone (Easter Time Zone) which is 4 hours behind of UTC. So, I am wondering if anyone can please guide me how I can create a inline-table-valued function that does not need any parameter, and able to use it as GetDate() function by simply running as below:



                  SELECT GetDate() 


      This is my attempt and I know this is not correct, but at least I tried.



      SET ANSI_NULLS ON
      GO
      SET QUOTED_IDENTIFIER ON
      GO

      CREATE FUNCTION [dbo].[Azure_EST_GetDate()] (
      )

      RETURNS TABLE
      AS
      RETURN
      (

      SELECT DATEADD(hh,-4,GETDATE()) AS 'handate'
      )
      GO


      Thanks in advance,







      sql azure datetime azure-sql-database ssms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 28 at 1:22









      harrisonthu

      1441312




      1441312
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Consider using DATETIMEOFFSET and AT TIMEZONE instead of DATEADD. This will properly convert times across time zones and apply the appropriate time change rules. You can convert the DATETIMEOFFSET result to another type (datetime2 in this example) if needed.



          I don't think your intent is to include the parenthesis in the function name so I removed it in this example.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate] (
          )

          RETURNS TABLE
          AS
          RETURN
          (
          SELECT CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3)) AS handate
          );
          GO


          EDIT



          A table-valued function can generally be used in queries wherever a table may be specified. For example:



          SELECT handate FROM dbo.Azure_EST_GetDate();


          Table-valued functions are appropriate when more than one column or row is returned. You could alternatively use a scalar function for your need since a scalar (single) value is returned. Below is the equivalent scalar function.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate_Scalar] ()
          RETURNS datetime2(3)
          AS
          BEGIN
          RETURN CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3));
          END;
          GO


          This scalar function can be used instead of GETDATE() in Azure SQL Database to return a local time:



          SELECT dbo.Azure_EST_GetDate_Scalar() AS handate;





          share|improve this answer























          • Thanks for your detailed explanation, and it works as I expected. I appreciated it.
            – harrisonthu
            Oct 29 at 14:51










          • since this newly created (table-valued function) will replace GetDate() function, so I am just curious if I want to use it in my stored procedure, like returning the result as a scalar function, so can you guide me how I can change it/use it? Thanks !
            – harrisonthu
            Nov 22 at 0:41








          • 1




            @harrisonthu, I added usage examples to my answer along with a scalar function example.
            – Dan Guzman
            Nov 22 at 11:57











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53027616%2finline-table-valued-function-without-a-parameter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Consider using DATETIMEOFFSET and AT TIMEZONE instead of DATEADD. This will properly convert times across time zones and apply the appropriate time change rules. You can convert the DATETIMEOFFSET result to another type (datetime2 in this example) if needed.



          I don't think your intent is to include the parenthesis in the function name so I removed it in this example.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate] (
          )

          RETURNS TABLE
          AS
          RETURN
          (
          SELECT CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3)) AS handate
          );
          GO


          EDIT



          A table-valued function can generally be used in queries wherever a table may be specified. For example:



          SELECT handate FROM dbo.Azure_EST_GetDate();


          Table-valued functions are appropriate when more than one column or row is returned. You could alternatively use a scalar function for your need since a scalar (single) value is returned. Below is the equivalent scalar function.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate_Scalar] ()
          RETURNS datetime2(3)
          AS
          BEGIN
          RETURN CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3));
          END;
          GO


          This scalar function can be used instead of GETDATE() in Azure SQL Database to return a local time:



          SELECT dbo.Azure_EST_GetDate_Scalar() AS handate;





          share|improve this answer























          • Thanks for your detailed explanation, and it works as I expected. I appreciated it.
            – harrisonthu
            Oct 29 at 14:51










          • since this newly created (table-valued function) will replace GetDate() function, so I am just curious if I want to use it in my stored procedure, like returning the result as a scalar function, so can you guide me how I can change it/use it? Thanks !
            – harrisonthu
            Nov 22 at 0:41








          • 1




            @harrisonthu, I added usage examples to my answer along with a scalar function example.
            – Dan Guzman
            Nov 22 at 11:57















          up vote
          1
          down vote



          accepted










          Consider using DATETIMEOFFSET and AT TIMEZONE instead of DATEADD. This will properly convert times across time zones and apply the appropriate time change rules. You can convert the DATETIMEOFFSET result to another type (datetime2 in this example) if needed.



          I don't think your intent is to include the parenthesis in the function name so I removed it in this example.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate] (
          )

          RETURNS TABLE
          AS
          RETURN
          (
          SELECT CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3)) AS handate
          );
          GO


          EDIT



          A table-valued function can generally be used in queries wherever a table may be specified. For example:



          SELECT handate FROM dbo.Azure_EST_GetDate();


          Table-valued functions are appropriate when more than one column or row is returned. You could alternatively use a scalar function for your need since a scalar (single) value is returned. Below is the equivalent scalar function.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate_Scalar] ()
          RETURNS datetime2(3)
          AS
          BEGIN
          RETURN CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3));
          END;
          GO


          This scalar function can be used instead of GETDATE() in Azure SQL Database to return a local time:



          SELECT dbo.Azure_EST_GetDate_Scalar() AS handate;





          share|improve this answer























          • Thanks for your detailed explanation, and it works as I expected. I appreciated it.
            – harrisonthu
            Oct 29 at 14:51










          • since this newly created (table-valued function) will replace GetDate() function, so I am just curious if I want to use it in my stored procedure, like returning the result as a scalar function, so can you guide me how I can change it/use it? Thanks !
            – harrisonthu
            Nov 22 at 0:41








          • 1




            @harrisonthu, I added usage examples to my answer along with a scalar function example.
            – Dan Guzman
            Nov 22 at 11:57













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Consider using DATETIMEOFFSET and AT TIMEZONE instead of DATEADD. This will properly convert times across time zones and apply the appropriate time change rules. You can convert the DATETIMEOFFSET result to another type (datetime2 in this example) if needed.



          I don't think your intent is to include the parenthesis in the function name so I removed it in this example.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate] (
          )

          RETURNS TABLE
          AS
          RETURN
          (
          SELECT CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3)) AS handate
          );
          GO


          EDIT



          A table-valued function can generally be used in queries wherever a table may be specified. For example:



          SELECT handate FROM dbo.Azure_EST_GetDate();


          Table-valued functions are appropriate when more than one column or row is returned. You could alternatively use a scalar function for your need since a scalar (single) value is returned. Below is the equivalent scalar function.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate_Scalar] ()
          RETURNS datetime2(3)
          AS
          BEGIN
          RETURN CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3));
          END;
          GO


          This scalar function can be used instead of GETDATE() in Azure SQL Database to return a local time:



          SELECT dbo.Azure_EST_GetDate_Scalar() AS handate;





          share|improve this answer














          Consider using DATETIMEOFFSET and AT TIMEZONE instead of DATEADD. This will properly convert times across time zones and apply the appropriate time change rules. You can convert the DATETIMEOFFSET result to another type (datetime2 in this example) if needed.



          I don't think your intent is to include the parenthesis in the function name so I removed it in this example.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate] (
          )

          RETURNS TABLE
          AS
          RETURN
          (
          SELECT CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3)) AS handate
          );
          GO


          EDIT



          A table-valued function can generally be used in queries wherever a table may be specified. For example:



          SELECT handate FROM dbo.Azure_EST_GetDate();


          Table-valued functions are appropriate when more than one column or row is returned. You could alternatively use a scalar function for your need since a scalar (single) value is returned. Below is the equivalent scalar function.



          CREATE FUNCTION [dbo].[Azure_EST_GetDate_Scalar] ()
          RETURNS datetime2(3)
          AS
          BEGIN
          RETURN CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime2(3));
          END;
          GO


          This scalar function can be used instead of GETDATE() in Azure SQL Database to return a local time:



          SELECT dbo.Azure_EST_GetDate_Scalar() AS handate;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 11:56

























          answered Oct 28 at 2:11









          Dan Guzman

          22.4k31539




          22.4k31539












          • Thanks for your detailed explanation, and it works as I expected. I appreciated it.
            – harrisonthu
            Oct 29 at 14:51










          • since this newly created (table-valued function) will replace GetDate() function, so I am just curious if I want to use it in my stored procedure, like returning the result as a scalar function, so can you guide me how I can change it/use it? Thanks !
            – harrisonthu
            Nov 22 at 0:41








          • 1




            @harrisonthu, I added usage examples to my answer along with a scalar function example.
            – Dan Guzman
            Nov 22 at 11:57


















          • Thanks for your detailed explanation, and it works as I expected. I appreciated it.
            – harrisonthu
            Oct 29 at 14:51










          • since this newly created (table-valued function) will replace GetDate() function, so I am just curious if I want to use it in my stored procedure, like returning the result as a scalar function, so can you guide me how I can change it/use it? Thanks !
            – harrisonthu
            Nov 22 at 0:41








          • 1




            @harrisonthu, I added usage examples to my answer along with a scalar function example.
            – Dan Guzman
            Nov 22 at 11:57
















          Thanks for your detailed explanation, and it works as I expected. I appreciated it.
          – harrisonthu
          Oct 29 at 14:51




          Thanks for your detailed explanation, and it works as I expected. I appreciated it.
          – harrisonthu
          Oct 29 at 14:51












          since this newly created (table-valued function) will replace GetDate() function, so I am just curious if I want to use it in my stored procedure, like returning the result as a scalar function, so can you guide me how I can change it/use it? Thanks !
          – harrisonthu
          Nov 22 at 0:41






          since this newly created (table-valued function) will replace GetDate() function, so I am just curious if I want to use it in my stored procedure, like returning the result as a scalar function, so can you guide me how I can change it/use it? Thanks !
          – harrisonthu
          Nov 22 at 0:41






          1




          1




          @harrisonthu, I added usage examples to my answer along with a scalar function example.
          – Dan Guzman
          Nov 22 at 11:57




          @harrisonthu, I added usage examples to my answer along with a scalar function example.
          – Dan Guzman
          Nov 22 at 11:57


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53027616%2finline-table-valued-function-without-a-parameter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo