0

I have a TEXT in this format 31/10/15.

How do I convert this into a DATE format?

As I need to let the user search from data using a date range.

example: From 15/7/13 to 31/10/15

Or is there a way to so without converting to date?

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
DaveSX
  • 1
  • You need specify RDMBS each db have differences regarding `dates` – Juan Carlos Oropeza Sep 22 '15 at 16:16
  • In the future, please ask separate questions as separate questions. – Kenny Evitt Sep 22 '15 at 16:30
  • It's also unclear whether "TEXT" refers to character or string data generally or to the specific `text` data type. Judicious formatting of code as code is helpful for better understanding. – Kenny Evitt Sep 22 '15 at 16:32
  • possible duplicate of [Convert string to date in sql server?](http://stackoverflow.com/questions/23864535/convert-string-to-date-in-sql-server) – Becuzz Sep 22 '15 at 16:32
  • @Becuzz Why would you link to a possible duplicate *that's closed as a duplicate*?? – Kenny Evitt Sep 22 '15 at 16:33
  • 1
    possible duplicate of [Sql Server string to date conversion](http://stackoverflow.com/questions/207190/sql-server-string-to-date-conversion) – Kenny Evitt Sep 22 '15 at 16:34
  • @KennyEvitt There are about a hundred different ones. That one's dates had the format closest to the OPs. – Becuzz Sep 22 '15 at 16:35
  • @Becuzz Sorry; I just found it funny. I flagged this as a duplicate of the duplicate-parent of the one to which you linked. There's no reason to have a separate question for each format. The documentation is very clear. – Kenny Evitt Sep 22 '15 at 16:38

2 Answers2

1

You can use CONVERT() for this:

DECLARE @d VARCHAR(50) = '31/10/50'
SELECT CONVERT(DATE, @d,3)

Note that with a 2-digit year SQL Server will make the year start with '19' for 50 and up, and 49 and below will be '20'

Storing as a DATE field will allow easier comparisons, otherwise you'll have to perform this conversion at each step.

Hart CO
  • 34,064
  • 6
  • 48
  • 63
0

Use CONVERT; example:

SELECT [Date] = CONVERT(date, '31/10/15', 3);

And yes, it's possible to search dates in the same format as the examples you provide, but don't do that – use the proper data types in both your queries and your table columns.

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93