SQLite ORDER BY DATE after importing .csv file - sqlite

My issue is quite weird, because it won't order my query result by its date field.. This is what I had done so far:
I create a table with schema below:
CREATE TABLE temp_price_list (
kode TEXT,
nama TEXT,
merk TEXT,
jenis TEXT,
modal TEXT,
tanggal DATE,
keterangan TEXT);
Below are my csv data:
44O2-24V,SEAL-BEAM,KOITO-II,UNIVERSAL,OL3-10,2010/12/17,OCU2-201
4401-24V,SEAL-BEAM,KOITO-II,UNIVERSAL,OL3-10,2010/10/15,OCU2-201
4401-24V,SEAL-BEAM,KYOTO,BULAT-KECIL-2COP,,2010/03/31,OL311
4402-24V,SEAL-BEAM,KYOTO,BULAT-KECIL-3COP,,2013/02/06,OLU2-IO
4402-24V,SEAL-BEAM,GEN,UNIVERSAL,,2014/04/30,IEU219
"H4-7""SEGI",SEAL-BEAM,GEN JP,UNIVERSAL,,2010/04/30,IEU211
7010-BLAT,SEAL-BEAM,RRT,12V=3COP=75W,CWU2-IO,2013/02/06,IWU2=24
7010-BLAT,SEAL-BEAM,DEPO,12V=3COP=75W,SOU222=PC,2012/01/07,IWU2=24
6014-12V,SEAL-BEAM,KYOTO,BULAT BIG 7010-12V 3C0P,OAU2-201,2010/08/10,OEU2-10
6052-12V,SEAL-BEAM,KYOTO,SEGI BESAR 3-COP,CFU2-IO,2013/02/06,CSU2-10
4402-24V,SEAL-BEAM,GEN,UNIVERSAL,CEU2-22,2010/05/26,IU311
4401-24V,SEAL-BEAM,GEN,UNIVERSAL =34=CO3,CEU2-22,2010/06/30,IU311
4001-12V,SEAL-BEAM,AIKOH,UNIVERSAL,,2010/09/17,XOU229
4002-12V,SEAL-BEAM,KYOTORRT,UNIVERSAL,,2013/02/06,OLU2-IO
4002-12V,SEAL-BEAM,GEN,UNIVERSAL,,2011/04/16,CSU222
33365-87701,SYNCROMES-KEY,SB,S-75 SMALL,,2012/01/25,O3-10
33365-87503,SYNCROMES-KEY,SB,S-75 BIG,,2012/01/25,OLRT-10
33365-87503,SYNCROMES-KEY,GB,S-75 BIG,,2012/01/25,I3-11
9-33263-048-1,SYNCROMES-KEY,GTYPE,PTER 3/4,,2010/05/31,F311
8-94152-557-0,SYNCROMES-KEY,G-TYPE,PTER 1/2,,2010/03/31,F311
Then I import those data using below command:
.mode csv
.import C:/sqlite/test.csv temp_price_list
Then I'll try to fetch the data using below query:
SELECT * FROM temp_price_list ORDER BY DATE(tanggal) DESC;
However it doesn't order it by date, just as the same ordering as the input. Any idea guys ?
I'm stuck here..

Reason - The Date information in your table is not in the correct format.
How to solve
Before inserting the data in your date column, convert the data into one of the standard date 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
as mentioned in the below link. http://www.sqlite.org/lang_datefunc.html
when you want to insert date to database, you can use this code.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
In database insert the string 'date'

Related

Ionic 3 / SQLite : How to capture data field from screen in YYYY-MM-DD HH24:mm:ss format and store it in same format in SQLite TEXT column

I have following pages in my LibraryApp, developing using Ionic 3 framework:
CategoryPage : To add new record in Category table
BookPage : To add new record in Book table
Transaction: To add daily transaction where TrxType is either "BookIssued" or "BookReturned"
CategorySummaryPage: summary for each CategoryID as on specific day
StudentSummaryPage: summary for each StudentID as on specific day
and following tables in my SQLite database :
Category(ID INTEGER,Name TEXT)
Book(ID INTEGER,CategoryID INTEGER,Title TEXT)
Transaction(TrxID INTEGER, TrxDate TEXT,TrxType TEXT,BookID INTEGER,DueReturnDate TEXT)
Student(StudentID INTEGER,StudentName TEXT)
CategorySummary(SummaryDate TEXT,CategoryID INTEGER,TotalBooksIssued INTEGER,TotalBooksReturned INTEGER)
StudentSummary(SummaryDate TEXT,StudentID INTEGER,TotalBooksIssued INTEGER,TotalBooksReturned INTEGER)
While saving new Transaction record, I am getting TrxDate from User(by default,its today date) and then adding 15days in it to get DueReturnDate 
i.e. DueReturnDate=TrxDate+15days.
TypeScript variables to store TrxDate and DueReturnDate are of type "Date"
How to ensure that values stored in above TypeScript variables and database columns are in format "YYYY:MM:DD HH:mm:ss"
so that when I select data from Transaction table I can do comparison and/or sorting on TrxDate and DueReturnDate
It is possible to store the date as YYYY:MM:DD HH:mm:ss format to do this try the below code which will convert the date to YYYY:MM:DD HH:mm:ss format.
this.save_details.date = new Date().toISOString();

Issue with Timestamp field in Fload

I am facing an issue with the fastload where my Timestamp fields are getting rejected to error table.
Below is the value of timestamp(6) field in my flat file.
23-06-2016 11:51:21.000000 23-06-2016 11:51:21.000000
Below is my code:
SET RECORD VARTEXT "¡";
DEFINE
TRANSACTION_SOURCE_TYPE_ID (VARCHAR(54))
,TRANSACTION_SOURCE_TYPE_CODE (VARCHAR(20))
,TRANSACTION_SOURCE_TYPE_DESC (VARCHAR(110))
,EFFECTIVE_START_DATE (VARCHAR(54))
,EFFECTIVE_END_DATE (VARCHAR(54))
,COUNTRY_CODE (VARCHAR(13))
,SOURCE_SYSTEM_ID (VARCHAR(54))
,DW_LOAD_TIMESTAMP (VARCHAR(76))
,DW_UPD_LOAD_TIMESTAMP (VARCHAR(76))
,FORCE_SKEW_KEY (VARCHAR(51))
FILE=?INPUT_FILE;
SHOW;
INSERT INTO ?DWSBKPDB.TRANSACTION_TMP
(
TRANSACTION_SOURCE_TYPE_ID
,TRANSACTION_SOURCE_TYPE_CODE
,TRANSACTION_SOURCE_TYPE_DESC
,EFFECTIVE_START_DATE
,EFFECTIVE_END_DATE
,COUNTRY_CODE
,SOURCE_SYSTEM_ID
,DW_LOAD_TIMESTAMP
,DW_UPD_LOAD_TIMESTAMP
,FORCE_SKEW_KEY
)
VALUES
(
:TRANSACTION_SOURCE_TYPE_ID
,:TRANSACTION_SOURCE_TYPE_CODE
,:TRANSACTION_SOURCE_TYPE_DESC
,:EFFECTIVE_START_DATE (DATE, FORMAT 'YYYY-MM-DD')
,:EFFECTIVE_END_DATE (DATE, FORMAT 'YYYY-MM-DD')
,:COUNTRY_CODE
,:SOURCE_SYSTEM_ID
,:DW_LOAD_TIMESTAMP (TIMESTAMP, FORMAT 'YYYY-MM-DDBHH:MI:SS.S(6)')
,:DW_UPD_LOAD_TIMESTAMP (TIMESTAMP, FORMAT 'YYYY-MM-DDBHH:MI:SS.S(6)')
,:FORCE_SKEW_KEY
);
DW_LOAD_TIMESTAMP is creating problem here.
Any idea as to why this is happening.
Regards,
Amit
Your data obviously doesn't match the FORMAT.
:DW_LOAD_TIMESTAMP(TIMESTAMP, FORMAT 'DD-MM-YYYYBHH:MI:SS.S(6)')

How to save Date type data as string with format "dd-MM-yyyy" in SQLite?

The Sqlite normal date string format is yyyy-MM-dd. How can I save it as dd/MM/yyyy? Should I keep the yyyy-MM-dd format when working with the database and use dd/MM/yyyy only with the end user interface?
Edit 1:
I want to save my date datas as 'dd/MM/yyyy' in SQLite to be the same as my locale date format. But the date comparison is return wrong result with this format. I changed the date string in DB to 'yyyy-MM-dd' and the date comparison work perfect. Is 'yyyy-MM-dd' is the only string format which SQLite can understand as date?
You can use the strftime() function which accepts as its first parameter a format string:
SELECT strftime('%d/%m/%Y', 'now');
Output:
06/10/2015
The strftime() function gives the ability to convert Y-m-d into d/m/Y or any other format you want. So you can continue to store your date data as is in the database, and you can query it out however you wish.
you can use date formatter code:
NSString *myString = #"2012-11-22 10:19:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"dd-MMM-yyyy";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);

Date issue while extracting from mysql in asp.net

I have a date column in my mysql db which stores
1999-03-30
as the date.
Using webservices when i extract the value by following code
DOB = Convert.ToDateTime(reader["DOB"]),
The output on the screen is
/Date(922732200000)/
How to get 1999-03-30 and what is this value /Date(922732200000)/...
The easiest way to handle dates in mysql is to use UNIX TIMESTAMP to store and retrieve dates.
For example:
The query for inserting date should be:
string query="insert into myTable(name,date) values ("StackOverflow",unix_timestamp('yyyy-mm-dd HH:mm:ss')";
eg. If you want to enter the current datetime, this query can be re-Written as:
string query="insert into myTable(name,date) values ("StackOverflow",unix_timestamp(current_timestamp)";
and when you retrive the data from your table, the query should be:
string queryToretrivedata="select name, from_unixtime(date) as DOB from myTable";
While the MySqlreader reads the data, the statement in your function should be:
DateTime DOB= Convert.ToDateTime(reader["DOB"].ToString());
Also, if you want to convert this dateTime into string you can use
string DateInString==DOB.ToString("yyyy-MM-dd HH:mm:ss"); //For 24 hour Format
string DateInString==DOB.ToString("yyyy-MM-dd hh:mm:ss tt"); //For 12 Hour format

date format issue with asp.net and sql server

when i'm inserting data to the sql server 2008 database through asp.net application date inserted to the database in the following format
"10 june 2011"
this is wrong and i need "dd/mm/yyyy" or "yyyy/mm/dd" format to be inserted.why does this happen
If your data type on the database side is either datetime or date (on sql server 2008), it shouldn't matter whether you insert the date as '10 june 2011' or as '6/10/2011' or as '2011-06-10'. If you see the data actually being displayed as '10 June 2011' that's more likely because your data type is varchar or nvarchar. If that's the case and the field is only meant to hold dates, I would advise you to change the data type to be actually a datetime or date.
Use DateTime.TryParseExact() method to parse the date.
string inputDate="10 June 2011";
string []format ={ "dd MMMM yyyy","dd MMM yyyy"};
DateTime date;
if (DateTime.TryParseExact(inputDate, format, CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
Console.WriteLine("Valid " + date);
}
Table column datatype was Nvarchar, so that it saves data in the above format which is wrong. By running the following I have solved my issue
SET DATEFORMAT DMY;
ALTER TABLE #tmpTest ALTER COLUMN MyCol DATE;

Resources