SQL Server CE date stored in string : how to compare - sql

I have a database in which, I see that dates are stored in the form of strings. Would it be possible for me to compare those date (in the form of string ) in a query?
For eg. column date1 stores 09-11-1992 00:00:00 and date2 stores 22-11-1992 00:00:00
Would it be sensible to execute a query as follows:
select * from tablename WHERE date1 > "06-11-1992 00:00:00";

If your strings are in a recognizable format, you should be able to cast them to datetimes and compare.
select *
from tablename
where cast(date1 as datetime) > cast('06-11-1992 00:00:00' as datetime)
However, I'd recommend ISO 8601 format for dates. Here's one possibility:
cast('1992-11-06T00:00:00' as datetime)
I'm assuming DD-MM-YYYY here because of your date2 example.
Just be sure you know which is the day and which is the month. Hopefully your strings are always the same format and are in-line with your regional settings.
Be careful: you may have to set the date format to get the right output from your data:
set dateformat dmy
select cast('06-11-1992 00:00:00' as datetime) -- returns 1992-11-06 00:00:00
set dateformat mdy
select cast('06-11-1992 00:00:00' as datetime) -- returns 1992-06-11 00:00:00

Related

how to do a SELECT in SQLite with a range of dates from datetime string to a UNIX timestamp?

Hi I have a database SQLite with a table "data" with the column "time#timestamp" that is a REAL for example 1669729394.792
So I have to select a range of data using 2 dates (start date and end date) written by the operator in human datetime format (ex. 2022-11-29) and extract all my data
somehow I should convert my date from standard format to UNIX timestamp
I tried like this but it doesn't work for me:
SELECT * FROM data WHERE date([time#timestamp]) BETWEEN CAST(strftime('%s', '2022-11-29') AS REAL) AND CAST(strftime('%s', '2022-11-30') AS REAL)
The function date() with a numeric parameter and no modifiers considers the numeric value as a Julian day and returns that date in the text format YYYY-MM-DD.
But your datetime values are not Julian days, they are unix timestamps and you can transform them to a readable date format YYYY-MM-DD with the modifier 'unixepoch':
date([time#timestamp], 'unixepoch')
After that you can directly compare the result to any date in the format YYYY-MM-DD and no casting is needed:
SELECT *
FROM data
WHERE date([time#timestamp], 'unixepoch') BETWEEN '2022-11-29' AND '2022-11-30'
Or, keep the value of [time#timestamp] as it is and transform the 2 date boundaries to unix timestamps:
SELECT *
FROM data
WHERE [time#timestamp] BETWEEN strftime('%s', '2022-11-29') AND strftime('%s', '2022-11-30')
Or, if your version of SQLite is 3.38.0+:
SELECT *
FROM data
WHERE [time#timestamp] BETWEEN unixepoch('2022-11-29') AND unixepoch('2022-11-30')

Converting Abbreviated Date to Date

I have a truncated "date" attribute in the dataset, and need to convert the value below to a full DATE format (assuming it's the current year).
SELECT '6-May'
would output:
2020-05-06
SQL Server is very flexible about recognizing date formats. If you want to produce a date datatype, you can cast as follows:
select cast(mycol + '-2020' as date) mydate from mytable

SELECT query with LIKE fails

I'm using a SQL SELECT query to bring back all rows from a specific date.
The column I'm using is called TimeStamp (datetime)
(An example of data from this column = 01/02/2018 07:55:55)
What I would like is to return all rows from a specific date eg 24/06/2019
I have tried
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02/01/2018 07:55:55'
which returns one row (which is correct as there is only one row containing this data)
If I then try
SELECT top 20 TimeStamp
from Report
where TimeStamp LIKE '02/01/2018%'
I get no results, I have also tried escaping the forward slashes
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02\/01\/2018%'
Most databases support a string function called left(). If I assume that your "timestamp" is a string, then:
where left(timestamp, 10) = '01/02/2018'
However, it should be stored as a date or date/time. If so, then you can do:
where timestamp >= '2018-02-01' and
timestamp < '2018-02-02'
Note the use of standard formatted dates (YYYY-MM-DD). That is the way most databases implement date literals.
In SQL Server, you can also use:
where convert(date, timestamp) = '2018-02-01'
Both this and the previous version will use an index on timestamp, so both are reasonable solutions.
this should work
SELECT TimeStamp FROM report where convert(Date, TimeStamp) = '2019-06-24'
or select timestamp from report where timestamp between '2019-06-24' and '2019-06-25'. This will get you everything between 2019-06-24 00:00:00 and 2019-06-25 00:00:00 thus all records with date 2019-06-24
Convert timestamp value to date.
SELECT TimeStamp
FROM report
WHERE CAST(TimeStamp AS DATE) = '2019-06-24'

SQL query with operator between and date in a strange format

I have a column in my table that stores a date in format (DD-MM-YY HH:MM:SS). For e.g.:
05-06-15 01:02:03
I need to output for instance all the records that have a date between the 4th and the 5th of June, so i tried:
SELECT * from table where date BETWEEN '04-06-15 00:00:00' AND '05-06-15 23:59:59'
But it also output results with a different month, as:
05-07-15 14:52:34
Is there a way to use a single query for solving this issue or I have to change all my database date format?
SELECT *
from table
where
STR_TO_DATE(date,'%d-%m-%Y %T') between
'2015-06-05 00:00:00' and '2015-06-5 23:59:59';

Convert nvarchar date to datetime

I have dates stored in a sql server database as nvarchar but I need to create a report and pull out data from the last day base on the date.
I use this when the data type is a DateTime:
SELECT *
FROM [table]
WHERE timein >= DateAdd(hh, -24, GETDATE())
I think I need to convert the GETDATE() -24 to a string to compare it to the db
The format needs to be like this:
April-30-15
Can anyone help me create a query that will select records for the past 24 hours using this date format?
Convert your timeIn string to a date and compare using dates not strings. If you replace the hyphens with spaces it will be able to cast to a date. I assume you want values since the start of previous day (ignoring the current time) so I cast that to a date also.
SELECT *
FROM [table]
WHERE cast(replace(timein, '-', ' ') as date) >= cast(DateAdd(dd, -1, GETDATE()) as date)