USE [TEST]
GO
CREATE FUNCTION dbo.[SetDatefromStr]
(
-- Add the parameters for the function here
@Y int,
@mmm nvarchar(20)
)
RETURNS datetime
AS
BEGIN
-- Declare the return variable here
DECLARE @ret datetime
DECLARE @m int
-- Add the T-SQL statements to compute the return value here
SET @m = (SELECT CASE
when UPPER(@mmm) = 'JAN' then 1
when UPPER(@mmm) = 'FEB' then 2
when UPPER(@mmm) = 'MAR' then 3
when UPPER(@mmm) = 'APR' then 4
when UPPER(@mmm) = 'MAY' then 5
when UPPER(@mmm) = 'JUN' then 6
when UPPER(@mmm) = 'JUL' then 7
when UPPER(@mmm) = 'AUG' then 8
when UPPER(@mmm) = 'SEP' then 9
when UPPER(@mmm) = 'OCT' then 10
when UPPER(@mmm) = 'NOV' then 11
else 12
end )
SET @ret = (
SELECT cast(@Y as varchar)+right('00'+@m,2)+'01'
)
RETURN @ret
END
อยากป้อนข้อมูล SELECT SetDatefromStr(2020,'Apr') แล้วได้ 20200401 ออกมาหนะครับ
error : Conversion failed when converting date and/or time from character string.
ต้องแก้ตรงไหนหรือครับ หรือ ผม cast ผิดประเภท ขอบคุณครับ
create function MSSQL
USE [TEST]
GO
CREATE FUNCTION dbo.[SetDatefromStr]
(
-- Add the parameters for the function here
@Y int,
@mmm nvarchar(20)
)
RETURNS datetime
AS
BEGIN
-- Declare the return variable here
DECLARE @ret datetime
DECLARE @m int
-- Add the T-SQL statements to compute the return value here
SET @m = (SELECT CASE
when UPPER(@mmm) = 'JAN' then 1
when UPPER(@mmm) = 'FEB' then 2
when UPPER(@mmm) = 'MAR' then 3
when UPPER(@mmm) = 'APR' then 4
when UPPER(@mmm) = 'MAY' then 5
when UPPER(@mmm) = 'JUN' then 6
when UPPER(@mmm) = 'JUL' then 7
when UPPER(@mmm) = 'AUG' then 8
when UPPER(@mmm) = 'SEP' then 9
when UPPER(@mmm) = 'OCT' then 10
when UPPER(@mmm) = 'NOV' then 11
else 12
end )
SET @ret = (
SELECT cast(@Y as varchar)+right('00'+@m,2)+'01'
)
RETURN @ret
END
อยากป้อนข้อมูล SELECT SetDatefromStr(2020,'Apr') แล้วได้ 20200401 ออกมาหนะครับ
error : Conversion failed when converting date and/or time from character string.
ต้องแก้ตรงไหนหรือครับ หรือ ผม cast ผิดประเภท ขอบคุณครับ