Populating records using BETWEEN - sql

A record has its date in 'yyyy.MM.dd' format (for example, 2018.05.02),
and I have made a query to populate certain records between a certain time frame.
SELECT * FROM tableA WHERE (BATCHDT '2018.01.01' AND '2018.05.01');
The query looks fine, at least to me, but does not work on my MSSQL.
Please help.

Just use ANSI standard formats:
SELECT *
FROM tableA
WHERE BATCHDT BETWEEN '2018-01-01' AND '2018-05-01';
No conversion should be necessary. I do not recommend using BETWEEN with dates, because they might have a time component. A safer method is:
SELECT *
FROM tableA
WHERE BATCHDT >= '2018-01-01' AND
BATCHDT < '2018-05-02';

try this.
In SQL a Date type is a Date type regardless of the format. Using it will mean casting the field against literals
SELECT * FROM tableA WHERE cast(BATCHDT as date) BETWEEN '2018-01-01' AND '2018-05-01'
--- 2018-jan-01 and 2018-May-01

You may need to convert your source field data into acceptable format for date conversion.
If your source data 'yyyy.MM.dd' format (for example, 2018.05.02)
then you can change it into format that acceptable for date conversion, for example 'YYYYMMDD', by remmoving the '.' character (so your date record become 20180502)
the query could be as follows
SELECT * FROM tableA WHERE cast(REPLACE(BATCHDT,'.','') as date) BETWEEN '20180101' AND '20180501'
by default in MSSQL server also recognize 'YYYYMMDD' format for conversion into date, so you also not required to cast it into date first
SELECT * FROM tableA WHERE REPLACE(BATCHDT,'.','') BETWEEN '20180101' AND '20180501'

I would suggest you to keep BATCHDT as DATE datatype to avoid confusion with time values, which will come with DATETIME.
Also, suggest you to use ISO 8601 standard for representing dates.
BETWEEN is a inclusive operator. So, below
BETWEEN '2018-01-01' AND '2018-05-01' will turn to be
BETWEEN '2018-01-01 00:00:00.000' AND '2018-05-01 00:00:00.000'
So, it will not return values which fall on date 2018-05-01. So, you have to apply below BETWEEN condition
BETWEEN '2018-01-01 00:00:00.000' AND '2018-05-01 23:59:59.997'
Instead of that, I would suggest you to go for DATE operator, which is very straight forward for BATCHDT.
BETWEEN '2018-01-01' AND '2018-05-01'

Related

how to select all entries having date 25-11-20 in oracle 11g?

sql table
here in the table above named carpooling contains a column name start_on which has date time as timestamp i have to write a query to select all the rows having date as 25-11-20 using to_char and to_date.
You write a timestamp literal like this:
timestamp '2020-11-25 00:00:00'
so the full filtering condition will be
where start_on >= timestamp '2020-11-25 00:00:00'
and start_on < timestamp '2020-11-26 00:00:00'
Note that dates and timestamps are different in Oracle, and dates include times down to the second (this is for historical reasons - originally there was only the date type, and timestamp was added much later).
Use the TRUNC function, along with date and interval literals:
SELECT *
FROM CARPOOLING
WHERE START_ON BETWEEN DATE '2020-11-25'
AND (DATE '2020-11-26' - INTERVAL '0.000001' SECOND)
You can simply use to_date, but it's recommended to remove the time when comparing the dates. Otherwise, rows having the same date, but a different time will not be selected. Removing the time can be done using TRUNC.
So you can do something like this:
SELECT * FROM carpooling
WHERE TRUNC(start_on) = TO_DATE('2020-11-25','yyyy.mm.dd');
If you don't want to check the 25th of November 2020, but another data, change the date to match your goal.

Filter observations with a string variable as date in Spark SQL

I am looking for the right syntax to use where clause to filter a string variable as date. For example, I would like to retain records with pol_expi_dt after July 01, 2020.
pol_expi_dt takes the format of '2020-12-01 00:00:00'.
What would be the right format for this Spark Sql query?
The following code returned 0 observations. So I figure I must did something wrong
SELECT *
FROM `cloud01_propertylocationmerge`.`exposure_new`
where cast(pol_expi_dt as date) >= '2020-07-01'
Since your '2020-07-01' is a string and pol_expi_dt is a timestamp, one should cast in the other format. For example when you cast the column,
date_format(pol_expi_dt, 'yyyy-MM-dd') >= '2020-07-01'
or cast both to the date format.
date(pol_expi_dt) >= date('2020-07-01')

TIMESTAMP To DATE in Oracle SQL

I have a column called Create_Date which has data in the format like 19-JUN-18 10.27.00.000000000 PM and data type is TIMESTAMP(6).
I am trying to look at date range like yesterday's date or between two dates in Create_Date without using TO_DATE(TO_CHAR(P.CREATE_DATE_TIME,'dd/mon/yy')) and entering the value as '19-JUN-18'.
I want to use Create_Date=SYSDATE-1 OR Create_Date=CURRENT_DATE-1 instead to filter on yesterdays date. Or Use Create_Date>=SYSDATE or Create_Date>=CURRENT_DATE to look at dates greater than or equal to today.
Can someone help?
You could use TRUNC:
SELECT *
FROM tab
WHERE Create_Date >= TRUNC(SYSDATE,'DD') -- -1
-- or between to dates (using date literals)
WHERE Create_Date >= DATE 'yyyy-mm-dd'
AND Create_Date < DATE 'yyyy-mm-dd'
As it's a timestamp I'd cast the truncated (to midnight) current date to a timestamp for clarity; Oracle will use an index on that column even if you leave it as a date, but it doesn't hurt to make it explicit:
where create_date >= cast(trunc(sysdate) as timestamp)
The trunc() function defaults to truncating to midnight; you can explicitly include 'DD' as a second argument if you prefer (for even more clarity, though some would see it as noise).
If you want a range, say yesterday:
where create_date >= cast(trunc(sysdate) - 1 as timestamp)
and create_date < cast(trunc(sysdate) as timestamp)
If you want to specify other dates then you can use timestamp literals, e.g. to see everything for May:
where create_date >= timestamp '2018-05-01 00:00:00'
and create_date < timestamp '2018-06-01 00:00:00'

I have a date/time stamp field, where I need to pull records just by date

I have a date/time stamp field, where I need to pull records just by date.
Example: All data were records are >= '01/01/2016'.
The data in the field is store in the following format '9/5/2012 7:34:59 AM'
I have tried the following but either I get an error or bad results:
where to_char(start_time) > '01/01/2016' (still gives 2012 records)
where trunc(start_time) > '01/01/2016' (Error: Not a valid month)
In mysql you should use
this is in string canonical format
select * from my_table
where start_time > '2016/01/01'
or
or converting by str_to_date using proper format
select * from my_table
where start_time > str_to_date('01/01/2016', '%d/%m/%Y')
Use to_date to convert the string to date and then do the comparison.
Try this:
where start_time >= to_date('01/01/2016', 'dd/mm/yyyy');
In Oracle (and perhaps other database products) you can use the ANSI date literal, as shown below. You could also use to_date(), but the benefit of the ANSI date literal is that it doesn't require a function call. (Function calls are overhead which consumes time and resources, although calling to_date() just once is not a concern.)
... where start_time >= date '2016-01-01'
Note that in the ANSI standard date literal, the date must be in the exact format YYYY-MM-DD (with dashes and not with slashes or any other separators), since the ANSI date literal does not take a format model.

SQL query selecting between two dates

I want to select records between two dates - a startDate and endDate (they are date/time format in sql). I have the following sql query but it does not work, could someone tell me what I'm doing wrong?
SELECT *
FROM house
WHERE startDate >= '2012/02/22 00:00:00' AND endDate <= '2012-02-25 00:00:00'
I would suggest converting the dates to a datetime and comparing them as well as keeping the date standard and consistent. Something like:
"SELECT *
FROM house
WHERE DATE(startDate) >= DATE('2012-02-22 00:00:00')
AND DATE(endDate) <= DATE('2012-02-25 00:00:00')"
NOTE: I assumed your startDate and endDate were of the same format as the strings your provided.
Do you want all rows that startDate is '2012-02-22' or later and endDate is '2012-02-22' or previous? Then, use this:
SELECT *
FROM house
WHERE startDate >= '2012-02-22'
AND endDate < '2012-02-26' --- notice the `<`, not `<=`
--- and the `day+1`
When using dates with SQL products, better use this format in queries and statements: '20120222' or this (which I find easier to read: '2012-02-22'.
Using slashes like '2012/02/22' or any other order than Year-Month-Day is not recommended.
There's no need to include the time part. '2012-02-22 00:00:00' is the same as '2012-02-22'.
Using endDate <= '2012-02-25 00:00:00' means that any row with date 25nd of Feb. 2012 but time after midnight ('00:00:00') will not match the condition. If you want those rows, too, use endDate < '2012-02-26' instead.
You could use DATE(endDate) <= DATE('2012-02-25 00:00:00') or DATE(endDate) <= '2012-02-25' but these conditions are "un-sargable", so your queries will not be able to use an index on endDate.
There is the builtin STR_TO_DATE function in MySql that takes same format mask as date_format.
start_date >= str_to_date('2012/02/22 00:00:00','%Y/%m/%d %h:%i:%s)
I guess thats type casting issue the reason why it din work because the input you are matching in the where clause is different that is the column is of date or datetime type and you are matching with a manual string format either use to_char on the left side of where to match the format on the right side or use to_date() on right side.
SELECT *
FROM house
WHERE
to_char(startDate, 'YYYY/MM/DD
24hh:mm:ss')>=
'2012/02/22 00:00:00'
AND to_char(endDate,
'YYYY/MM/DD
24hh:mm:ss') <= '2012-02-25
00:00:00'