programmer138200 programmer138200 - 5 months ago 9
SQL Question

How to split one section of string via Tsql?

I try to split this string

3/9/1395 12:00:00 AM
and get this string
1395/9/3
.I search but sql has no method for this work. How to do this?

Answer

You can convert your variable to a Date and get Year, Month and Day

DATEPART(YEAR,Cast('3/9/1395 12:00:00 AM' as date)) 

DATEPART(MONTH,Cast('3/9/1395 12:00:00 AM' as date)) 

DATEPART(DAY,Cast('3/9/1395 12:00:00 AM' as date))

https://msdn.microsoft.com/en-us/library/ms174420.aspx