?
Solved

SQL Serving converting substring to date

Posted on 2014-09-26
4
Medium Priority
?
473 Views
Last Modified: 2014-09-27
SQL Server 2008.
I have a file name that contains date information that I need to load into a date field. To get the date information I'm using a substring. I am not able to convert the substring to a date, please help.

File Name: XXXX_XXXXRN_FLAT10_XXX_20131230_0731.DAT

substring: SUBSTRING(FILE_NM,24,4)+'-'+SUBSTRING(FILE_NM,28,2)+'-'+SUBSTRING(FILE_NM,30,2)+' 00:00:00.000'

Returns: 2013-12-30 00:00:00.000

I am not able to update the 2013-12-30 00:00:00.000 to a date field. I have tried CAST and CONVERT on the substring with no luck.
0
Comment
Question by:SharonBernal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1500 total points
ID: 40347194
Use a style number when converting (here's a list)

XXXX_XXXXRN_FLAT10_XXX_20131230_0731.DAT
                                                  ^^^^^^^^

there happens to be a style for YYYYMMDD so it can be simplified too

     convert(date,substring(file_nm,24,8),112)
or
     convert(datetime,substring(file_nm,24,8),112)

---
ps: the safest possible date string to use in SQL Server is YYYYMMDD
0
 

Author Comment

by:SharonBernal
ID: 40347694
The substring is still not converted to a date.

I get this message:
Conversion failed when converting date and/or time from character string.
0
 

Author Comment

by:SharonBernal
ID: 40347789
Thanks for pointing me in the right direction. I've figured it out.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40347951
What was the problem?
(I did actually test what I proposed and it worked without issue)
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question