convert SQL string into a date that can be queried - sql

I am pulling from a database using the following:
SELECT
ID, CUSTOMER, NAME, etc, etc, TERM_INDEX
FROM "DAB055.ADT" DAB055
Now I want to limit the date range we pull, so would like to do something like:
WHERE TERM_INDEX >= '01.01.2015'
but the output in that field looks like:
20150629W
How can I convert that into a usual date field within the same statement, so that it can be filtered on?
Thanks.

It looks like your dates are stored in a format that will sort chronologically by using the alphabetical order. You can probably just say WHERE TERM_INDEX >= '20150101'.
Also it shouldn't be difficult to grab the first 8 characters and convert to a date type. Without knowing which platform you're on we'd have to guess at the syntax though.
One of these might work to figure out what your database server is:
select ##version
select * from v$version

Related

Getting not valid month in Oracle sql

I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.

How to correctly put date in SQL form

I know nothing about SQL but am trying to order a memorial keychain for someone who DOES and I'd like to put the date of their pet's passing in SQL form ... Is this correct?
SELECT DATE("2019-11-05");
It depends on the database. In general, date and string constants should use single quotes. The SQL standard for a date literal is:
SELECT DATE '2019-11-05'
However, databases might also prefer:
SELECT DATE('2019-11-05')
SELECT CAST('2019-11-05' as DATE)
Or perhaps something else.

Date/Time as string in Access SQL - Select a specific date

I would like run a SQL in MS Access like the following:
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time LIKE '2016.10.05'
ORDER BY ID;
However the result is nothing, The Time field data is look like the following:
2016.12.05 09:42:17.026
2016.12.05 09:42:17.387
2016.12.05 09:42:17.951
2016.12.05 09:42:18.464
...
2016.12.06 09:24:41.449
2016.12.06 09:24:41.854
2016.12.06 09:24:42.258
Therefore, I would like to extract the data day by day (this example: 2016.10.05)
Can anyone help me to solve this problem?
Lawrence
You need to check two things first...
Did your insert query work without errors .. are you sure time '2016.10.5' data exist in your DB?
Can you execute standard query to get time data and it works? Meaning can you 'SELECT FROM AUDCAD" ang get time data 2016.10.5
You must use the proper syntax for date expressions in Access SQL:
SELECT [Time], Ask, Bid
FROM AUDCAD
WHERE [Time] = #2016/10/05#
ORDER BY ID;
or, if Time has a time component:
SELECT [Time], Ask, Bid
FROM AUDCAD
WHERE Fix([Time]) = #2016/10/05#
ORDER BY ID;
However, it looks like you retrieve data from a DateTime2 field in SQL Server.
Thus, either change the data type to DateTime, or use the SQL Native Driver version 10 or 11 for your ODBC connection. If not, you will receive the date/time as text, not date/time values.
The separator for DateTime fields is #. Ex: #12/30/2016#.
I would recommend to always use the american order in VBA (m/d/y) even if the local machine is configured otherwise. It works fine that way.
Your sample query
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time LIKE '2016.10.05'
ORDER BY ID;
uses a LIKE clause without any wildcards, so WHERE Time LIKE '2016.10.05' behaves just the same as WHERE Time = '2016.10.05'. That won't return any rows because the [Time] column always includes some characters after the date.
By default, Access uses the asterisk (*) as the "0 or more characters" wildcard, so
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time LIKE '2016.10.05 *'
ORDER BY ID;
should work. Alternatively, you could use the ALIKE ("ANSI LIKE") keyword with the percent (%) wildcard:
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time ALIKE '2016.10.05 %'
ORDER BY ID;

Comparing dates in SQL returns wrong records

I am trying to locate a date in database between two specific dates entered by user. Something like:
SELECT date FROM table WHERE date>=dateFrom AND date<=dateTO
I have the following table:
I have made a mistake saving the date as VARCHAR and now i have to do all the str_to_date and date_format as i am using phpMyAdmin. I somehow did it but i am facing this strange problem using this query:
SELECT date_format(str_to_date(data,'%d/%m/%Y'),'%d/%m/%Y') AS data FROM montaggio WHERE data>=date_format(str_to_date('29/08/2014','%d/%m/%Y'),'%d/%m/%Y')
The query would return to me only the date 19/08/2014 where as i expected it to return 01/09/2014. On the other hand if it enter the query
SELECT date_format(str_to_date(data,'%d/%m/%Y'),'%d/%m/%Y') AS data FROM montaggio WHERE data>=date_format(str_to_date('29/08/2014','%d/%m/%Y'),'%d/%m/%Y') AND data<=date_format(str_to_date('05/09/2014','%d/%m/%Y'),'%d/%m/%Y')
The query returns nothing.
I am using phpMyAdmin.
What am i missing here? Any help would be appreciated!
You do seem a bit confused. All the operations on dates should be done on the date data type. There is no need to convert things back to strings:
SELECT data
FROM montaggio
WHERE str_to_date(data, '%d/%m/%Y') >= str_to_date('29/08/2014', '%d/%m/%Y')
You seem to understand the real solution, which is to store dates/times in the database using the correct type. If you have to use strings for dates, then always stored them as YYYY-MM-DD, so comparisons and sorting will work correctly.

MsSQL - Return results within a date range

I was wondering if someone could help me out.
I need to return database results based on a date range, im using Classic ASP and MsSQL
My script gives me dates formatted as follows:
6/18/2014
The dates are saved in the database in the following format
12/24/2014 7:03:00 AM
What im wanting to do is something as follows:
SELECT * FROM table WHERE paid >= 6/18/2014 AND =< 6/28/2014
When i run that, im getting weird results as the dates arent formatted the same.
Can someone help me out.
Cheers,
you should put those two dates between single quotes like..
SELECT * FROM table WHERE paid BETWEEN '6/18/2014' and '6/28/2014'
EDIT:
you can use DATE_FORMAT(date,format) function to display date/time data in different formats.
here's some reference
http://www.w3schools.com/sql/func_date_format.asp
if its not typo
SELECT * FROM table WHERE paid BETWEEN '6/18/2014' AND '6/28/2014'
EDIT:-my database is storing it in the format yyyy-mm-dd
SELECT *
FROM table
WHERE DATE >= '2014-05-18'
AND DATE <= '2014-06-28'
and its working correctly
here is a reference
change the format according to your own database it will work