Output of Empty Columns in SQL Query

血红的双手。 提交于 2019-12-11 19:33:53

问题


I have a fairly large SQL Query that converts a numeric date data type to a date time value and further specifies the search based on my requirements. However when I run this query I'm seeing the column names but there is no data in the columns, it is just the titles of the columns in this order: Dataset, Date, Time, MsgID, Parms, Dataset When the order should be: Data, Time, Dataset, Media (Separate column with Substring of Parms). Also this is only showing my data from the past 2 days instead of the entire database, which also is a problem.

I need some help in displaying the columns in that order with the data present from all of the columns. If anyone can make any suggestions or modificiations to my existing SQL query to get the required output, it would be greatly appreciated. I know this is a quick fix for an expert programmer, but I'm still learning the ropes and require some assistance.

This is my SQL Query:

    SELECT [Object] AS [Dataset],
    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',
    MsgId,
    Parms,
    CASE WHEN MsgID = '61' THEN SUBSTRING(Parms,35,6)
    ELSE '' --Optional ELSE
    END  AS [Dataset]
    FROM ( SELECT  ItemId,
    CONVERT(VARCHAR(18),[Date]) [Date],
    [Object],
    MsgID,
    Parms
    FROM JnlDataSection
    WHERE 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()))) A --Converting to date again to          remove     the time part
    WHERE SUBSTRING(Parms,35,6) = 'X05219' AS [Media]
    ORDER BY [DATE] DESC;

Please Note: I'm using SQL Server Management Studio 2008.


回答1:


Now, it looks like your extraction from [DATE] into Date and Time is incorrect. You are putting the same string into 'Date' and 'Time'



来源:https://stackoverflow.com/questions/17658995/output-of-empty-columns-in-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!