Learn Basic SQL | Create a Function | Case Expression

Publicado el: 27 agosto 2023
en el canal de: I made a Chart
5
1

Create a Function and learn the Case Expression
Get the Weekday of a Date or Datetime.
/*
Here the Statements and the Functions.
*/
select LoginID, BirthDate, HireDate from HumanResources.Employee


-- with a simple CASE expression
select LoginID, BirthDate,
case datepart(WEEKDAY, BirthDate)
when 1
then 'Sunday'
when 2
then 'Monday'
when 3
then 'Tuesday'
when 4
then 'Wednesday'
when 5
then 'Thursday'
when 6
then 'Friday'
when 7
then 'Saturday'
end as BirthDay
from HumanResources.Employee

-- with a searched CASE expression
select LoginID, BirthDate,
case
when (datepart(WEEKDAY, BirthDate) = 1)
then 'Sunday'
when (datepart(WEEKDAY, BirthDate) = 2)
then 'Monday'
when (datepart(WEEKDAY, BirthDate) = 3)
then 'Tuesday'
when (datepart(WEEKDAY, BirthDate) = 4)
then 'Wednesday'
when (datepart(WEEKDAY, BirthDate) = 5)
then 'Thursday'
when (datepart(WEEKDAY, BirthDate) = 6)
then 'Friday'
when (datepart(WEEKDAY, BirthDate) = 7)
then 'Saturday'
end as BirthDay
from HumanResources.Employee


-- =============================================
-- Author: I made a Chart
-- Create date: 2023-08-17
-- Description: Get the Weekday of a Date / Datetime
-- =============================================
Create or ALTER FUNCTION [dbo].[get_WeekDay]
(
@date Datetime
)
RETURNS varchar(9)
AS
BEGIN
DECLARE @weekDay varchar(9) = NULL

set @weekday = case datepart(WEEKDAY, @date)
when 1
then 'Sunday'
when 2
then 'Monday'
when 3
then 'Tuesday'
when 4
then 'Wednesday'
when 5
then 'Thursday'
when 6
then 'Friday'
when 7
then 'Saturday'
end

-- Return the result of the function
RETURN @weekday

END

-- =============================================
-- Author: I made a Chart
-- Create date: 2023-08-17
-- Description: Get the Weekday of a Date / Datetime in the Language of the Country
-- =============================================
Create or Alter FUNCTION [dbo].[get_WeekDay_localistion]
(
@date Datetime,
@country varchar(5)
)
RETURNS varchar(15)
AS
BEGIN
-- Declare and initialize
DECLARE @weekDay varchar(15) = NULL
-- Set Default to english
if @country not in ('en','de','es') set @country = 'en'

--switch in case of country for the localisation
if @country = 'en'
begin
set @weekday = case datepart(WEEKDAY, @date)
when 1
then 'Sunday'
when 2
then 'Monday'
when 3
then 'Tuesday'
when 4
then 'Wednesday'
when 5
then 'Thursday'
when 6
then 'Friday'
when 7
then 'Saturday'
end
end

if @country = 'de'
begin
set @weekday = case datepart(WEEKDAY, @date)
when 1
then 'Sonntag'
when 2
then 'Montag'
when 3
then 'Dienstag'
when 4
then 'Mitwoch'
when 5
then 'Donnerstag'
when 6
then 'Freitag'
when 7
then 'Samstag'
end
end

if @country = 'es'
begin
set @weekday = case datepart(WEEKDAY, @date)
when 1
then 'Domingo'
when 2
then 'Lunes'
when 3
then 'Martes'
when 4
then 'Miércoles'
when 5
then 'Jueves'
when 6
then 'Viernes'
when 7
then 'Sábado'
end
end

RETURN @weekday

END

______________________________________________
Source:

______________________________________________
HashTags:
#SQLAdventure #DataDive #NerdKnowledge #TechMagic #DatabaseMagic #SQLExploration #GeekyTech #CodeCrush #DataWizard #QueryQuest #TableTales #ByteSizedSQL #TechTidbits #CodeMagic #CuriousCoder #DigitalDiscoveries #TechEnthusiast #BytesOfWisdom #GeekLife #DiveIntoData #TechExploration
______________________________________________
Software:
Microsoft SQL Server Manager Studio 2018
Google Translate
OBS Studio
CapCut (FreeVersion)


En esta página del sitio puede ver el video en línea Learn Basic SQL | Create a Function | Case Expression de Duración hora minuto segunda en buena calidad , que subió el usuario I made a Chart 27 agosto 2023, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 5 veces y le gustó 1 a los espectadores. Disfruta viendo!