SQL Server 2008 Date parameter - sql

I'm passing a start date and end date parameter to my stored procedure. I'm doing a simple test here:
DECLARE #StartDate DATE = '10/06/2013' --dd/mm/yyyy
SELECT #StartDate -- this statement running successfully
DECLARE #EndDate DATE = '30/06/2013' --dd/mm/yyyy
SELECT #EndDate -- this statement giving error
This statement returns the following error
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
Does anybody have idea what's going wrong with EndDate?

I'm pretty sure the error is on this line:
DECLARE #EndDate DATE = '30/06/2013' --dd/mm/yyyy
Not on the SELECT. It wouldn't make sense that it would be on the SELECT, because processing a variable should be fine.
I would recommend that you use YYYYMMDD formats. The following is my preference:
DECLARE #EndDate DATE = '2013-30-06' ;
However, it can fail for certain internationalization settings. The following is documented to always work:
DECLARE #EndDate DATE = '20133006' ;

'10/06/2013' means 06-Oct-2013 not 10-Jun-2013. There is nothing exists with month 30 as in your #EndDate '30/06/2013'.
DECLARE #StartDate DATE='10/06/2013'
DECLARE #DummyDate DATE = '2013-Oct-06'
IF #StartDate = #DummyDate
BEGIN
SELECT 1
END

By default sql server takes date value in 'yyyy-mm-dd' format,
So you need to follow that format or you need to convert date format accordingly.
T-SQL convert string to datetime - SQL Server convert string to date
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
-- mon types are nondeterministic conversions, dependent on language setting
SELECT convert(datetime, '23 OCT 2016', 106) -- dd mon yyyy
SELECT convert(datetime, 'Oct 23, 2016', 107) -- mon dd, yyyy
-- SQL string to datetime conversion without century - some exceptions
SELECT convert(datetime, '10/23/16', 1)   -- mm/dd/yy
SELECT convert(datetime, '16.10.23', 2)   -- yy.mm.dd
SELECT convert(datetime, '23/10/16', 3)   -- dd/mm/yy
SELECT convert(datetime, '23.10.16', 4)   -- dd.mm.yy
SELECT convert(datetime, '23-10-16', 5)   -- dd-mm-yy
SELECT convert(datetime, '23 OCT 16', 6)  -- dd mon yy
SELECT convert(datetime, 'Oct 23, 16', 7) -- mon dd, yy

Related

Convert a string to a date in sql

how do I convert this:
DECLARE #FromDate varchar = 'Feb 2020'
to a Date like this
DD/MM/YYYY or DDMMYYYY
I need to sort some dates and from the combobox you can just choose those three : Dez 2019, Jan 2020 or Feb 2020.
So I thought I could do something like that:
DECLARE #FromDate varchar = 'Feb 2020';
SELECT RIGHT(convert(varchar, CONVERT(date, CONVERT(varchar, PIN_DATE)), 106),8)
FROM PIN
WHERE RIGHT(CONVERT(varchar, CONVERT(date, CONVERT(varchar, PIN_DATE)), 106),8)
= CONVERT(datetime, #FromDate,106)
The date in PIN_DATE looks like this : YYYYMMDD aka. 20201231
This is an ERROR:
DECLARE #FromDate varchar = 'Feb 2020'
This assigns #FromDate as a SINGLE character, so the value is 'F'.
When using the character types in SQL Server, always use a length:
DECLARE #FromDate varchar(255) = 'Feb 2020'

MS SQL Server Convert Date

I have a Date column, within it, dates are in two formats, see below.
Date
------------------------
2/28/2017 10:00
2017-03-15 10:00:00
I want to convert the dates that are mm/dd/yyyy to yyyy-mm-yy within the date column, however, my script is not converting rows that are mm/dd/yyyy.
I am not sure what I else I might be missing or doing wrong.
My script
SELECT
CONVERT(NVARCHAR, DateColumn, 120) AS [Receipt Date]
FROM
databasename.dbo.sales
Review your SQL Statement NVARCHAR needs a declaration on lenght: NVarchar(10)
declare #mydate datetime
set #mydate = GETDATE();
select CONVERT(nvarchar(10),#mydate,120)
Use
SET DateFormat dmy
Or
SET LANGUAGE French;
Then
declare #mydate datetime
set #mydate = GETDATE();
select CONVERT(nvarchar(10),#mydate,12
0)

ADD time 23:59:59.999 to end date for between

I have been having an issue with using the following:
Column_Name BETWEEN #StartDate AND #EndDate.
This is because the #EndDate = 00:00:00.000 for the time, which doesn't pick up all the values for that day.
How would I convert the #EndDate (Always 00:00:00.000) to always be Date + 23:59:59.999?
One option that avoids needing to add EndDate + 23:59:59.999 is to not use the between comparison and instead use column_name >= #StartDate and column_name < #EndDate +1
Please note the accuracy and rounding of the DATETIME type in SQL Server 2005:
datetime values are rounded to increments of .000, .003, or .007 seconds
SQL Server 2008 introduced the DATETIME2 type which has an accuracy of 100 nanoseconds. So in SQL Server 2008 you could do:
DECLARE #d DATETIME = '2011-10-07 00:00:00.000'
SELECT DATEADD(MS, -1, DATEADD(D, 1, CONVERT(DATETIME2, #d)))
Alternatively you may want to avoid the BETWEEN operator in this case:
#StartDate <= Column_Name AND Column_Name < DATEADD(D, 1, #EndDate)
Since the advent of datetime2 datatype, I have been struggling with this problem. To calculate the end of day as a datetime2 datatype I add the number of seconds in a day to the =date= then subtract 100 nanoseconds. Voila:
declare #bod datetime2
declare #eod datetime2
set #bod = cast (GETDATE() as DATE)
set #eod = DATEADD(ns, -100, DATEADD(s, 86400, #bod))
print #bod
print #eod
-- answer:
2013-12-01 00:00:00.0000000
2013-12-01 23:59:59.9999999
Now I'm off to datetimeoffset data type.
You can change the time in a date like this (I'm using getdate() as an example):
select cast(convert(char(8), getdate(), 112) + ' 23:59:59.99' as datetime)
Explanation:
convert(char(8), getdate(), 112) converts the date to yyyymmdd format (as string).
Then you can just append the desired time, and convert the whole string to datetime again.
EDIT:
It slows the performance when you do the casting on a database column, yes.
But he has a datetime variable and he just uses the casting to change the time in the variable once
--> I see no performance issue if he uses my code to change his #EndDate variable.
Valid point, however. Casting is not a good solution in all situations.
You could also do this:
select #endDate = dateadd(ms,-3,dateadd(day,1,DATEADD(dd, DATEDIFF(dd,0,#endDate), 0)))
when #endDate is '5/3/2013'
--Execution / post date is 1 Feb. 2020
-- sql server this month start and end
select DATEADD(month, DATEDIFF(month, 0, getdate()), 0) -- 2020-02-01 00:00:00.000
select DATEADD(second,-1, datediff(day,0,EOMONTH(getdate()))+1) -- 2020-02-29 23:59:59.000
-- sql server this day start and end
select DATEADD(day, DATEDIFF(day, 0, getdate()), 0) -- 2020-02-01 00:00:00.000
select DATEADD(second,-1, datediff(dd,0,getdate())+1) -- 2020-02-01 23:59:59.000
-- sql server last 30 days start and end
select DATEADD(day, -30, DATEDIFF(day, 0, getdate())) -- 2020-01-02 00:00:00.000
select DATEADD(second,-1, datediff(dd,0,getdate())+1) -- 2020-02-01 23:59:59.000
You can use between if your end date is set to 00:00:00 of the next day:
ColumnName between #StartDate and convert(datetime, convert(date, #EndDate + 1))
This converts the next day to a date, which removes the hours information, then you convert it back to a datetime which adds default hour information: 00:00:00.
You can use any variant of the date from parts functions. For example, let's use DATETIMEFROMPARTS:
DECLARE #d DATETIME2(0) = '2011-10-07 04:32:12.000';
SELECT DATETIMEFROMPARTS(YEAR(#d), MONTH(#d), DAY(#d), 23, 59, 59, 0);
-- 2011-10-07 23:59:59
I first convert the original datetime to begin of the day, then add hours and seconds to it:
DECLARE #start DATETIME, #end DATETIME
SET #start = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
SET #end = DATEADD(HOUR, 23, DATEADD(n, 59, #start))
PRINT #start
PRINT #end
Oct 27 2017 12:00AM
Oct 27 2017 11:59PM

Date format date-month-year

I have a table which has a datetime column. I want to show date in date-month-year forma.I am using SQL Server 2008.
You need to convert datetime column
select convert(varchar,datecolumn,103) from yourtable
Some datetime convertions:
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]
/* YYYY/MM/DD
2015/07/11 */
SELECT CONVERT(VARCHAR(10), GETDATE(), 112) AS [YYYYMMDD]
/* YYYYMMDD
20150711 */
-- SQL convert date string to datetime - time set to 00:00:00.000 or 12:00AM
PRINT CONVERT(datetime,'07-10-2012',110) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'2012/07/10',111) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'20120710', 112) -- Jul 10 2012
You can learn all DateTime convertion from here
FORMAT can be used for this;
SELECT FORMAT(GETDATE(), 'dd-MM-yyyy') --20-09-2021
in your case;
SELECT FORMAT(datetime, 'dd-MM-yyyy') AS datetime --20-09-2021
Edit;
Sorry to inform that I've just seen you mentioned SQL Server 2008 this code works for SQL Server 2012 for those who use 2012 and ends up in here!

str_to_date function in sql server?

MySQL has a function called STR_TO_DATE, that converts a string to date.
Question:
Is there a similar function in SQL Server?
If you need to parse a particular format, use CONVERT(datetime, #mystring, #format). Use this as a reference: https://web.archive.org/web/20200729210252/http://www.sqlusa.com/bestpractices/datetimeconversion/
Some examples:
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd ANSI date with century
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
-- mon types are nondeterministic conversions, dependent on language setting
SELECT convert(datetime, '23 OCT 2016', 106) -- dd mon yyyy
SELECT convert(datetime, 'Oct 23, 2016', 107) -- mon dd, yyyy
-- 2016-10-23 00:00:00.000
SELECT convert(datetime, '20:10:44', 108) -- hh:mm:ss
-- 1900-01-01 20:10:44.000
What if the string is 7/7/2010?
Then use CONVERT with either 101 (mm/dd/yy) or 103 (dd/mm/yy) depending on what you want:
SELECT CONVERT(DATE, '7/7/2010', 103)
Result:
2010-07-07
CAST(<string> AS DATETIME)
Use CAST.
declare #MyString varchar(10)
declare #MyDate datetime
set #MyString = '2010-08-19'
set #MyDate = cast(#MyString as datetime)
select #MyDate
Here is a good example:
declare #myDate datetime
set #myDate = '06/09/2017'
select concat(convert(varchar(20), #myDate,101), ' -- ',
convert(varchar(20), #myDate,103), ' -- ',
convert(varchar(20), #myDate,6))
This is what you get, depending on 101 or 103 or 6:
09/06/2017 -- 06/09/2017 -- 06 Sep 17
A good summary of types of dates is here - https://www.w3schools.com/sql/func_convert.asp
On MSSQL:
select cast('2012/06/12 10:32AM' as datetime);
You will get it:
2012-06-12 10:32:00.000