Home > database >  SQL server date format
SQL server date format

Time:12-23

Excuse me how to format the date for the following format?

. 20 th, 2020

CodePudding user response:

Can go to see the convert function, transfer date string corresponding parameters and format

CodePudding user response:

Find the didn't find the corresponding format

CodePudding user response:

SELECT the CONVERT (VARCHAR (19), GETDATE ())
If you don't get in, may be because the simplified Chinese character version of the question,
Remove the date (date) (month) (year) respectively to deal with it?

CodePudding user response:

Can according to the following, but if use much more, had better be written as a scalar function,
 select upper (CASE DATEPART (MONTH, GETDATE ()) 
1 then the WHEN 'Jan.'
THEN the WHEN 2 'Feb.'
WHEN 3 THEN 'Mar.'
THEN the WHEN 4 'Apr.'
5 THEN the WHEN 'May.'
6 THEN the WHEN 'Jun.'
7 THEN the WHEN 'Jul.'
8 THEN the WHEN the '. '
9 THEN the WHEN 'Sept.'
10 THEN the WHEN 'Oct.'
THEN the WHEN 11 'Nov.'
12 THEN the WHEN 'Dec.'
END)
+
Upper (CASE DATEPART (DAY, GETDATE ())
THEN the WHEN 1 '1 st'
THEN the WHEN 2 '2 nd'
THEN the WHEN 3 '3 rd'
The ELSE LTRIM (DATEPART (DAY, GETDATE ())) + 'th'
END)
+ ",
The ltrim (DATEPART (YEAR, GETDATE ()))
/*
DEC. 22 th 2020
*/

CodePudding user response:

 IF OBJECT_ID (' dbo. Fun_GetEnDateString) IS NOT NULL 
The DROP FUNCTION dbo. Fun_GetEnDateString
GO
-=============================================
Author: yenange
- the Create date: 2020-12-22
- Description: get English date string
-=============================================
The CREATE FUNCTION dbo. Fun_GetEnDateString
(
@ d DATETIME
)
RETURNS a VARCHAR (50)
AS
The BEGIN
DECLARE @ r VARCHAR (50);
The SET @ r=upper (CASE DATEPART (MONTH, @ d)
1 then the WHEN 'Jan.'
THEN the WHEN 2 'Feb.'
WHEN 3 THEN 'Mar.'
THEN the WHEN 4 'Apr.'
5 THEN the WHEN 'May.'
6 THEN the WHEN 'Jun.'
7 THEN the WHEN 'Jul.'
8 THEN the WHEN the '. '
9 THEN the WHEN 'Sept.'
10 THEN the WHEN 'Oct.'
THEN the WHEN 11 'Nov.'
12 THEN the WHEN 'Dec.'
END)
+
Upper (CASE DATEPART (DAY, @ d)
THEN the WHEN 1 '1 st'
THEN the WHEN 2 '2 nd'
THEN the WHEN 3 '3 rd'
The ELSE LTRIM (DATEPART (DAY, @ d)) + 'th'
END)
+ ",
Ltrim (DATEPART (YEAR, @ d))
RETURN @ r;
END
GO
-- -- -- -- -- -- -- -- -- -- -- -- -- -- to create functions above -- -- -- -- -- -- -- -- -- -- -- -- --

- have a function, direct access to:
The SELECT dbo. Fun_GetEnDateString (' 2020-08-20 ')

/*
2020
20 th.*/
  • Related