SQL query where the last 2 digits of WHERE should be ignored

Asked

Viewed 91 times

2

I’ll try to get to the point.

I got the following SELECT:

SELECT * FROM "Inverter" WHERE "InstallationUUID" = '0D013C023900-15-0B4B00' AND "CreatedTime_Key" > 20170718000000 AND "CreatedTime_Key" <= 20170718001500 AND "DataQuality" = 1 ORDER BY "CreatedTime_Key" ASC

I was wondering if you had any commands WHERE ignore the last 2 digits of the value I am comparing to it. Example:

"CreatedTime_Key" > 201707180000(00) AND "CreatedTime_Key" <= 201707180015(00)

Numbers between '()' are the ones I want to ignore.

Examples for a better understanding of what I want to do:

Table example:

| ID | CreatedTime_Key |  Nome  |
| 01 | 20170718000001  |  CAIO  |
| 02 | 20170718000501  |  JOAO  |
| 03 | 20170718000002  |  MARIA |
| 04 | 20170718001000  |  MARIO |
...

I want the SELECT:

SELECT * FROM "Inverter" WHERE "InstallationUUID" = '0D013C023900-15-0B4B00' AND "CreatedTime_Key" > 20170718000000 AND "CreatedTime_Key" <= 20170718001500 AND "DataQuality" = 1 ORDER BY "CreatedTime_Key" ASC

Return to me:

| ID | CreatedTime_Key |  Nome  |
| 02 | 20170718000501  |  JOAO  |
| 04 | 20170718001000  |  MARIO |

NOTE: I cannot change the values that go on WHERE. They need to be thus Datetime (20170818001500 = 2017/07/18 00:15:00), or it is necessary to ignore the seconds in the WHERE.

1 answer

3


A possibility that you can use and use aq LEFT function, stating how many characters you use from the left, it is interesting to consider whether the data size will be the same for all records.

SELECT
  *
FROM Inverter
WHERE InstallationUUID = '0D013C023900-15-0B4B00'
AND LEFT(CreatedTime_Key, 12) > 201707180000
AND LEFT(CreatedTime_Key, 12) <= 201707180015
AND DataQuality = 1
ORDER BY CreatedTime_Key ASC
  • by chance the command left only works with string? Because I tested it here and did not return any value. The column would be of the type BIGINT, updated the question informing the type.

  • 1

    Run the script below to see the values that are displayed: SELECT *, LEFT(Createdtime_key, 12) FROM Inverter WHERE Installationuuid = '0D013C023900-15-0B4B00'

  • he brings the column CreatedTime_Key cut straight, without the last 2 values.

  • I updated the reply query, check if it is bringing what I expected!!

  • Don’t just take the result of splitting your bigint by 100 to make the comparison?

  • 1

    @Oliveira was just the comparison value that was wrong(you edited), need to put without the last 2 digits too. It worked perfectly, thank you!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.