How to SELECT between two dates in SQL Server - sql

I need to give a select count in a table using the range of two dates, however my dates are in "yyyy-MM-dd" format, and when I execute the select SQL it returns the following error:
Converting a varchar data type to a datetime data type resulted in a value out of range
Here is the query I'm using:
SELECT COUNT(*) AS score
FROM requests
WHERE date_em BETWEEN '2019-04-01' AND '2019-04-30'
Table structure
date_em = DATETIME
Can someone help me?

My preferred method is:
SELECT COUNT(*) AS score
FROM requests
WHERE date_em >= '2019-04-01' AND
date_em < '2019-05-01'
This handles the time component and will make use of an index.
The following also works well in SQL Server:
SELECT COUNT(*) AS score
FROM requests
WHERE CONVERT(date, date_em) BETWEEN '2019-04-01' AND '2019-04-30'
In SQL Server, this will also use an index (if available). However, normally functions on columns preclude the use of an index, so I'm more hesitant about this approach.

have you try this
SELECT COUNT(*) AS score FROM requests WHERE date_em BETWEEN '2019-04-01 00:00' AND '2019-04-30 23:59'
add 00:00 and 23:59 to make it look like a DateTime.

Related

Convert unix timestamp to datatime sql query

I have query like this
select * from my_tabel where created_at >= 1655546400000
I want to give this query to the end-user and the ceraetd_at field in the database is Unix timestamp and he doesn't know Unix timestamp so he should be able to change it to custom DateTime. I want to change it to this query because this query is readable for end-user
select * from my_tabel where created_at >= "2022-06-18 10:00:00"
I have found this very helpful, you can try it. After converting you can use the value however you want.
DECLARE #UnixDate BIGINT = 1655546400000
SELECT CAST(DATEADD(ms, CAST(RIGHT(#UnixDate,3) AS SMALLINT),
DATEADD(s, #UnixDate / 1000, '1970-01-01')) AS DATETIME2(3))
More details about DATEADD function.
After the update of your question, What I have understood after reading your question is, that your user wants to compare date with ceraetd_at field, as UNIX time does not give much details about regular date format, that's you want to convert the ceraetd_at, so that they can compare or run their query.
So for doing that you can do like below:
SELECT *
FROM my_tabel
WHERE (CAST(DATEADD(ms, CAST(RIGHT(created_at,3) AS SMALLINT),
DATEADD(s, created_at / 1000, '1970-01-01')) as DATETIME2(3))
>= '2022-06-18')

Where Clause for a datetime Format Not Cooperating

I am having an issue working with a date range in our DB. The organic format is datetime "yyyy-mm-dd 00:00:00.000". I have tried working with this a few ways and I am not having any success, those methods were:
Organic as noted above:
Select
I.InvoiceDate
....
Where I.InvoiceDate >= '2017-01-01 00:00:00.000'
And converting to a mm/dd/yyy in the select with the where clause as:
Select
Convert(Char(10),I.InvoiceDate,101) as 'InvDt'
...
Where Year(Convert(Char(10),I.InvoiceDate,101)) >= '2017'
Also using the organic field name rather than the convert for the where clause:
Select
Convert(Char(10),I.InvoiceDate,101) as 'InvDt'
...
Where I.InvoiceDate >= '10/01/2017'
Lastly, I also tried picking out the year and/or month using the appropriate year/month tags in the select and/or where clause.
In each instance the same results were produced:
Results I get AND the organic datetime format before converting to mm/dd/yyyy
Thanks for the help good people.
You can try this
select
*
from tableName
where Create_Date >='2017-11-08 00:00:00.000'

SQL Server: how to select records with specific date from datetime column

I have a table with one column dateX formatted as datetime and containing standard dates.
How can I select all records from this table where this dateX equals a certain date, e.g. May 9, 2014 ?
I tried the following, but this returns nothing even if I have several records with this date.
SELECT *
FROM dbo.LogRequests
WHERE (CONVERT(VARCHAR(10), dateX, 101) = '09/05/14')
Edit: In the database the above example looks as follows, using SQL 2012: 2014-05-09 00:00:00.000
The easiest way is to convert to a date:
SELECT *
FROM dbo.LogRequests
WHERE cast(dateX as date) = '2014-05-09';
Often, such expressions preclude the use of an index. However, according to various sources on the web, the above is sargable (meaning it will use an index), such as this and this.
I would be inclined to use the following, just out of habit:
SELECT *
FROM dbo.LogRequests
WHERE dateX >= '2014-05-09' and dateX < '2014-05-10';
For Perfect DateTime Match in SQL Server
SELECT ID FROM [Table Name] WHERE (DateLog between '2017-02-16 **00:00:00.000**' and '2017-12-16 **23:59:00.999**') ORDER BY DateLog DESC
SELECT *
FROM LogRequests
WHERE cast(dateX as date) between '2014-05-09' and '2014-05-10';
This will select all the data between the 2 dates

Parsing date in sql query

I am using SQL Server 2005.
My Trade_Date column is of datatype datetime.
It has values as: 2/12/2013 11:59:00 PM , 2/13/2013 11:59:00 PM
i.e. different dates with time.
I want to compare date [only date] in where clause.
I am trying with following query:
select *
from foclosing
where CONVERT(varchar(11), Trade_Date) like '2/12/2013 %'
or
select *
from foclosing
where Trade_Date = CONVERT(datetime, '2/12/2013')
but both of these queries are not working.
What can be the issue?
select * from foclosing
where Trade_Date >= '20130212' AND Trade_Date < '20130213'
Use yyyymmdd which is safe for SQL Server
Don't apply functions to columns, then compare (your 1st query)See mistake 2 here: https://www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/
Don't compare locale-based dates as varchar (your 1st query)
Trade_Date has a time element which is why your 2nd query fails
In summary, a date conversion should not be used for examples like this: datetime is a range

SQL Server DateTime and SQL

I am having trouble with the following simple query
SELECT * FROM users WHERE Created = '28/02/2013'
The issue is the column CREATED is a datetime datatype, so the query executes fine as long as the timestamp is 0:00:0, if the time stamp is set to say 12:00, then the query does not return a result set.
Any idea why?
Thanks
Because you are not specifying the time, so it assumes that you are doing:
SELECT * FROM users WHERE Created = '28/02/2013 00:00:00'
If you want the whole day, then you need a range of times:
SELECT *
FROM users
WHERE Created >= '20130228' AND Created < '20130301'
Also, please use non ambiguous format for dates ('YYYYMMDD') instead of other formats.
SELECT * FROM users WHERE CAST(Created AS DATE) = '28/02/2013'
will fix it, but be careful, it disables indexes
SELECT * FROM users WHERE Created BETWEEN '28/02/2013 00:00' AND '28/02/2013 23:59'
And this will use index
If you don't need to consider time: try to convert created field to date and then compare as;
SELECT * FROM users WHERE convert(date,Created) = '28/02/2013'
--this would be even better with iso date format (not culture specific)
SELECT * FROM users WHERE convert(date,Created) = '20130228' --yyyymmdd format
You have to convert the column into date and then compare
SELECT * FROM users WHERE CONVERT(VARCHAR(11), Created, 106) = '28 Feb 2013'