Using a between Date in SQL QUERY - sql

Im using vb.net code with Sql Server 2008 R2.
I'm trying to get result by using a SQL query to get all rows that have a value between 2 dates:
Here is my the where clause of my statement:
Where (CONVERT(varchar(10), visit_nextVisitDate, 103) between '02/04/2017' AND '15/05/2017')"
but I always get all rows for the same month (month 4).
I tried this:
WHERE (CAST(dbo.Visits.visit_date AS date) BETWEEN '24/04/2017' AND '02/05/2017')
but I got an error cause my date fields are saved in format yyyy/mm/dd
How can I change the SQL date format to dd/mm/yyyy?

Why would you do date comparisons using strings? That is just wrong, wrong, wrong. (If you do it, use ANSI standard formats, YYYY-MM-DD so the comparisons are correct.)
Just do this using dates:
Where visit_nextVisitDate between '2017-04-02' AND '2017-05-02'
Actually, it is a bad idea to use between with dates. Aaron Bertrand has a very good blog on this subject.
I recommend:
Where visit_nextVisitDate >= '2017-04-02' AND
visit_nextVisitDate < '2017-05-03'

Related

Sql Server Table date query showing incorrect result

I have a Sql server table which contains below Date values(4th october)
Now Below query is not showing any result
select
*
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '10/01/2018' and TBA.ActionDate <= '10/04/2018' which is not correct.
But If I write
select
*
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '10/01/2018' and TBA.ActionDate <= '10/05/2018' it is returning me all results.
What I am doing wrong.
There are two problems with this query. The first, is that it's using a localized string. To me, it looks like it's asking for rows between January and April. The unambiguous date format is YYYYMMDD. YYYY-MM-DD by itself may not work in SQL server as it's still affected by the language. The ODBC date literal, {d'YYYY-MM-DD'} also works unambiguously.
Second, the date parameters have no time which defaults to 00:00. The stored dates though have a time element which means they are outside the search range, even if the date parameter was recognized.
The query should change to :
select
*
from [dbo].[TB_AUDIT] TBA
where
cast(TBA.ActionDate as date) between '20181001' and '20181004'
or
cast(TBA.ActionDate as date) between {d'2018-10-01'} and {d'2018-10-04'}
Normally, applying a function to a field prevents the server from using any indexes. SQL Server is smart enough though to convert this to a query that covers the entire date, essentially similar to
where
TBA.ActionDate >='2018:10:01T00:00' and TBA.ActionDate <'2018-10-05T00:00:00'
When you don't specify a time component for a DATETIME, SQL Server defaults it to midnight. So in your first query, you're asking for all results <='2018-10-04T00:00:00.000'. All of the data points in your table are greater than '2018-10-04T00:00:00.000', so nothing is returned.
You want
TBA.ActionDate >= '2018-10-01T00:00:00.000' and TBA.ActionDate < '2018-10-05T00:00:00.000'`
Use properly formatted dates!
select *
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '2018-10-01' and TBA.ActionDate <= '2018-10-04'
YYYY-MM-DD isn't just a good idea. It is the ISO standard for date formats, recognized by most databases.
when you just filter by the date, it is with regard to the time as per the standard.

Issues with cast/convert datetime/timestamp to date

I have troubles when I try to cast or convert DateTime/timestamp to date.
The source is a SQL Server and the SQL I use is CONVERT(date,[the_column]) AS [the_column] or the other SQL is CAST([the_column] AS DATE) AS [the_column].
No matter which one I use I get a wrong date - instead of 2014-11-25 the result is 2014-11-23. I've tried to change the type of the column in the DB (it was datetime I changed it to timestamp) - no difference.
Does anyone know what could be the reason and what is the solution? Thank you.
P.S. This is the sql I use.
SELECT [NUMER_ZAMOWIENIA]
,[NUMER_ODBIORCY]
,CONVERT(date,[DATA_REJESTRACJI]) AS [DATA_REJESTRACJI]
,CAST([TERMIN_KLIENTA] AS DATE) AS [TERMIN_KLIENTA]
,[INDEKS]
,[ILOSC_ZAMOWIONA]
,[ILOSC_WYSLANA]
,[STATUS_POZYCJI]
,[CENA]
,[CENA_SPRZEDAZY]
,CAST([DATA_REALIZACJI] AS DATE) AS [DATA_REALIZACJI]
,[ORIGINAL_NUMER]
,DATEPART(hour,[ZAMOWIENIA].[DATA_REJESTRACJI]) AS HOUR_REG
,DATEPART(minute,[ZAMOWIENIA].[DATA_REJESTRACJI]) AS MIN_REG
,DATEPART(hour,[ZAMOWIENIA].[TERMIN_KLIENTA]) AS HOUR_TER
,DATEPART(minute, [ZAMOWIENIA].[TERMIN_KLIENTA]) AS MIN_TER
,DATEPART(hour,[ZAMOWIENIA].[DATA_REALIZACJI]) AS HOUR_REALIZ
,DATEPART(minute, [ZAMOWIENIA].[DATA_REALIZACJI]) AS MIN_REALIZ
FROM [ACCESS].[dbo].[ZAMOWIENIA]
Query results in PDI
Query results in MSSQL Server

T-SQL Dates using Convert() function?

I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.

Between Date is not working in SQL SERVER

I have a very simple SQL which select data from a range of date. So, for example if I have,
SELECT ...... WHERE PV.[Time] BETWEEN '02/26/2014' AND '02/26/2014'
This SQL Select statement is not selecting the data in 02/26/2014. Please help
Your current statement BETWEEN '02/26/2014' AND '02/26/2014' has the same value on the left and right and so is equivalent to = '02/26/2014'.
This will only bring back rows at midnight on 26 Feb. Use
WHERE PV.[Time] >= '20140226' AND PV.[Time] < '20140227'
It's always preferable to use a non-dubious string format for dates (1/2/2000 can be interpreted as 01-feb-2000 or 02-jan-2000, depending on your local settings). I prefer the ISO format yyyymmdd or ODBC canonical yyyy-mm-dd and always use CONVERT to be explicit when handling dates. But this is not your problem :)
The problem with your query is that you are actually filtering dates BETWEEN '02/26/2014 00:00:00' AND '02/26/2014 00:00:00', thus you'll only get values at exactly the datetime. To get datetime values through whole day 02/26/2014 use the following:
SELECT ...
WHERE PV.[Time] >= '2014-02-26' AND PV.[Time] < dateadd(day, 1, '2014-02-26');

Need a date instead of datetime in SQL Server 2005 with dateadd

I've got a query that get's a date of a field in a program. This date has to be modified with 10 years.
The query I made is
SELECT DATEADD(yy, +10, '"+thisfield.value+"')
where '"+thisfield.value+"' is coming from the program and is filled in like 01-08-2012.
The result of the query is 2022-07-31 00:00:00.000. The problem I have is that I just need 2022-08-01 but in the format of 01-08-2022 so that I can automatically fill an other field with this result.
In SQL Server 2005 the date function doesn't work only the datetime function and I just don't need that.
I hope this is clear (first time i post something). Can anyone help me?
You can either truncate the result, or cast it to DATE
CAST(DATEADD(yy, +10, '"+thisfield.value+"') AS DATE)"
CONVERT(VARCHAR, DATEADD(yy, +10, '"+thisfield.value+"'), 101)"
CONVERT using style 101 is in the format mm/dd/yyyy, which happens to be the format you want. Keep in mind that you can format the result however you want in your application for display purposes which is better than returning strings from SQL Server.
Also note that you should look into parameterized queries and/or stored procedures.