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,
sql azure datetime azure-sql-database ssms
add a comment |
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,
sql azure datetime azure-sql-database ssms
add a comment |
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,
sql azure datetime azure-sql-database ssms
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
sql azure datetime azure-sql-database ssms
asked Oct 28 at 1:22
harrisonthu
1441312
1441312
add a comment |
add a comment |
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;
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
add a comment |
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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