Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Numeric date into date time in SQL Server query
    text
    copied!<p>I have this query that is running error-free:</p> <pre><code>Select [Object], CASE WHEN MsgID = '61' THEN SUBSTRING(Parms,35,6) END AS [MEDIA] from JnlDataSection WHERE [Object] = '061 STATEMENTS' </code></pre> <p>There is a third column which I am interested in this table called Date. However database was developed a long time ago and the person that did has made date into a <code>(numeric(17,9), not null</code> datatype). E.g. <code>20130106.050257023</code>. </p> <p>What I want is this split into two different columns one as date: <code>2013-01-06</code> and the second as time: <code>05:02:57</code> (no microseconds). </p> <p>I have come up with this query to convert:</p> <pre><code>Select [Object], CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+ SUBSTRING([Date],10,2)+':'+ SUBSTRING([Date],12,2)+':'+ SUBSTRING([Date],14,2)+'.'+ SUBSTRING([Date],15,3))) AS DATE) 'Date', LEFT(CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+ SUBSTRING([Date],10,2)+':'+ SUBSTRING([Date],12,2)+':'+ SUBSTRING([Date],14,2)+'.'+ SUBSTRING([Date],15,3))) AS TIME),8) 'Time', CASE WHEN MsgID = '61' THEN SUBSTRING(Parms,35,6) END AS [MEDIA] from JnlDataSection WHERE [Object] = '061 STATEMENTS' AND CAST(substring(convert(varchar(50), [Date]), 0, 5) + '-' + substring(convert(varchar(50), [Date]), 5, 2) + '-' + substring(convert(varchar(50), [Date]), 7, 2) AS DATETIME) = CONVERT(date, DATEADD(day, -1, getdate())) --Converting </code></pre> <p>However I receive this error when I run this:</p> <blockquote> <p>Msg 8116, Level 16, State 1, Line 1<br> Argument data type numeric is invalid for argument 1 of substring function.</p> </blockquote> <p>Can anyone modify my query for it to run error free? That would be greatly appreciated.</p>
 

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