SQLite varchar comparison as date - sql

I have two varchar field named StartDate(like 'MM/dd/yyyy') and StartTime(like 'hh:mm').
select * from Table "where StartDate<'MM/dd/yyyy'"
Can anybody help me with this query?

For your specific problem you will have to convert the varchar yourself and the answer can be found in this stackoverflow post: Sqlite convert string to date
sqlite would allow you to use a regular query for a date saved as a varchar - however, you will have to store the dates in the correct format - maybe you can change your date fields to follow this format? Have a look at the sqlite documentation: sqlite date functions.

Related

SQL - VARCHAR to date where the separator is a period not a dash SQL

I have a VARCHAR field, well many in fact in my SQL tables where the date is a VARCHAR in the following format YYYY.MM.DD
I have started with VARCHAR(REPLACE(mc.CANXREASONDATE, '.', '-') but I dont know how to change this then to a date
I have googled but cannot find the answer
Any help would be appreciated
Try cast()ing to a date:
CAST(REPLACE(mc.CANXREASONDATE, '.', '-' AS DATE)
The format YYYY-MM-DD is an ISO 8601 standard date format that most but not all databases recognize.
If you actually want to change the date column itself, you can use ALTER TABLE, with syntax such as:
ALTER TABLE mc ALTER COLUMN CANXREASONDATE DATE
Of course, the exact syntax of these statements may vary depending on the database. But this should give you some direction.
EDIT:
In SQL Server, you can find bad values using:
SELECT mc.CANXREASONDATE
FROM mc
WHERE TRY_CAST(REPLACE(mc.CANXREASONDATE, '.', '-' AS DATE) IS NULL;
You can use either TRY_CAST() in your query or fix the data.

can we insert date into varchar2 in oracle without converting by to_char?

can we insert date into varchar2 in oracle without converting by to_char?
Oracle will convert the date into a string using what localization settings are in place during the insert. This is true not only of Oracle but of any database. For a date, that might commonly be in the format "DD-MON-YY".
Here is an example of inserting a date into a varchar2 column.
That said, you should not do this. You should be storing date/time values in the database using the correct data type -- and that would be either date or timestamp. If you want to insert a constant, then use the date or timestamp qualifier with the appropriate value following it.

How to convert timestamp to date&time in SQL?

Table have one column which contain timestamp value.I want to convert that timestamp value to date&time in SQL.
When I using like this
select cast(1520339311 as datetime)
I'm getting an error:
Arithmetic overflow error converting expression to data type datetime
How to handle that?
SQL Server's TIMESTAMP datatype has nothing to do with a date and time!
See the links Below:
CAST and CONVERT (Transact-SQL)
rowversion (Transact-SQL)
Already Answered

sorting data by date?

I am sorting my database depending on date. The date field in SQLite is nothing but a string, stored in the form of dd-mm-yyyy.
How do i carry out the sorting.
My idea is to create a dummy table.Convert the date in a format yyyymmdd. Then sort it simply using sort by date column.Then again drop the table.
Is there an efficient or a better way ?
You should recreate your database to store data as ISO date yyyy-mm-dd (as recommended) then the sorting will be fine in SQLite.
Otherwise from the above, you can always substring fields from this field, and ordey by them, but that is so oldschool. Too bad on Android you can't have user defined functions.
The better way is to have a Date datatype in your database. In that way you can easily:
SELECT * FROM table ORDER BY dateColumn;
or
SELECT * FROM table ORDER BY dateColumn DESC;
if you want it in the other order.
It will be easier for you if you just make it a date data type.
edit
Wrong link. Thanks for comment, here's the correct link for datatypes: http://www.sqlite.org/datatype3.html
edit

inserting only date in a column in sql server

Is there a way to insert only date in a datetime column in sql server without the time?
for example
date (datetime)
===============
12-01-2000
16-02-2000
or i should store this as a varchar and cast it when retriving so that i can convert to whatever form i need.
my solution is to store it as varchar and convert it to datetime whenever needed
SELECT CONVERT(VARCHAR(10),GETDATE(),111) -- get datepart only
or
also check this post about creating date type :
create user defined data types:
create type Date from dateTime
http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx
If you are using SQLServer 2008 you can use the date data type.
The following SQL will strip out any time values and set them all to zero. So you won't need to worry whether a time value is there or not.
Select Cast(Floor(Cast(MyDateColumn as float)) as DateTime) as MyDateColumn
From dbo.MyTable
Just use smalldatetime or date. Convert your dates to your format before you update your date values or after you select date values in your app.
You can change format of date format in sql queries or in your app.
Here is a list on date formats in sql
http://www.sql-server-helper.com/tips/date-formats.aspx
Here's a link on date data types
http://databases.about.com/od/sqlserver/a/date_time.htm
Good Luck!