Getting DateRange between date part only sql server 2005 - sql-server

What is the best way to get records between dates taking into account only date part only? I mean getting only eg 2012-07-30
the following does not seem to work
DECLARE #StartDate datetime,#EndDate datetime,#NewStartDate dateTime
SET #StartDate='2012-06-03 17:43:56.220'
SET #EndDate='2012-07-30 00:00:00.000'
SELECT *
FROM MyTable
WHERE CONVERT(varchar(10),StartDate,111) >=CONVERT(varchar(10),#StartDate,111)
AND CONVERT(varchar(10),EndDate,111) <=CONVERT(varchar(10),#EndDate,111)

select CONVERT(varchar(10),getdate(),111)
prints
2012/05/23
So it definitely strips the date part.
What makes you think your query "does not seem to work" ?

Related

SQL Server returns different result for the same Date selection query, In different scenarios

I was trying to get a date based on Timezone and I was nearly successful with that. But I got an issue when I incorporate that query with the stored procedure.
Here is the code which gives me the correct output.
DECLARE
#TimeZone VARCHAR(100) = 'India Standard Time'
declare #EndDate DATETIME = (SELECT (CONVERT( datetime,SYSDATETIMEOFFSET() AT TIME ZONE #TimeZone)))
select #EndDate
and the output is (correct o/p)
2019-12-23 20:43:18.020
Then I incorporate it with a stored procedure
which comes under an if condition
O/P is like this
Dec 23 2019 8:38PM
can anybody help me with this
The value of the variable looks correct but just shown in different format. You can force it to show in the format you want:
select CONVERT(nvarchar(100),#EndDate,120) as EndDate;

Find Weekending Date (Friday) based on another Date column in SQL Server?

I am trying to replicate a weekending query that I use in access, in SQL Server and not having much luck with getting ti to perform how I need it to.
Basically, I need to find the week-ending date (with the week-ending date being a Friday) of another date column and would like it formatted to be American short date (eg. 10/06/2017).
I use the below in Access which gets me the result I need. So if the ACTUAL_DATE is 10/03/2017, the result I would need is 10/06/2017.
Actual_Date_WE: [ACTUAL_DATE] + 7 - Weekday([ACTUAL_DATE], 7)
Thank you! :)
try this
Declare #Date date
SET #Date = '2017-10-01'
select dateadd(day, (7+(6 - datepart(WEEKDAY, #date))) %7, #date)
declare #i date='10/3/2017'
declare #Friday int=6
SELECT convert(varchar,dateadd(day,(#Friday-DATEPART(dw,#i)),#i) ,110)

SQL Server 2008 script - how to to acquire current date from system and store it into a date column

Hey fellas, I'm having difficulty obtaining only the date from the system and inserting it into a column, is there a built-in function that can acquire it?
On top of that, how do I add years to the current date?
I know I'm pushing it right now, but I'm also wondering what's the format for the date datatype?
Because sometimes I'd like to manually insert values into a column with that type in mind.
Any help would greatly be appreciated.
Thanks.
To get date only (SQL Server 2008 only) CAST to date type
SELECT CAST(GETDATE() AS date)
To add years, use DATEADD
SELECT DATEADD(year, 2, CAST(GETDATE() AS date))
Formats: use yyyymmdd or ISO yyyy-mm-dd (for newer datetime types) for safety.
Read this for everything about date+time in SQL Server
To add a year to the current date, look at the dateadd() function.
To just get the date from sql w/o the time, you can do this:
DECLARE #Date DATETIME
SELECT #Date = CONVERT(VARCHAR, GETDATE(), 101)
SELECT #Date
Sql will implicity convert the VARCHAR back to DATETIME. Look up the CONVERT function in BOL and it will give you all kinds of different styles for the 3rd parameter.
Bender

How to return the miliseconds of a GetDate() function of T-SQL?

I have this query:
UPDATE Player
SET move = GETDATE()
and it return this:
09/12/2010 13:43:51
But I want the miliseconds. How can I do this?
My move column type is DateTime.
Thank you.
GETDATE() does give milliseconds
If move does not have milliseconds stored then it:
it isn't datetime
it one of the SQL Server 2008 types with incorrect precision
Edit: if column is datetime then it's probably the client tools are not showing milliseconds
What does SELECT * FROM Player show in a query pane? Are you using the SSMS grids: these use your client settings which will lose milliseconds
If it is a DateTime then it will have stored a fractional part, but you will need to format the output to show them.
Well, I have no ideia why, but the solution is this:
SELECT playerId, (SELECT CONVERT(varchar(23), GETDATE(), 121) AS Expr1) AS Expr1
FROM Player
DateTime data type by default has milliseconds information
declare #myDate datetime;
Select #myDate=getdate();
Select #myDate;
will output
2010-12-09 21:03:28.243
What needs to be checked is the Select query you are using to get the output of
09/12/2010 13:43:51
Moreover what version of MS-SQL are you using?
Edit & Snip : Just a hunch, but if you are using SQL 2000 Query Anaylyzer the setting Tools-Options-Connections Use regional settings when displaying number, date and times might be useful

SQL Server function to return minimum date (January 1, 1753)

I am looking for a SQL Server function to return the minimum value for datetime, namely January 1, 1753. I'd rather not hardcode that date value into my script.
Does anything like that exist? (For comparison, in C#, I could just do DateTime.MinValue)
Or would I have to write this myself?
I am using Microsoft SQL Server 2008 Express.
You could write a User Defined Function that returns the min date value like this:
select cast(-53690 as datetime)
Then use that function in your scripts, and if you ever need to change it, there is only one place to do that.
Alternately, you could use this query if you prefer it for better readability:
select cast('1753-1-1' as datetime)
Example Function
create function dbo.DateTimeMinValue()
returns datetime as
begin
return (select cast(-53690 as datetime))
end
Usage
select dbo.DateTimeMinValue() as DateTimeMinValue
DateTimeMinValue
-----------------------
1753-01-01 00:00:00.000
Have you seen the SqlDateTime object? use SqlDateTime.MinValue to get your minimum date (Jan 1 1753).
As I can not comment on the accepted answer due to insufficeint reputation points my comment comes as a reply.
using the select cast('1753-1-1' as datetime) is due to fail if run on a database with regional settings not accepting a datestring of YYYY-MM-DD format.
Instead use the select cast(-53690 as datetime) or a Convert with specified datetime format.
Enter the date as a native value 'yyyymmdd' to avoid regional issues:
select cast('17530101' as datetime)
Yes, it would be great if TSQL had MinDate() = '00010101', but no such luck.
Here is a fast and highly readable way to get the min date value
Note: This is a Deterministic Function, so to improve performance further we might as well apply WITH SCHEMABINDING to the return value.
Create a function
CREATE FUNCTION MinDate()
RETURNS DATETIME WITH SCHEMABINDING
AS
BEGIN
RETURN CONVERT(DATETIME, -53690)
END
Call the function
dbo.MinDate()
Example 1
PRINT dbo.MinDate()
Example 2
PRINT 'The minimimum date allowed in an SQL database is ' + CONVERT(VARCHAR(MAX), dbo.MinDate())
Example 3
SELECT * FROM Table WHERE DateValue > dbo.MinDate()
Example 4
SELECT dbo.MinDate() AS MinDate
Example 5
DECLARE #MinDate AS DATETIME = dbo.MinDate()
SELECT #MinDate AS MinDate
It's not January 1, 1753 but select cast('' as datetime) wich reveals: 1900-01-01 00:00:00.000 gives the default value by SQL server.
(Looks more uninitialized to me anyway)
This is what I use to get the minimum date in SQL Server. Please note that it is globalisation friendly:
CREATE FUNCTION [dbo].[DateTimeMinValue]()
RETURNS datetime
AS
BEGIN
RETURN (SELECT
CAST('17530101' AS datetime))
END
Call using:
SELECT [dbo].[DateTimeMinValue]()
What about the following?
declare #dateTimeMin as DATETIME = datefromparts(1753, 1, 1);
select #dateTimeMin;
The range for datetime will not change, as that would break backward compatibility. So you can hard code it.

Resources