June 30, 2017 at 10:12 am
Hello, I have a date field in the DATE in the format of 'YYYY-MM-DD' and how it is displayed, and I'm trying to split our Year and Month and concatenate into 'YYYYMM' format. I've tried DATEPART, DATEADD and other variations and I'm not quite getting the correct result or error.
June 30, 2017 at 10:20 am
It is incorrect to talk about a format for a date column itself. A date is always stored in a binary form with no display format associated with it.SELECT convert(varchar(6),SYSDATETIME(),112);
June 30, 2017 at 10:28 am
Bill Talada - Friday, June 30, 2017 10:20 AMIt is incorrect to talk about a format for a date column itself. A date is always stored in a binary form with no display format associated with it.SELECT convert(varchar(6),SYSDATETIME(),112);
where would i insert the date field in question to convert, and would I want to name it 'as colname'?
June 30, 2017 at 10:33 am
SELECT t.name, convert(varchar(6),t.create_date,112) AS MyDateCol FROM sys.tables t;
June 30, 2017 at 10:40 am
What is the actual data type of your "date field"?
June 30, 2017 at 10:41 am
Bill Talada - Friday, June 30, 2017 10:33 AMSELECT t.name, convert(varchar(6),t.create_date,112) AS MyDateCol FROM sys.tables t;
LEFT(CONVERT(char(8), datecolname, 112), 6) AS YearMo
This does it for me, and so does the above
Thank you
June 30, 2017 at 11:06 am
quinn.jay - Friday, June 30, 2017 10:41 AMBill Talada - Friday, June 30, 2017 10:33 AMSELECT t.name, convert(varchar(6),t.create_date,112) AS MyDateCol FROM sys.tables t;
LEFT(CONVERT(char(8), datecolname, 112), 6) AS YearMo
This does it for me, and so does the above
Thank you
You don't need the LEFT if you convert to a 6-chars long string. 😉
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply