I have a table that one of its columns is XML data type. This XML value includes a TimePeriod value that looks like this:
"{start:{align:'Start',date:'2011-05-20T00:00:00'}"
I managed to extract only the date from this string with the following query:
SELECT SUBSTRING(configuration.value('(/ActiveService/@TimePeriod)[1]','nvarchar(50)'),
CHARINDEX('''', configuration.value('(/ActiveService/@TimePeriod)[1]','nvarchar(50)'),28)+1,10 )
FROM [MySystem].[dbo].[MyServices]
so i got this as the result: 2013-11-01
But, now i need to convert it to this format: YYYYMMDD
And when I'm using CONVERT to format 112 I Still get the same result: 2013-11-01
Why?
It you took value from xml as varchar, you cannot convert it into format YYYYMMDD (because it's already varchar), you have to convert it to date (or datetime first):
select convert(varchar(8), convert(datetime,
substring(
@data.value('(/ActiveService/@TimePeriod)[1]','nvarchar(50)'), 29, 10
), 120), 112)
But in your case it could be easier just remove -
, by replace(<your value>, '-', '')
.