如何在sql server中的波斯语中转换datetime

时间:2011-07-15 12:04:25

标签: sql-server-2008 datetime-format

我想在sql server中将我的日期时间转换为波斯语日期时间。我的日期时间是MM / DD / YYYY格式。在sql server中是否有任何功能可以执行此操作,因为当我想要hijri datetime时我会使用此

 select CONVERT(VARCHAR(40), getdate(), 131) //Output is 14/08/1432 5:02:01:197PM 

我正在使用sql server 2008

11 个答案:

答案 0 :(得分:10)

我知道现在为时已晚,但也许对我这样的人有用。

您应该为此转换编写一个SQL函数,如下所示: Converting Gregorian to Persian Date

然后像这样使用它:

SELECT dbo.[UDF_Gregorian_To_Persian]('2013-08-24')

答案 1 :(得分:8)

我知道回答这个问题为时已晚,但我已经提交了我长时间使用的功能而没有任何错误,我见过的所有其他方法都存在闰年问题:

CREATE FUNCTION [CalculatePersianDate] ( @intDate DATETIME )
RETURNS NVARCHAR(max)
BEGIN

DECLARE @shYear AS INT ,@shMonth AS INT ,@shDay AS INT ,@intYY AS INT ,@intMM AS INT ,@intDD AS INT ,@Kabiseh1 AS INT ,@Kabiseh2 AS INT ,@d1 AS INT ,@m1 AS INT, @shMaah AS NVARCHAR(max),@shRooz AS NVARCHAR(max),@DayCnt AS INT
DECLARE @DayDate AS NVARCHAR(max)

SET @intYY = DATEPART(yyyy, @intDate)

IF @intYY < 1000 SET @intYY = @intYY + 2000

SET @intMM = MONTH(@intDate)
SET @intDD = DAY(@intDate)
SET @shYear = @intYY - 622
SET @DayCnt = 5

IF ( ( @intYY - 1992 ) % 4 = 0) SET @Kabiseh1 = 0 ELSE SET @Kabiseh1 = 1

IF ( ( @shYear - 1371 ) % 4 = 0) SET @Kabiseh2 = 0 ELSE SET @Kabiseh2 = 1

SET @m1 = 1
SET @d1 = 1
SET @shMonth = 10
SET @shDay = 11

IF ( ( @intYY - 1993 ) % 4 = 0 ) SET @shDay = 12


WHILE ( @m1 != @intMM ) OR ( @d1 != @intDD )
BEGIN

  SET @d1 = @d1 + 1
  SET @DayCnt = @DayCnt + 1

  IF 
  (@d1 = 32 AND (@m1 = 1 OR @m1 = 3 OR @m1 = 5 OR @m1 = 7 OR @m1 = 8 OR @m1 = 10 OR @m1 = 12))
  OR
  (@d1 = 31 AND (@m1 = 4 OR @m1 = 6 OR @m1 = 9 OR @m1 = 11))
  OR
  (@d1 = 30 AND @m1 = 2 AND @Kabiseh1 = 1)
  OR
  (@d1 = 29 AND @m1 = 2 AND @Kabiseh1 = 0)
  BEGIN
    SET @m1 = @m1 + 1
    SET @d1 = 1
  END

  IF @m1 > 12
  BEGIN
    SET @intYY = @intYY + 1
    SET @m1 = 1
  END

  IF @DayCnt > 7 SET @DayCnt = 1

 SET @shDay = @shDay + 1

  IF
  (@shDay = 32 AND @shMonth < 7)
  OR
  (@shDay = 31 AND @shMonth > 6 AND @shMonth < 12)
  OR
  (@shDay = 31 AND @shMonth = 12 AND @Kabiseh2 = 1)
  OR
  (@shDay = 30 AND @shMonth = 12 AND @Kabiseh2 = 0)
  BEGIN
    SET @shMonth = @shMonth + 1
    SET @shDay = 1
  END

  IF @shMonth > 12
  BEGIN
    SET @shYear = @shYear + 1
    SET @shMonth = 1
  END

END

IF @shMonth=1 SET @shMaah=N'فروردین'
IF @shMonth=2 SET @shMaah=N'اردیبهشت'
IF @shMonth=3 SET @shMaah=N'خرداد'
IF @shMonth=4 SET @shMaah=N'تیر'
IF @shMonth=5 SET @shMaah=N'مرداد'
IF @shMonth=6 SET @shMaah=N'شهریور'
IF @shMonth=7 SET @shMaah=N'مهر'
IF @shMonth=8 SET @shMaah=N'آبان'
IF @shMonth=9 SET @shMaah=N'آذر'
IF @shMonth=10 SET @shMaah=N'دی'
IF @shMonth=11 SET @shMaah=N'بهمن'
IF @shMonth=12 SET @shMaah=N'اسفند'

IF @DayCnt=1 SET @shRooz=N'شنبه'
IF @DayCnt=2 SET @shRooz=N'یکشنبه'
IF @DayCnt=3 SET @shRooz=N'دوشنبه'
IF @DayCnt=4 SET @shRooz=N'سه‌شنبه'
IF @DayCnt=5 SET @shRooz=N'چهارشنبه'
IF @DayCnt=6 SET @shRooz=N'پنجشنبه'
IF @DayCnt=7 SET @shRooz=N'جمعه'

--SET @DayDate = @shRooz + " " + LTRIM(STR(@shDay,2)) + " " + @shMaah + " " + STR(@shYear,4)
--پنجشنبه 17 اردیبهشت 1394

/*
SET @DayDate = LTRIM(STR(@shDay,2)) + " " + @shMaah + " " + STR(@shYear,4)
--17 اردیبهشت 1394

SET @DayDate = STR(@shYear,4) + "/"+LTRIM(STR(@shMonth,2)) + "/" + LTRIM(STR(@shDay,2))
--1394/2/17


--1394/02/17
*/
SET @DayDate = REPLACE(RIGHT(STR(@shYear, 4), 4), ' ', '0') + '/'+ REPLACE(STR(@shMonth, 2), ' ', '0') + '/' + REPLACE(( STR(@shDay,2) ), ' ', '0')
RETURN @DayDate
END

自定义函数的结果非常简单。 采自:this page

答案 2 :(得分:2)

开箱即用,没有。

您必须编写自己的UDF,但CodePlexanother上有一个

答案 3 :(得分:2)

convert Jalali to Georgian and Georgian to Jalali

试试这个链接。您可以在SQL服务器中添加一些功能,并在需要时调用它们。

答案 4 :(得分:2)

尝试一下:

select format(getdate() , 'yyyy/MM/dd', 'fa-ir')

答案 5 :(得分:1)

SQL Server 2016中的最佳方法

示例:

SELECT FORMAT(GETDATE(), 'yyyy/MM/dd-HH:mm:ss', 'fa')

答案:

1398/10/08-05:37:59

答案 6 :(得分:0)

我认为最好的解决方案是使用SQLCLR-Jalali-Date-Utility。它具有简单明了的安装指南和易于使用的功能。此外,您可以无限制地定义转换日期的格式。实际上,您可以使用standard time formatting来定义转换日期的形状。

在GitHub页面中提供了几个示例。

0101111101100001

0110000101011111
0011000010101111

1010111100110000

答案 7 :(得分:0)

您可以使用此功能将其转换为shamsi。

第一个功能。 创建函数[dbo]。[ToPersianDate](@ dt [datetime]) 使用执行者调用返回nvarchar 如 外部名称[PersianSQLFunctions]。[UserDefinedFunctions]。[ToPersianDate]

GO

第二个功能。 设置ANSI_NULLS OFF 开始

将QUOTED_IDENTIFIER设置为OFF 开始

创建功能[dbo]。[ToPersianDate](@ dt [datetime]) 使用执行者调用返回nvarchar 如 外部名称[PersianSQLFunctions]。[UserDefinedFunctions]。[ToPersianDate]

GO

第三功能。 创建功能[dbo]。[fnToShamsiDate] (     @d DateTime ) 返回NVARCHAR(10) 如 开始     -在这里声明返回变量     十进制@RV NVARCHAR(10)     如果(@d)为NULL,则返回NULL;     其他选择@RV = DBO.ToPersianDate(@D);     -返回函数结果     返回@RV

END

GO

您还可以通过此功能找到假的月份

创建函数[dbo]。[fnGetShamsiMonth] (     @GregorianDate日期 ) 返回nvarchar(2) 如 开始     声明@ShamsiMonth nvarchar(2),@ShamsiDate nvarchar(10);

set @ShamsiDate = confsys.dbo.fnToShamsiDate(@GregorianDate);

set @ShamsiMonth = SUBSTRING(@ShamsiDate,6,2);

return @ShamsiMonth

结束 开始

示例

选择confsys.dbo.fnToShamsiDate(getdate()) 结果是1397/12/29

获得假月份 选择confsys.dbo.fnGetShamsiMonth(GETDATE());

答案 8 :(得分:0)

这是 SQL 2016+ 中的波斯日历函数

ALTER FUNCTION [dbo].[PCalendar](@date datetime)
RETURNS @ret TABLE (
   ly int,
    y int,
    m int,
    mname nvarchar(15),
    d int,
    dy int,
    dw int,
    dname nvarchar(10),
    hh int,
    mm int,
    ss int,
    mss int,
    dt datetime,
    t nvarchar(3))
as
BEGIN
    DECLARE @format varchar(19);
    set @format = 'yyyy/MM/dd HH:mm:ss';
    DECLARE @y int;
    DECLARE @m int;
    DECLARE @d int;
    DECLARE @dy int;
    DECLARE @dw int;
    DECLARE @hh int;
    DECLARE @mm int;
    DECLARE @ss int;
    DECLARE @ms int;
    DECLARE @ldt varchar(8);
    set @y = DATEPART(YEAR, FORMAT(@date, @format, 'fa')) ;
    set @m = DATEPART(MONTH, FORMAT(@date, @format, 'fa'));
    set @d = DATEPART(DAY, FORMAT(@date, @format, 'fa')) ;
    set @dy = DATEPART(DAYOFYEAR, FORMAT(@date, @format, 'fa'));
    set @dw = DATEPART(WEEKDAY, FORMAT(@date,@format, 'fa'));
    set @hh = DATEPART(HOUR, @date) ;
    set @mm = DATEPART(MINUTE, @date) ;
    set @ss = DATEPART(SECOND, @date);
    set @ms = DATEPART(MILLISECOND, @date);
    set @ldt =DATEPART(year, FORMAT(@date, @format, 'en'));
    DECLARE @_w nvarchar(10);

    set @_w = CASE
        WHEN @dw=1 THEN N'جمعه'
        WHEN @dw=2 THEN N'شنبه'
        WHEN @dw=3 THEN N'یکشنبه'
        WHEN @dw=4 THEN N'دوشنبه'
        WHEN @dw=5 THEN N'سه شنبه'
        WHEN @dw=6 THEN N'چهارشنبه'
        ELSE N'پنج شنبه'
    END;    
DECLARE @_m nvarchar(15);
set @_m = CASE
    WHEN @m=1 THEN N'فروردین'
    WHEN @m=2 THEN N'اردیبهشت'
    WHEN @m=3 THEN N'خرداد'
    WHEN @m=4 THEN N'تیر'
    WHEN @m=5 THEN N'مرداد'
    WHEN @m=6 THEN N'شهریور'
    WHEN @m=7 THEN N'مهر'
    WHEN @m=8 THEN N'آبان'
    WHEN @m=9 THEN N'آذر'
    WHEN @m=10 THEN N'دی'
    WHEN @m=11 THEN N'بهمن'
    ELSE N'اسفند'
END;    
set @_m = @_m+N' ماه';
INSERT INTO @ret 
    SELECT 
    IIF(@y % 33 in (1,5,9,13,17,22,26,30) , 1 , 0) as ly, 
    @y as y, 
    @m as m, 
    @_m as mname, 
    @d as d, 
    @dy as dy, 
    @dw as dw, 
    @_w as dname,
    @hh as hh, 
    @mm as mm, 
    @ss as ss, 
    @ms as mss, 
    @date as dt,
    IIF(@hh > 12 , N'ب.ظ','ق.ظ') as t;
RETURN;

结束

答案 9 :(得分:0)

选择格式(GETDATE(), 'yyyy/MM/dd-HH:mm:ss', 'fa')
1400/02/08-05:08:51
SELECT cast( FORMAT(GETDATE(), 'yyyyMMdd', 'fa') as int)
14000208

答案 10 :(得分:-1)

CREATE   FUNCTION [dbo].[MITSH] (@MDate  DateTime)  
RETURNS Varchar(10)
AS  
BEGIN 
   DECLARE @SYear  as Integer
   DECLARE @SMonth  as Integer
   DECLARE @my_mah varchar(2)
   declare @my_day varchar(2)
   DECLARE @SDay  as Integer
   DECLARE @AllDays  as float
   DECLARE @ShiftDays  as float
   DECLARE @OneYear  as float
   DECLARE @LeftDays  as float
   DECLARE @YearDay  as Integer
   DECLARE @Farsi_Date  as Varchar(100) 
   SET @MDate=@MDate-CONVERT(char,@MDate,114)

  SET @ShiftDays=466699   +2
  SET @OneYear= 365.24199


   SET @SYear = 0
   SET @SMonth = 0
   SET @SDay = 0
   SET @AllDays  = CAst(@Mdate as Real)

   SET @AllDays = @AllDays + @ShiftDays

  SET @SYear = (@AllDays / @OneYear) --trunc
  SET @LeftDays = @AllDays - @SYear * @OneYear

  if (@LeftDays < 0.5)
  begin
    SET @SYear=@SYear+1
    SET @LeftDays = @AllDays - @SYear * @OneYear
  end;

  SET @YearDay = @LeftDays --trunc
  if (@LeftDays - @YearDay) >= 0.5 
    SET @YearDay=@YearDay+1

  if ((@YearDay / 31) > 6 )
  begin
    SET @SMonth = 6
    SET @YearDay=@YearDay-(6 * 31)
    SET @SMonth= @SMonth+( @YearDay / 30)
    if (@YearDay % 30) <> 0 
      SET @SMonth=@SMonth+1
    SET @YearDay=@YearDay-((@SMonth - 7) * 30)
  end 
  else
  begin
    SET @SMonth = @YearDay / 31
    if (@YearDay % 31) <> 0 
      SET @SMonth=@SMonth+1 
    SET @YearDay=@YearDay-((@SMonth - 1) * 31)
  end
  SET @SDay = @YearDay
  SET @SYear=@SYear+1


if @SMonth <10 begin 
   set @my_mah='0'+str(@SMonth,1)
end else begin
    set @my_mah = str(@SMonth,2)
end   
if @sday <10 begin
   set @my_day='0'+str(@Sday,1)
end else begin
    set @my_day = str(@Sday,2)
end   

 
 SET @Farsi_Date =   CAST (@SYear   as VarChar(10)) + '/' + @my_mah + '/' + @my_day
 Return @Farsi_Date



END

执行功能

SELECT DBO.MITSH(GETDATE())
 
for example date is 2020-09-25

  resualt =>>>>  1399/07/04