Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting all chars before space in SQL SERVER

Tags:

sql

sql-server

i am trying to get all chars before the space:

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

but it is not working. please help!

example data:

7/8/2010 11:47 AM
7/8/2010 10:55 AM
like image 355
Alex Gordon Avatar asked Aug 19 '10 22:08

Alex Gordon


1 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)
like image 71
Thomas Avatar answered Oct 12 '22 07:10

Thomas