How to get previous day via SQLite in Swift

Multi tool use
Multi tool use


How to get previous day via SQLite in Swift



I have a sqlite table with a number of dates. I would like to execute a query to select those records where the Date is the previous date. I have tried the following but it doesn't work:


SELECT SUM(Amount) AS TOTAL FROM (SpentAmount) Where Date ='(Date().yesterday)'"



and output is : '2018-07-02 05:43:16 +0000'


'2018-07-02 05:43:16 +0000'



But I want only date and this format: '02-07-2018'.


'02-07-2018'



And I m trying This : SELECT SUM(Amount) AS TOTAL FROM SpentAmount WHERE Date = DATE('now', '-1 days');



but this Query give me no result



This is my database Database
but when i execute the query then give me this result





Previous date to what? Yesterday, or the previous date to some other date?
– Tim Biegeleisen
Jul 3 at 5:55





How are you storing your dates? Are you storing them as 02-07-2018 ?
– Tim Biegeleisen
Jul 3 at 6:05


02-07-2018





Yes this is my Date Format 02.07.2018
– Sham Dhiman
Jul 3 at 6:09





And is that July 2nd or February 27th? Not a good format.
– rmaddy
Jul 3 at 6:11




1 Answer
1



If you want to target yesterday (i.e. the date before today), then use DATE('now', '-1 days'):


DATE('now', '-1 days')


SELECT SUM(Amount) AS TOTAL
FROM SpentAmount
WHERE Date = STRFTIME('%d.%m.%Y', DATE('now', '-1 days'));



As a general comment, your current date format is very non ideal. It will be very hard to sort your table by date. Instead, you should always store your date information in ISO format with SQLite, i.e. use 2018-07-02, not 02-07-2018.


2018-07-02


02-07-2018





This should work on date but If needed in exact '02-07-2018' format, we can use STRFTIME like STRFTIME('%d-%m-%Y', DATE('now', '-1 days'))
– CRDave
Jul 3 at 6:03






Updated Question Please check
– Sham Dhiman
Jul 3 at 6:09





@CRDave It looks like you were right. Thanks for commenting, I appreciate that.
– Tim Biegeleisen
Jul 3 at 6:09





Thank You @TimBiegeleisen This query Give me properly Result Thank you again Senior
– Sham Dhiman
Jul 3 at 6:30





@CRDave also Thank you Senior
– Sham Dhiman
Jul 3 at 6:30






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Khh1lnM HQBosUEObmbaBi5o8l4WlkuY0wQMc22RIrgapJ6pExipRgJ4x0eZ kAx,Qap,Ake
pm k0M JxisMP6ey7gBC

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications