Converting string to date in sql

681 views Asked by At

I need to convert 2014-11-18T14:08:43+00:00 which is in varchar in my sql developer to date format in order to filter a few entries.

I tried

to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')

but it gives an error

ORA-01830: date format picture ends before converting entire input string.

Kindly help..

5

There are 5 answers

0
Jeremy C. On BEST ANSWER

Just in case you didn't mean to put up sql server but instead you need to use oracle (seeing as you are using to_date and you are getting an ora exception) I added a quick datetime conversion for date and timestamp (no milliseconds) for your date format:

SELECT to_Date(concat
               (substr
                (myvar,0,10),
                concat(' ',
                       substr(myvar,12,8)
                      )
               ),'YYYY-MM-DD HH24:mi:ss') AS mydate
FROM mytable

Fiddle

3
ThePravinDeshmukh On
declare @varDate as nvarchar(50) = '2014-11-18T14:08:43+00:00'

select CAST(substring(@varDate,0,CHARINDEX('T',@varDate)) as date)
1
Chiragkumar Thakar On

Either you can use it like this

declare @Date as nvarchar(100) = '2014-11-18T14:08:43+00:00'
SELECT CONVERT(DATE,@Date) AS Date

OR go for this answer which is accepted in this question

ORA-01830: date format picture ends before converting entire input string / Select sum where date query

0
Mr Robbes On

You could try this;

Select CAST ('2014-11-18T14:08:43+00:00' as date)

The assumption is you are in SQL Server 2012

1
Lalit Kumar B On

ORA-01830: date format picture ends before converting entire input string. to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')

2014-11-18T14:08:43+00:00 is TIMESTAMP and not DATE.

First of all, you should never ever store DATE/TIMSTAMP as string. It is a database design flaw.

Anyway, you could convert it to TIMESTAMP WITH TIMEZONE.

For example,

SQL> SELECT to_timestamp_tz('2014-11-18T14:08:43+00:00',
  2                         'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
  3                         AS tm_stamp
  4  FROM dual;

TM_STAMP
----------------------------------------------------------------
18-NOV-14 02.08.43.000000000 PM +00:00

SQL>