I got some problem with SQLite DataTime Filtering.
I used System.Data.SQLite in my wpf project.
I can filter using the following sql query.
SELECT * FROM tblTest WHERE CAST(testDate As date) > CAST('28/09/2015' As date);
According to above sql, I get all the dates bigger than 28/09/2015. btw testDate field is Varchar data type in my sqlite table.
When I use the BETWEEN clause, I get nothing.
SELECT * FROM tblTest WHERE CAST(testDate As date) BETWEEN CAST('28/09/2015' as date) AND CAST('10/10/2015' as date);
How should I filter date in between?
dd/mm/yyyy is not a valid TimeString in SQLite.
The correct format is yyyy-mm-dd.
As reported here,
valid TimeStrings in SQLite are:
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
And you don't need to cast to... date (which is not an existing data type in SQLite, and it will fall back to TEXT).
Reference on SQLite DataTypes: https://www.sqlite.org/datatype3.html
Also note that the semicolon (;) at the end of queries and commands is completely useless, since SQLite won't concatenate SQL statements.
In the end, your query should really be
SELECT * FROM tblTest WHERE testDate BETWEEN '2015-09-28' AND '2015-10-10'
Related
I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.
I'm new in SQL server. I have imported an Excel file into my SQL Server Database. There is a column with the Name of myDate and it shows dates like: 20.07.2018.
I did right click on my table, then I chose "Select 1000 rows" and added the convert code there but it didn't work! I have the same result as before! (the Format didn't change). Is there anyone who knows why it didn't change?
Here is what I wrote:
select convert (varchar,[dateColumn],102) as date2
from [test1].[dbo].[change1]
Go
Having a column's name as myDate doesn't mean its datatype is date, datetime or datetime2. Apparently your data type is VARCHAR. You should use date, datetime or datetime2 for such a column.
For conversion you can do it like this:
SET DATEFORMAT DMY;
SELECT convert (varchar, TRY_CAST(myDate AS DATE),102) as date2
from [test1].[dbo].[change1];
(Again this is questionable why would you convert to a varchar).
I have query database like this:
SELECT * FROM TABLE WHERE start_date = '01-10-2016' //DD-MM-YYYY
But above code error and actually in database date format is YYYY-MM-DD.
So how to create query with format date DD-MM-YYYY?
A proper date column (data type date !) has no format. Then it's enough to get your data input for a date column right, use to_date() for non-standard input format like #Shiva posted. Or better yet, always provide date literals in ISO 8601 format 'YYYY-MM-DD' to begin with, which works with any locale setting.
If you are running a broken design with dates stored as text, then combine to_date() and to_char() to transform any valid date format into any other text format:
SELECT * FROM tbl
WHERE start_date = to_char(to_date('01-10-2016', 'DD-MM-YYYY'), 'YYYY-MM-DD');
May be you can use to_date function to convert the above format into the standard format.
For instance,
SELECT to_date('01-10-2016', 'DD-MM-YYYY');
----------
2016-10-01
1 row
https://www.techonthenet.com/postgresql/functions/to_date.php
I want to get some values from my table an there are some conditions about its datetime columns.
I want to get all hotel values of a stated city from my table, which is named "LocalHotels". Also I should declare two DateTimevalues. First value should be less than or equal to hotel's value in "start" column, which is datetime data type. Second value should be greater than or equal to hotel's value in "deadline" column, which is datetime data type, either.
All datetime values in these two columns are inserted in German CultureInfo format.
When I stated query below, there are no problems;
string query = "SELECT * FROM LocalHotels WHERE city='LONDON' AND start <='5.12.2015 00:00:00' AND deadline >='8.12.2015 00:00:00' ORDER BY city";
However when I changed day value of DateTime values from one digit to two digits, as I stated in below;
string query "SELECT * FROM LocalHotels WHERE city='LONDON' AND start <='15.12.2015 00:00:00' AND deadline >='18.12.2015 00:00:00' ORDER BY city"
I got an SQLException which indicates;
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
Even though in Europe and everywhere in the world it makes perfect sense to use the month as the second of three items in the date. In the US and in this case, apparently, SQL's date time is MM.DD.YYYY, so you're going for the 15th month and 18th month
Therefore you should use
string query "SELECT * FROM LocalHotels WHERE city='LONDON' AND start <='12.15.2015 00:00:00' AND deadline >='12.18.2015 00:00:00' ORDER BY city"
or
string query "SELECT * FROM LocalHotels WHERE city='LONDON' AND start <='2015-12-15' AND deadline >='2015-12-18' ORDER BY city"
Try changing
15.12.2015 00:00:00
to
2015-12-15 00:00:00
and same format for the other date also.
You can view SQL Server's date and time format for yourself by running this query:
SELECT
GETDATE()
As you can see the format is YYYY-MM-DD HH:MM:SS.MMM. Stick to this and you won't run into any unexpected conversion errors.
EDIT
ISO 8601 is a standard date format. From MS Docs:
The advantage in using the ISO 8601 format is that it is an international standard with unambiguous specification. Also, this format isn't affected by the SET DATEFORMAT or SET LANGUAGE setting.
For this reason I would recommend it above all other formats. Examples:
2020-02-25
20200225
2020-02-25T18:37:00
SELECT CONVERT(char(10), GetDate(),126)
or
select convert(varchar,getDate(),112)
or
select replace(convert(varchar, getdate(), 111), '/','-')
Test out the queries above to get the date in the desired format (replace GetDate() with your date, or dateColumn).
As others pointed out you need the format YYYY-MM-DD.
I need to compare dates that are stored in my database as varchar against today's date.
Specifically, I need to exclude any records with a date that has passed.
I tried:
SELECT * FROM tblServiceUsersSchedule
WHERE ScheduleEndDate !='' AND ScheduleEndDate < '2015/05/31'
This selected values such as 17/06/2012 and 19/04/2015, which have both already passed, along with 01/06/2015 which hasn't.
I then tried to cast the data with:
SELECT *
FROM tblServiceUsersSchedule
WHERE CAST(ScheduleEndDate as DATETIME) < CAST('05/31/2015' as DATETIME) AND ScheduleEndDate !='' AND ScheduleEndDate is not null
But got the following error:
The coversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
I checked the data behind and none are null, none are blank white space. All are dates in the format of dd/mm/yyyy.
I can't figure out how to compare the varchar date stored with todays date.
Storing date values as varchar is simply wrong.
If possible, you should alter the table to store them as date data type.
You can do it in a few simple steps:
Rename the current columns (I'm guessing ScheduleStartDate is also varchar) to columnName_old. This can be easily done by using sp_rename.
Use alter table to add the columns with the appropriate data type.
Copy the values from the old columns to the new columns using an update statement. Since all of the dates are stored in the same format, you can use convert like this: set ScheduleStartDate = convert(date, NULLIF(ltrim(rtrim(ScheduleStartDate_old)), ''), 103) If your sql server version is 2012 or higher, use try_convert. Note i've used the nullif, ltrim and rtrim to convert values that only contains white spaces to null.
Drop and recreate indexes that is referencing these columns. The simplest way to do this is by right-clicking the index on SSMS and choose script index as -> drop and create.
Use alter table to remove the old columns.
Note: if these columns are being referenced in any other objects on the database you will have to change these objects as well. This includes stored procedures, foreign keys etc`.
If you can't change the data types of the columns, and your sql server version is lower then 2012, you need to use convert like this:
SELECT * FROM tblServiceUsersSchedule
WHERE CONVERT(DATE, NULLIF(ScheduleEndDate, RTRIM(LTRIM('')), 103)
< CAST(GETDATE() As Date);
AND ScheduleEndDate IS NOT NULL
Note that if you have even a single row where the column's data is not in dd/MM/yyyy format this will raise an error.
For sql server versions 2012 or higher, use Try_convert. This function will simply return null if the conversion fails:
SELECT * FROM tblServiceUsersSchedule
WHERE TRY_CONVERT(DATE, NULLIF(ScheduleEndDate, RTRIM(LTRIM('')), 103)
< CAST(GETDATE() As Date);
AND ScheduleEndDate IS NOT NULL
Note: I've used CAST(GETDATE() as Date) to remove the time part of the current date. This means that you will only get records where the ScheduleEndDate is at least one day old. If you want to also get the records where the ScheduleEndDate is today, use <= instead of <.
One final thing: Using functions on columns in the where clause will prevent Sql Server to use any indexing on these columns.
This is yet another reason why you should change your columns to the appropriate data type.
Other than what people have already suggested that you should never store DATETIME as VARCHAR. Always store it in a DATETIME type column; I think you should change your condition in WHERE
ScheduleEndDate < '2015/05/31'
To this, in order to get all dates which hasn't passed yet
ScheduleEndDate >= '2015/05/31'
Your query should look like
SELECT * FROM tblServiceUsersSchedule
WHERE ScheduleEndDate IS NOT NULL
AND ScheduleEndDate >= '2015/05/31'
If you HAVE TO store your dates as varchar (and as per other other answers, this is a poor practice), then using an ISO style format like yyyy-mm-dd should allow textual comparisons without issue.
If your column is a date data type, and you're using SQL 2012 or later then use DATEFROMPARTS (or one of its variants) for date comparison, so
WHERE DateToCompare < DATEFROMPARTS (2019, 12, 31)
rather than
WHERE DateToCompare < '2019-12-31'
SQL handles the latter fine, but the former is more "correct".