简体   繁体   中英

SQL Date Conversion Query

TimeStamp is a numeric datatype field in my table R for example: 2445302102010 (02/10/2010) I am trying to query by a date range of 02/09/15 - 02/15/15, the problem is I am getting results from 2010?

Select distinct
            R.CID,
            RIGHT(R.TimeStamp,8)
            from TableRev R 
            WHERE
            R.Codes in ('NY','NV') 
            AND RIGHT(R.TimeStamp,8) between 02092015 and 02152015
            ORDER BY R.TimeStamp

You can build the date in a way that will allow you to look for a specific date range. For that, CONCAT (or || : || can be used as a synonym for CONCAT [see Note 19]) will be helpful. So you can try something like this :

Select distinct
        R.CID,
        RIGHT(R.TimeStamp,8)
        from TableRev R 
        WHERE
        R.Codes in ('NY','NV') 
        AND (RIGHT(R.TimeStamp,4) || LEFT(RIGHT(R.TimeStamp,8),4)) 
                        between '20150209' and '20150215'
        ORDER BY R.TimeStamp

Looks like you've run into a problem that I run into all the time. Some programmer a long time ago thought that it would be a good idea.... You're going to have to pull that data in, and convert it to a function that SQL will understand.

Assuming you are using a SQL engine to query a convert will work: Try this:

AND CONVERT(datetime,RIGHT(R.TimeStamp,8),101) between 02/09/2015 and 02/15/2015

If you are using some DB2 tool you will have to CAST the data, if the original data is formatted correctly. Check the FILELAY to see if the field is in this chart. IBM Has to pretty much be an VARCHAR or TIMESTAMP to Cast. If it is, you can:

AND CAST(TimeStamp as Date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM