Getting all characters before space in SQL SERVER

I am trying to get all characters before a space:

SUBSTRING(reporttime,1,CHARINDEX(reporttime,' ',1))

but it does not work. please, help!

data examples:

7/8/2010 11:47 AM
7/8/2010 10:55 AM
+3
source share
2 answers
Select Substring( MyTextColumn, 1, CharIndex( ' ', MyTextColumn ) - 1)

Actually, if these are datetime values, then there is a better way:

Select Cast(DateDiff(d, 0, MyDateColumn) As datetime)
+10
source

To get a date from a DateTime value, the best line of code I found is to remove the time value and change your requirements:

CONVERT(NVARCHAR(10), reporttime, 103)

It will be displayed on 08/07/2010.

+1
source

Source: https://habr.com/ru/post/1760641/


All Articles