Muhammad Dody Suhada Muhammad Dody Suhada - 2 months ago 5
SQL Question

How can I convert this query result to date format (SQL Server)

This for example :

concat (concat('201', (substring('A41020', 2, 1)), '-', (substring('B210906', 6, 2))


and the result is 2014-06.

What additional query I can use to change this string (2014-06) to date format while I retrieve the data?

For now, I have to change cell format in Excel when I am using data from this query.

Help me master

Answer

Use the below query..here i have set the date as 01,which you can change as per your requirement. Also you can use CONVERT or CAST function in this situation.

SELECT CONVERT(datetime,'201'+substring('A41020', 2, 1)+'-'+substring('B210906', 6, 2)+'-01')

OR

SELECT CAST(('201'+substring('A41020', 2, 1)+'-'+substring('B210906', 6, 2)+'-01')as  datetime)

enter image description here

Comments