Last active
February 9, 2024 18:39
-
-
Save zonaro/b3f28a0ec67c0ae1b67ad571fc9801fb to your computer and use it in GitHub Desktop.
Retorna o primeiro ou o ultimo dia do mes a partir da MesAno especificada (4 digitos)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ============================================= | |
-- Author: Zonaro | |
-- Create date: 26/01/2022 | |
-- Description: Retorna o primeiro ou o ultimo dia do mes a partir da MesAno especificada (4 digitos) | |
-- ============================================= | |
CREATE OR ALTER FUNCTION [dbo].[DataMesAno] | |
( | |
@MesAno varchar(4), -- Exemplo '0123' => Janeiro/2023 | |
@FimDoMes bit = 0 -- 1 para 31-01-2023 | 0 para 01-01-2023 | |
) | |
RETURNS date | |
AS | |
BEGIN | |
set @MesAno = ISNULL(NULLIF(isnull(@MesAno,''), ''), cast(MONTH(getdate()) as varchar) + right(cast(Year(getdate()) as varchar),2)) | |
declare @data date = cast(FORMAT(CONVERT(INT,'01' + REPLICATE('0', 4 - LEN(@MesANo)) + RTrim(@MesANo)),'##-##-\2\0##') as date) | |
return IIF(isnull(@FimDoMes,0) = 0,@data,EOMONTH(@data)) | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment