SQL Select between dates - sql

I am running sqlite to select data between two ranges for a sales report. To select the data from between two dates I use the following statement:
SELECT * FROM test WHERE date BETWEEN "11/1/2011" AND "11/8/2011";
This statement grabs all the dates even those outside the criteria. The date format you see entered is in the same format that I get back. I'm not sure what's wrong.

SQLite requires dates to be in YYYY-MM-DD format. Since the data in your database and the string in your query isn't in that format, it is probably treating your "dates" as strings.

Change your data to that formats to use sqlite datetime formats.
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
SELECT * FROM test WHERE date BETWEEN '2011-01-11' AND '2011-08-11'

One more way to select between dates in SQLite is to use the powerful strftime function:
SELECT * FROM test WHERE strftime('%Y-%m-%d', date) BETWEEN "11-01-2011" AND "11-08-2011"
These are equivalent according to https://sqlite.org/lang_datefunc.html:
date(...)
strftime('%Y-%m-%d', ...)
but if you want more choice, you have it.

SELECT *
FROM TableName
WHERE julianday(substr(date,7)||'-'||substr(date,4,2)||'-'||substr(date,1,2)) BETWEEN julianday('2011-01-11') AND julianday('2011-08-11')
Note that I use the format: dd/mm/yyyy.
If you use d/m/yyyy, Change in substr().

Or you can cast your string to Date format with date function. Even the date is stored as TEXT in the DB.
Like this (the most workable variant):
SELECT * FROM test WHERE date(date)
BETWEEN date('2011-01-11') AND date('2011-08-11')

SQLite does not have a concept of dates. It only knows them as text. When you do this in SQLite you're actually doing string comparisons. You can read more from the official documentation.
When two TEXT values are compared an appropriate collating sequence is used to determine the result.
Any numeric (i.e., not using words like 'May') format for dates that is padded and in order from biggest field to smallest field will work. "2021-05-07" (May 7th) comes before "2021-05-09" (May 9th). So if you use "yyyy-mm-dd" format then you'll be set. "yyyy/mm/dd" and "yyyymmdd" work just fine too. (For a better phrasing on "sortable" date formats check out RFC 3339 section 5.1.)
A reason to use "yyyy-mm-dd" format is because that's the format that SQLite's builtin date uses.

Special thanks to Jeff and vapcguy your interactivity is really encouraging.
Here is a more complex statement that is useful when the length between '/' is unknown::
SELECT * FROM tableName
WHERE julianday(
substr(substr(date, instr(date, '/')+1), instr(substr(date, instr(date, '/')+1), '/')+1)
||'-'||
case when length(
substr(date, instr(date, '/')+1, instr(substr(date, instr(date, '/')+1),'/')-1)
)=2
then
substr(date, instr(date, '/')+1, instr(substr(date, instr(date, '/')+1), '/')-1)
else
'0'||substr(date, instr(date, '/')+1, instr(substr(date, instr(date, '/')+1), '/')-1)
end
||'-'||
case when length(substr(date,1, instr(date, '/')-1 )) =2
then substr(date,1, instr(date, '/')-1 )
else
'0'||substr(date,1, instr(date, '/')-1 )
end
) BETWEEN julianday('2015-03-14') AND julianday('2015-03-16')

Put the variable in the Where Condition and parse both dates using 'BETWEEN':
SELECT * FROM emp_master
-> if you have date formate like dd/mm/yyyy simple then,
WHERE joined_date BETWEEN '01/03/2021' AND '01/09/2021';
-> and if you have date formate like yyyy/mm/dd then,
WHERE joined_date BETWEEN '2021/03/01' AND '2021/09/01';
☻♥ Done Keep Code.

Let's say you are preparing data for some report. Then the whole ordeal will look similar to this.
--add column with date in ISO 8601
ALTER TABLE sometable ADD COLUMN DateInISO8601;
--update the date from US date to ISO8601 date
UPDATE sometable
SET DateInISO8601 = substr([DateInUSformat],length([DateInUSformat])+1, -4)
|| '-' ||
substr('00' || [DateInUSformat],instr('00' || [DateInUSformat],'/'),-2)
|| '-' ||
substr('00' || rtrim(substr([DateInUSformat],instr([DateInUSformat],'/')+1,2),'/'),-2,2);
SELECT DateInISO8601
FROM sometable
WHERE DateInISO8601 BETWEEN '2022-02-02' AND '2022-02-22';
You can of course do all that on the fly, but if you have the choice -- don't. Use the ISO date by default and convert it on the way in and out to SQLite DB.

Related

SQL Server : change date format

I need to change the date format from 'yyyy-mm-dd' to 'dd.mm.yyyy'.
I have data in my table like this '2018-08-08', I need convert it to '08.08.2018'.
I have tried:
UPDATE daily_tasks
SET date = REPLACE(date, date, CONVERT(VARCHAR(255), daily_tasks.date, 102))
WHERE 1;
But, it doesn't work.
Ideally you should be storing your dates as bona-fide date columns, not as text. That being said, the date text '2018-08-08' is in fact in an ISO format, and would still allow you to do things like sort and compare against other date literals, so it is not so bad.
But converting this text to a '08.08.2018' format is the wrong thing to do. If a anything, you might want to consider adding a new date column new_date to store this date information. Do that, and then populate it with:
UPDATE daily_tasks
SET new_date = TRY_CONVERT(datetime, date);
Store your date as DATE datatype and when you read data from database use
DECLARE #myDate DATE = '2018-08-08'
SELECT FORMAT(#myDate, 'dd.MM.yyyy')
SELECT CONVERT(VARCHAR(10), #myDate, 104)
Your syntax looks like SQL Sever, so i would do :
UPDATE daily_tasks
SET Col = REPLACE(CONVERT(VARCHAR(10), daily_tasks.date, 103), '/', '.')
WHERE . . . ;
However, i would not recommend to do this, just use CONVERT() with SELECT statement whenever necessary :
SELECT REPLACE(CONVERT(VARCHAR(10), daily_tasks.date, 103), '/', '.')
Regardless of the database, dates are stored in an internal format. This is the correct way to store dates. Do not store dates as strings.
You can specify the format when you query:
CONVERT(VARCHAR(255), daily_tasks.date, 102)
Or, you can even add a computed column to provide this information:
alter table daily_tasks
add date_display as ( CONVERT(VARCHAR(255), daily_tasks.date, 102) ) ;
You could convert the date column to a varchar to store the date in your specified format. However I strongly recommend against this. You should leave it stored as a date.
If you want to do a SELECT to get the data out then you can convert it to your specified format like this:
SELECT CONVERT(VARCHAR, daily_tasks.date, 4)

SQL query that returns a date

My DB contains a period(month) and a year - I am trying to convert it to a date. I don't care for the actual day of the month so I have just made it "1" (the 1st of the month).
In my code I had to convert the "13th" period to the 12th because of the 12 months of the year, so my decode did that part... Ultimately, I want it to return as a date. I have a concatenation to make it 'look' like a date, but not actually a date.
What i do with the data is query it and return it in Excel for further manipulation. When imported to Excel, it does not import as a date nor does it let me convert to a date format.
SELECT DIA_PROJECT_DETAIL.FY_DC || '/' ||
decode(DIA_PROJECT_DETAIL.PER_DC,1,1,2, 2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,12)||
'/01' as "Date"
FROM AMS.DIA_PROJECT_DETAIL DIA_PROJECT_DETAIL
There has to be an easier or more effective way to do this!
There is no much simpler way. DECODE is fine for converting month 13 to month 12, but you use it a bit too complicated. Then you shouldn't rely on session settings, but explicitly tell the DBMS the date format your assembled string represents. Use TO_DATE with the appropriate format.
select
to_date(fy_dc || to_char(decode(per_dc, 13, 12, per_dc), '00') || '01', 'yyyymmdd')
as "Date"
from ams.dia_project_detail dia_project_detail;
Just use least():
SELECT (DIA_PROJECT_DETAIL.FY_DC || '/' ||
LEAST(DIA_PROJECT_DETAIL.PER_DC, 12) ||
'/01'
) as "Date"
FROM AMS.DIA_PROJECT_DETAIL DIA_PROJECT_DETAIL;

How do I convert dates in SqlLite from m/d/y to Y-m-d so that I can use strftime

I have a set of data that I am importing into SqlLite, but don't really have the opportunity to manipulate it before insertion. I am trying to calculate "age" of the date, but this is proving very difficult in its current format.
I am looking for a select that I can use to update the data and then begin writing queries the way I want.
Data Samples
9/20/1983
2/18/1986
8/1/1994
5/29/1999
Desired
1983-09-20
1986-02-18
1994-08-01
1999-05-29
Once I have data in that format, I will calculate the date using the following
(strftime('%Y', 'now') - strftime('%Y', Born)) - (strftime('%m-%d', 'now') < strftime('%m-%d', BOrn))
I guess if there's a way to cast the date into the right format and calculate the age in one query, that would save a step, but I haven't been able to find a way so far.
You could use the following update statement to change the date format to YYYY-MM-DD:
update t
set Born =
substr(Born, -4) || '-' ||
substr('0' || substr(Born, 1, instr(Born, '/')-1), -2) || '-' ||
substr('0' || substr(Born, instr(Born, '/')+1,
length(Born)-5-instr(Born, '/')), -2)
where substr(Born, -5, 1) = '/'
and Born LIKE '%/%/%'
The where clause is there to only update dates that have a d/m/yyyy format, where d and m could be two digits as well.

Sybase date comparison - Correct format?

I'm pretty new to Sybase and am writing a query to return results after a specified date, and also before a specified date. MM/DD/YYYY format
At the moment im doing..
SELECT *
From aTable
WHERE afterDate >= 08/07/2013
AND beforeDate <= 08/08/2013
I'm getting records back, but as I'm a Sybase newbie, I want to be sure Sybase is interpreting these dates correctly..
Their online doc is pretty bad for basic explanations on things like this!
Anyone able to confirm if what I have works, or does it need some formatting round the dates?
You'll need to convert the dates into DATETIME and tell sybase what the format is to be sure.
According to this documentation the code for MM/DD/YYYY is 101, so something like this:
SELECT *
FROM aTable
WHERE afterDate >= CONVERT(DATETIME,'08/07/2013',101)
AND beforeDate <= CONVERT(DATETIME,'08/08/2013',101)
You can see the difference by running the following select statements:
SELECT CONVERT(DATETIME,'08/07/2013',101) --MM/DD/YYYY (2013-08-07 00:00:00.000)
SELECT CONVERT(DATETIME,'08/07/2013',103) --DD/MM/YYYY (2013-07-08 00:00:00.000)
For any date-time field in sybase, instead of going through the convert function, there is a more direct approach.
SELECT *
From aTable
WHERE afterDate >= '2013-08-07'
AND beforeDate <= '2013-08-08'
The date has to be in the form 'YYYY-MM-DD'
If you want to add a time, it can be included along with the date. The date and the time have to be separated by a T.
Any date time field can be directly used using the format 'YYYY-MM-DDTHH:MM:SS'
Using the functions is too lengthy. Noone needs a bazooka to shoot a squirrel! :)
CAST( '2000-10-31' AS DATE )
will convert from text to date format....
I am assuming that your two fields (afterDate and beforeDate) are in Date format.
Your example would be:
SELECT *
From aTable
WHERE afterDate >= CAST( '08/07/2013' AS DATE )
AND beforeDate <= CAST( '08/08/2013' AS DATE )
Also, usually (but not always) a date range is on the SAME field. As I said, that is not true all the time and you may have a good reason for that.
The best approach is to use the ANSI standard which does not require any conversion: yyyymmdd (you can also include hh:mm:ss) for instance:
DateField1 >= "20150101" and DateFile1 <= "20150102"
You should decide which Input-Strings the user is going to use as parameter and then convert them and concatenate them like you want, unless it is Datetime it is not important which initial format it had, you can use it in a between-condition.
E. g. the user is from Europe and uses "DD.MM.YY" and "hh:mm" as an input parameter, I would convert and concatenate like this:
WHERE dateCol between convert(DATETIME,
convert(char(11),
convert(DATETIME, '01.06.14', 4), 16) || ' ' || '00:00', 8)
AND convert(DATETIME,
convert(char(11),
convert(DATETIME, '01.07.14', 4), 16) || ' ' || '16:00', 8)

How can I calculate the difference in days between two days stored as YYYYMMDD in a query?

I am writing a query where I need to calculate the number of days since a date that is stored in the database in the format "YYYYMMDD". Since this is not a Date datatype, I can't use native Date functions. What is the best way (performance-wise, readability-wise, etc.) to perform such a calculation in a SQL query.
Best? Convert that old table to use real date columns.
Next best? Write a database function to convert YYMMDD to a real date. Alan Campin's iDate can help. You'd end up with something akin to select cvty2d(date1)-cvty2d(date2) from ...
Better than nothing? Write ugly SQL to convert the number to character, split the character up, add hyphens and convert THAT to a real date. That beast would look something like
select
date(
substr(char(date1),1,4) concat
'-' concat
substr (char(date1),5,2) concat
'-' concat
substr(char(date1),7,2)
) -
date(
substr(char(date2),1,4) concat
'-' concat
substr (char(date2),5,2) concat
'-' concat
substr(char(date2),7,2)
)
from ...
Edit
The reason these gymnastics are necessary is that the DB2 DATE() function wants to see a string in the form of 'YYYY-MM-DD', with the hyphens being necessary.
Which version of SQL are you running? SQL2008 has no issues with that format as a date datatype
Declare #something nvarchar(100)
set #Something = '20120112'
select dateadd(dd, 1, #Something)
select datediff(dd, #Something, getdate())
2012-01-13 00:00:00.000
118