Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I couldn't find a more elegant solution for the datetime conversion but here you go:</p> <p><strong>1. DATETIME conversion</strong></p> <p>This assumes your value is always in the same format you specified:</p> <p><strong>Example code for you to run</strong></p> <pre><code>DECLARE @Value VARCHAR(255) = '20131211142319' SELECT CONVERT(DATETIME,LEFT(@Value,8) + SPACE(1) + STUFF(STUFF(STUFF(RIGHT(@Value,6), 1, 0, REPLICATE('0', 0)),3,0,':'),6,0,':')) </code></pre> <p>This splits the field into two sections, the DATE portion <code>LEFT(@Value,8)</code> and then the TIME <code>STUFF(STUFF(STUFF(RIGHT(@Value,6), 1, 0, REPLICATE('0', 0)),3,0,':'),6,0,':')</code>. The TIME portion is essentially just adding in the colon where applicable (see STUFF on <a href="http://technet.microsoft.com/en-us/library/ms188043.aspx" rel="nofollow">MSDN</a>)so that it returns a value such as:</p> <p><code>20131211 14:23:19</code> This makes it applicable to directly convert to a DATETIME. </p> <p><strong>2. Removing the # from the numbers</strong></p> <p><strong>Example code for you to run</strong></p> <pre><code>DECLARE @ValueNumber VARCHAR(255) = '#8339274' SELECT SUBSTRING(@ValueNumber,2,LEN(@ValueNumber)) </code></pre> <p>The above statement will take your number and only return data from the 2nd value onwards, excluding the #. See SUBSTRING on <a href="http://technet.microsoft.com/en-us/library/ms187748.aspx" rel="nofollow">MSDN</a></p> <p>To make this run on your table, just replace my variable names in the <code>SELECT</code> statement with your column names.</p> <p><strong>Example using the above on columns in a table:</strong></p> <pre><code>SELECT CONVERT(DATETIME,LEFT([DATECOLUMNNAME],8) + SPACE(1) + STUFF(STUFF(STUFF(RIGHT([DATECOLUMNNAME],6), 1, 0,REPLICATE('0',0)),3,0,':'),6,0,':')) AS [Date], SUBSTRING([NUMBERCOLUMNNAME],2,LEN([NUMBERCOLUMNNAME])) AS [Number] FROM [TABLENAME] </code></pre> <p>Replace [DATECOLUMNNAME] with the name of the column that holds your datetime value. Replace the [NUMBERCOLUMNNAME] with the name of the column that holds your number with the #.</p> <p>Then finally replace [TABLENAME] with your table name that contains those columns.</p>
    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. VO
      singulars
      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