Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT, using ISDATE() makes sense for validation, change of approach.</p> <p>With respect to that conversion error, all I can assume is that some data doesn't meet some date/time 'rules'. With that in mind perhaps try something like this for discovery of those conditions. It's simplistic but may be helpful. Note you could do this as a count(*) first as I'm unaware how many records you are dealing with and won't say that this will be super quick.</p> <pre><code>SELECT yyyymmddxxxxxx , CASE WHEN isdate( (yyyy + '0101') ) = 0 OR ( yyyy &lt; '1960' OR yyyy &gt; '2020' ) THEN yyyy ELSE '' END AS yyyy , CASE WHEN isdate( (yyyy + mm + '01') ) = 0 THEN mm ELSE '' END AS mm , CASE WHEN isdate( (yyyy + mm + dd) ) = 0 THEN dd ELSE '' END AS dd , CASE WHEN ( hh &gt; '23' ) THEN hh ELSE '' END AS hh , CASE WHEN ( mi &gt; '59' ) THEN mi ELSE '' END AS mi , CASE WHEN ( ss &gt; '59' ) THEN ss ELSE '' END AS ss FROM your_table CROSS apply (SELECT convert(varchar(14), yyyymmddxxxxxx)) AS ca1 (int2str) CROSS apply (SELECT substring(int2str, 1,4) -- yyyy , substring(int2str, 5,2) -- mm , substring(int2str, 7,2) -- dd , substring(int2str, 9,2) -- hh , substring(int2str,11,2) -- mi , substring(int2str,13,2) -- ss ) AS ca2 (yyyy,mm,dd,hh,mi,ss) WHERE ISDATE(convert(varchar(8),int2str,112)) = 0 OR ISDATE( (hh + ':' + mi + ':' + ss) ) = 0 OR ( yyyy &lt; '1960' OR yyyy &gt; '2020' ) -- arbitrary, ignore of change to suit ; </code></pre> <p><a href="http://sqlfiddle.com/#!3/9ab73/1" rel="nofollow">http://sqlfiddle.com/#!3/9ab73/1</a> output is like this:</p> <pre><code>| YYYYMMDDXXXXXX | YYYY | MM | DD | HH | MI | SS | |----------------|------|----|----|----|----|----| | 99990410123456 | 9999 | | | | | | | 20149910123456 | | 99 | 10 | | | | | 20140499123456 | | | 99 | | | | | 20140410993456 | | | | 99 | | | | 20140410129956 | | | | | 99 | | | 20140410123499 | | | | | | 99 | | 20140231123499 | | | 31 | | | | </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload