As your data already in varchar, you have to convert it into date first:
select convert(varchar(10), cast(ts as date), 101) from <your table>
Answer from roman on Stack OverflowAs your data already in varchar, you have to convert it into date first:
select convert(varchar(10), cast(ts as date), 101) from <your table>
Use CONVERT with the Value specifier of 101, whilst casting your data to date:
CONVERT(VARCHAR(10), CAST(Created_TS AS DATE), 101)
t sql - How to convert a "dd/mm/yyyy" string to datetime in SQL Server? - Stack Overflow
T-SQL date conversion needed - datetime to mm/dd/yyyy hh:mm
sql server - How to get a date in YYYY-MM-DD format from a TSQL datetime field? - Stack Overflow
Converting date format in sqlserver
Videos
The last argument of CONVERT seems to determine the format used for parsing. Consult MSDN docs for CONVERT.
111 - the one you are using is Japan yy/mm/dd.
I guess the one you are looking for is 103, that is dd/mm/yyyy.
So you should try:
SELECT convert(datetime, '23/07/2009', 103)
Try:
SELECT convert(datetime, '23/07/2009', 103)
this is British/French standard.
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?
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
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
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!
Assuming your "date" column is not actually a date.
Select convert(varchar(8),cast('12/24/2016' as date),112)
or
Select format(cast('12/24/2016' as date),'yyyyMMdd')
Returns
20161224
DECLARE @v DATE= '3/15/2013'
SELECT CONVERT(VARCHAR(10), @v, 112)
you can convert any date format or date time format to YYYYMMDD with no delimiters