That task should be done by the next layer up in your software stack. SQL is a data repository, not a presentation system
You can do it with
CONVERT(VARCHAR(10), fmdate(), 101)
But you shouldn't
Answer from TFD on Stack Overflowsql server convert date to string MM/DD/YYYY - Stack Overflow
How do i convert yyyymmdd into mmmm (text)
How to change date format from YYMMDD to DD-MM-YYYY
sql server - SQL Convert 'yyyymmdd date' to 'mm/dd/yyyy' date - Stack Overflow
Videos
That task should be done by the next layer up in your software stack. SQL is a data repository, not a presentation system
You can do it with
CONVERT(VARCHAR(10), fmdate(), 101)
But you shouldn't
select convert(varchar(10), fmdate, 101) from sery
101 is a style argument.
Rest of 'em can be found here.
CAST and CONVERT (Transact-SQL) - Date and time styles
Invoice_Date, [Date] = CONVERT(CHAR(15),CONVERT(datetime,STR(invoice_date)),106)
This is the closest I've come so far. I can use a substring from this to get the month, but its abbreviated atm which is not what i want. It's showing as 01 Feb 2024 whereas i just want it to show 'February'.
Here's a simple example that should answer your question:
declare @date date
set @date = '19901124'
select @date --your date format
select CONVERT(varchar(10), @date, 101) --new date format
Hope that helps.
Thanks, Matt
declare @date varchar(8)
set @date = '20151205'
SELECT convert(varchar(10), cast(@date AS date),103) -- dd/mm/aaaa
101 = mm/dd/aaaa
103 = dd/mm/aaaa
111 = aaaa/mm/dd
more info: https://msdn.microsoft.com/es-pe/library/ms187928.aspx
The following isn’t pretty and I’m 99% there is a better way to do it, but it works. The “LEFT” function was necessary because I couldn’t find a style that didn’t include the seconds.
SELECT (CONVERT(VARCHAR(20), [yourdatetimefield], 101) + ' ' + LEFT(CONVERT(VARCHAR(20), [yourdatetimefield], 108) ,5)) AS myDateTime
FROM [yourdb.yourtable]
Can’t seem to find answer to what seems like a simple question. I have a date that is returned 2022-02-07 18:53:36.000 I need to convert that date to mm/dd/yyyy hh:mm. Is that possible?
Hi @Raj D ,
Here is how to convert text as a DATE data type.
After that you can format it at will by using CONVERT() or FORMAT() functions.
SQL
-- DDL and sample data population, start
DECLARE @processdata TABLE ([ProcessDate] NVARCHAR(255) NOT NULL);
INSERT @processdata VALUES
('Sat May 30 2020 14:19:55 GMT-0700 (Pacific Daylight Time)'),
('Sat May 30 2020 14:19:55');
-- DDL and sample data population, end
DECLARE @separator CHAR(1) = SPACE(1);
SELECT *
, TRY_CAST('' +
REPLACE([ProcessDate], @separator, '') +
'' AS XML)
.value('concat((/root/r[4]/text())[1],"-", (/root/r[2]/text())[1],"-", (/root/r[3]/text())[1])', 'DATE') AS Result
FROM @processdata;
;WITH rs AS
(
SELECT *
, TRY_CAST('' +
REPLACE([ProcessDate], @separator, '') +
'' AS XML)
.value('concat((/root/r[4]/text())[1],"-", (/root/r[2]/text())[1],"-", (/root/r[3]/text())[1])', 'DATE') AS Result
FROM @processdata
)
SELECT *
, TRY_CONVERT(VARCHAR(10), rs.result, 101) AS [Converted]
, FORMAT(rs.result, 'MM/dd/yyyy') AS [Formatted]
FROM rs;
Output
+-----------------------------------------------------------+------------+
| ProcessDate | Result |
+-----------------------------------------------------------+------------+
| Sat May 30 2020 14:19:55 GMT-0700 (Pacific Daylight Time) | 2020-05-30 |
| Sat May 30 2020 14:19:55 | 2020-05-30 |
+-----------------------------------------------------------+------------+
SELECT try_convert(date, substring(ProcessDate, 5, 11))
FROM @processdata
I am here assuming that month names are always three letters and dates are always two digits. To keep it simple, I'm ignoring the time part.
You should always store date and time values in proper data types; you should never store them as strings. Never!
SELECT CONVERT(char(10), GetDate(),126)
Limiting the size of the varchar chops of the hour portion that you don't want.
SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy – 10/02/2008
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd – 2008.10.02
SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) -- dd mon yyyy
SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy
SELECT convert(varchar, getdate(), 108) -- hh:mm:ss
SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) -- yyyymmdd
SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm
SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm
I understand your problem to be how to successfully convert the string into a DATE value. An undelimited string of integers is assumed to be Year-Month-Day order. (And, of course, I agree with the comments that dates should be stored in DATE data types in the database.)
Reviewing the MSDN CONVERT documentation does not show a built-in conversion for your string, but it is easy to work around.
http://msdn.microsoft.com/en-us/library/ms187928.aspx -- CAST and CONVERT
I changed the month to 8 to make it easier to double check. Using the CONVERT style option 3, you can do the following:
DECLARE @String VARCHAR(10);
DECLARE @DateValue DATE;
SET @String = '250809';
-- Convert your undelimited string DDMMYY into a DATE
-- First: Add / between the string parts.
SET @STRING = SUBSTRING(@String,1,2)+'/'+
SUBSTRING(@String,3,2)+'/'+SUBSTRING(@String,5,2);
-- Second: Convert using STYLE 3 to get DD/MM/YY interpretation
SELECT @DateValue = CONVERT(Date, @String, 3);
-- Using the DATE
-- Select the value in default Year-Month-Day
SELECT @DateValue AS DefaultFormat;
-- Select the value formatted as dd/mm/yy
SELECT CONVERT(VARCHAR(20),@DateValue,3) AS [DD/MM/YY];
The results of the last two selects are:
DefaultFormat
-------------
2009-08-25
DD/MM/YY
--------
25/08/09
To get your DATE formatted in the way you want it, you have to insert the '/' delimiters then use the STYLE 3 in converting from the string to the DATE. (I am sure that there are other workarounds and conversion styles that would work as well.)
Likewise when displaying the DATE as you desire, you need to use STYLE 3
Another approach is to cast it directly to a date in SQL Server 2008 or above, then store it that way as @ypercube commented above. Assuming 2000 <= all expected years <= 2099:
DECLARE @d CHAR(6) = '250909';
SELECT DATEFROMPARTS('20'+RIGHT(@d,2),SUBSTRING(@d,3,2),LEFT(@d,2));
You may need to do things a little differently if you could have 250999 etc, then you would need some way to indicate whether that's 1999 or 2099, for example. This also doesn't handle validation (like the other answer, it will choke on values like 252525).
When you want to display the date, then format it at the display/presentation layer, but store it correctly in the database. I still question whether it is actually useful to display as ambiguous formats like 25/09/2009 - for that date specifically it's clearly September 25th, but are you sure your entire audience will always get 07/08/2009 correctly? While I'm in the USA that's July 8th, but last week I was in Canada, and I would expect that to be August 7th. Output formats like 2009-07-08 are much clearer and less prone to misinterpretation; even better would be July 8th, 2009 - but then that opens the door to folks using a different language. All that said, these formats can be completely controlled by the client application (C# has very powerful formatting functions), and shouldn't dictate how you actually store the data in the database. They should be stored as dates because you get automatic validation, all of the date/time functionality, etc. Stop storing dates as strings (and maybe even try to get the CSV to contain more reliable literal formats, like YYYYMMDD). Some useful reading perhaps:
https://sqlblog.org/2009/10/12/bad-habits-to-kick-choosing-the-wrong-data-type
https://sqlblog.org/2009/10/16/bad-habits-to-kick-mis-handling-date-range-queries