在SQL Server中格式化日期

时间:2017-05-01 06:01:34

标签: sql sql-server date

我想问一下如何在select语句中格式化日期。例如,我的日期'2000-07-31'。我希望输出类似于'星期一,即2000年7月30日'

3 个答案:

答案 0 :(得分:0)

使用此脚本创建UDF,甚至尝试改进它。

declare @t table(col1 int,col2 varchar(50),col3 int)
insert into @t (col1,col2) VALUES (0,'Zero'),(1,'First'),(2,'Second')
,(3,'Third'),(4,'Fourth'),(5,'fifth'),(6,'Sixth'),(7,'Seventh'),(8,'Eighth')
,(9,'Ninth'),(10,'Tenth'),(11,'eleventh'),(12,'Twelveth'),(13,'thirteenth')
,(14,'Fourteenth'),(15,'Fifteeth'),(16,'Sixteenth'),(17,'Seventeenth')
,(18,'Eighteenth'),(19,'Nineteenth'),(20,'Twenty'),(30,'Thirty')

declare @input varchar(10)='21'

declare @Words varchar(2000)=''
declare @i int=len(@input)

while((len(@input)>0))
begin

if(@input>=0 and @input<=19 and @i=len(@input))
begin
select @Words=@Words+' '+ col2 from @t where col1 =@input
BREAK
END
else if(len(@input) between 3 and 4)
BEGIN
select @Words=@Words+' '+ col2 from @t where col1 =@input/cast(('1'+replicate('0',len(@input)-1)) as int)
select @Words=@Words+' '+ col2 from @t where col1 ='1'+replicate('0',len(@input)-1)
END
else
begin
select @Words=@Words+' '+ col2 from @t where col1 =left(@input,1)+ replicate('0',@i-1)

END
set @input=stuff(@input,1,1,'')
if(cast(@input as int)=0)
BREAK
set @i=@i-1


end
select @Words

然后这样做

declare @Input date='2017-04-30'

    select datename(WEEKDAY, datepart(day,@input))+' '+'the '
    +fn.colname
    +' '+'of '+datename(MONTH, @Input)+' 'cast(datepart(year,@input) as varchar)
    cross apply(select colname from fn_NumtoWords(datepart(day,@input)))fn

答案 1 :(得分:0)

这个问题has already answer here。 因此,没有内置方法可用于此类转换。

但是如果你将问题分解成更小的部分,你可以很好地使用stackoverflow来解决问题。 检查this answer

我已经从那个答案中修改了UDF以便为序数编号工作,我没有打扰超过一百万的大数字:

CREATE FUNCTION [dbo].[fnNumberToWords] (@Number AS bigint, @ordinals AS bit)
RETURNS varchar(1024)
AS
BEGIN

  DECLARE @Below20 TABLE (
    ID int IDENTITY (0, 1),
    Word varchar(32)
  )

  DECLARE @Below100 TABLE (
    ID int IDENTITY (2, 1),
    Word varchar(32)
  )
  DECLARE @Below100ordinals TABLE (
    ID int IDENTITY (2, 1),
    Word varchar(32)
  )

  IF (@ordinals = 0)
  BEGIN
    INSERT @Below20 (Word)
      VALUES ('Zero'), ('One'), ('Two'), ('Three'),

      ('Four'), ('Five'), ('Six'), ('Seven'),

      ('Eight'), ('Nine'), ('Ten'), ('Eleven'),

      ('Twelve'), ('Thirteen'), ('Fourteen'),

      ('Fifteen'), ('Sixteen'), ('Seventeen'),

      ('Eighteen'), ('Nineteen')
  END
  ELSE
  BEGIN
    INSERT @Below20 (Word)
      VALUES ('Zeroth'), ('First'), ('Second'), ('Third'),

      ('Fourth'), ('Fifth'), ('Sixth'), ('Seventh'),

      ('Eight'), ('Nineth'), ('Tenth'), ('Eleventh'),

      ('Twelveth'), ('Thirteenth'), ('Fourteenth'),

      ('Fifteenth'), ('Sixteenth'), ('Seventeenth'),

      ('Eighteenth'), ('Nineteenth')
    INSERT @Below100ordinals
      VALUES ('Twentieth'), ('Thirtieth'), ('Fortieth'), ('Fiftieth'),

      ('Sixtieth'), ('Seventieth'), ('Eightieth'), ('Ninetieth')
  END
  INSERT @Below100
    VALUES ('Twenty'), ('Thirty'), ('Forty'), ('Fifty'),

    ('Sixty'), ('Seventy'), ('Eighty'), ('Ninety')

  DECLARE @English varchar(1024) = (SELECT
    CASE
      WHEN @Number = 0 THEN ''
      WHEN @Number BETWEEN 1 AND 19 THEN (SELECT
          Word
        FROM @Below20
        WHERE ID = @Number)
      WHEN @Number BETWEEN 20 AND 99 THEN CASE
          WHEN @ordinals = 1 THEN CASE
              WHEN (@Number % 10 > 0) THEN (SELECT
                  Word
                FROM @Below100
                WHERE ID = @Number / 10)
                + '-' + dbo.fnNumberToWords(@Number % 10, 1)
              ELSE (SELECT
                  Word
                FROM @Below100ordinals
                WHERE ID = @Number / 10)
            END
          ELSE (SELECT
              Word
            FROM @Below100
            WHERE ID = @Number / 10)
            + '-' + dbo.fnNumberToWords(@Number % 10, 0)
        END
      WHEN @Number BETWEEN 100 AND 999 THEN (dbo.fnNumberToWords(@Number / 100, 0)) + ' Hundred' + CASE
          WHEN @ordinals = 1 THEN CASE
              WHEN (@Number % 100 > 0) THEN ' ' + dbo.fnNumberToWords(@Number % 100, 1)
              ELSE 'th'
            END
          ELSE ' ' + dbo.fnNumberToWords(@Number % 100, 0)
        END
      WHEN @Number BETWEEN 1000 AND 999999 THEN (dbo.fnNumberToWords(@Number / 1000, 0)) + ' Thousand' + CASE
          WHEN @ordinals = 1 THEN CASE
              WHEN (@Number % 1000 > 0) THEN ' ' + dbo.fnNumberToWords(@Number % 1000, 1)
              ELSE 'th'
            END
          ELSE ' ' + dbo.fnNumberToWords(@Number % 1000, 0)
        END
      ELSE ' INVALID INPUT'
    END)

  SELECT
    @English = RTRIM(@English)

  SELECT
    @English = RTRIM(LEFT(@English, LEN(@English) - 1))

  WHERE RIGHT(@English, 1) = '-'

  RETURN (@English)
END

使用此功能,您可以编写

DECLARE @d date = '20000731'
SELECT datename(WEEKDAY, datepart(day,@d))+ 
       ', the '+ [dbo].[fnNumberToWords](DAY(@d),1)  +
       ' of '  + datename(MONTH ,@d) + 
       ', year '+ [dbo].[fnNumberToWords](YEAR(@d),0)

获得这样的结果:

  周四,七月三十一日,一年两千。

对于除英语之外的计算机非友好语言,这可能要困难得多。

答案 2 :(得分:-1)

试试这个:

选择日期名称(工作日,月份(&#39; 2000-07-31&#39;)),演员表(日(&#39; 2000-07-31&#39;)作为字符),年份(&# 39; 2000年7月31日&#39)