conversion failed when converting nvarchar to int - sql-server-2008

I am getting above error can anyone please help what is the issue with the code...
P.YKey BETWEEN YEAR('''+ CONVERT(VARCHAR(50), #BDate, 103) +''')
AND YEAR('''+ CONVERT(VARCHAR(50), #eDate, 103) +''')
AND P.DCreation BETWEEN '''+ CONVERT(VARCHAR(50), #BDate, 103) +'''
AND '''+ CONVERT(VARCHAR(50), #eDate, 103) +''''

What is the type of #BDate, #eDate?
Update:
As the type of the two variables are DATETIME, the conversion is not necessary.
P.YKey BETWEEN YEAR(#BDate)
AND YEAR(#eDate)
AND P.DCreation BETWEEN #BDate
AND #eDate

as it seems that you are interested on the creation date only, not the time, I would also cast the datetimes to date. So assuming that DCreation is datetime too:
P.YKey BETWEEN YEAR(#BDate)
AND YEAR(#eDate)
AND cast(P.DCreation as date) BETWEEN cast(#BDate as date)
AND cast(#eDate as date)

Related

SSRS Conditional default StartDate

I am attempting to set the default start date of a report to the previous business day, which is
dateadd(dd,-1,getdate())
on all days except Monday when it is
dateadd(dd,-3,getdate())
Prior to this report being converted to an SSRS report this was handled in the proc with the query
declare #startDate datetime =
(
select
case
when DATENAME(dw, CONVERT(date, getdate())) = 'Monday' then cast(Convert(date, dateadd(DD,-3,CONVERT(date, getdate()))) as nvarchar(100))
else cast(Convert(date, dateadd(DD,-1,#today)) as nvarchar(100))
end
)
How would I implement this conditional start date using an SSRS =IIF statement?
Thanks
Upon more research I've discovered the method to do this is:
=DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())
I would use switch personally:
=Switch(
WeekdayName(Weekday(today))="Monday",dateadd(DateInterval.Day,-3,today),
True,dateadd(DateInterval.Day,-1,today)
)

DATEDIFF() seconds with millisecond to float or decimal

How to convert the below code to float or decimal type?
SELECT DATEDIFF(ss, StartTime, GETDATE()) + '.' +
DATEDIFF(ms, StartTime, GETDATE())
This is what I needed. CONVERT(float, DATEDIFF(ms, StartTime, GETDATE()) / 1000.0)
The code i my question was completely wrong because DATEDIFF(ms, StartTime, GETDATE()) returns total number of milisecond between the two dates and not as i thought only difference in the milliseconds part.
The code im my question would work if I used the DATEPART instead DATEDIFF in both expressions:
DATEPART(ss, GETDATE()) - DATEPART(ss, #StartTime) + '.'
+ DATEPART(ms, GETDATE()) - DATEPART(ms, #StartTime)

How to get a particular date format ('dd-MMM-yyyy') in SELECT query SQL Server 2008 R2

I am using CONVERT(data_type(length),expression,style) function to change the format of a date in a SELECT query.
Declare #dt nvarchar(20)
Select #dt = Convert(nvarchar(20), SalesDate, 113) FROM SalesTable
The format I need is 'dd-MMM-yyyy' (eg. '05-Jul-2013') but I could not find the proper style number (eg. 113) for this particular format. Can any one help me with that please?
Try this:
Declare #dt NVARCHAR(20)
Select
#dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ')
FROM SalesTable
select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]
or else
select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')
both works fine
It doesn't look like DD-MMM-YYYY is supported by default (at least, with dash as separator). However, using the AS clause, you should be able to do something like:
SELECT CONVERT(VARCHAR(11), SYSDATETIME(), 106) AS [DD-MON-YYYY]
See here: http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
I Think this is the best way to do it.
REPLACE(CONVERT(NVARCHAR,CAST(WeekEnding AS DATETIME), 106), ' ', '-')
Because you do not have to use varchar(11) or varchar(10) that can make problem in future.
SELECT Convert(varchar(10),CONVERT(date,'columnname',105),105) as "end";
OR
SELECT CONVERT(VARCHAR(10), CAST(event_enddate AS DATE), 105) AS [end];
will return the particular date in the format of 'dd-mm-yyyy'
The result would be like this..
04-07-2016
select convert(varchar(11), transfer_date, 106)
got me my desired result of date formatted as 07 Mar 2018
My column transfer_date is a datetime type column and I am using SQL Server 2017 on azure
select FORMAT(getdate(), 'dd-MMM-yyyy') as DateToday
Other answers here seem to return columns that are quite wide.
This answer returns a varchar(11) which is all that is required for a date in dd-Mon-yyyy format.
'SalesDate' = CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),SalesDate, 106), ' ', '-')),
Try also:
select CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),getdate(), 106), ' ', '-'))
As of today, this gives a result as per: 22-Jul-2021

Issue in SQL Server 2008 while converting varchar to date

I have a nvarchar column and it is containing dates in different formats like
'2/1/2012',
'2/2/12',
'20 01 12',
'20 03 2012',
'20.01.12',
'30.04.2012',
'20jan 12',
'20-MARCH-2012',
'22MARCH2012',
'23 may 2012',
'23-MAR-2012',
'26MAR-2012',
'27TH JAN 4660',
'CHL. Date- 30.01.2012',
'APRIL/12/2012',
'N/A',
'DT.:5/1/12',
Now I want to insert the data from this column to date column in format dd/mm/yyyy but not able to do that as it always give error
Conversion failed when converting date and/or time from character string
Please help if anyone knows how to convert this column to date column with all specified values defined above
To convert a nvarchar column to a DATETIME, you will need to use the CONVERT function in T-SQL.
This function supports a set of "styles" - all of which are very well documented on MSDN.
If your source string matches one of those defined styles, you can use the appropriate CONVERT to get a DATETIME from your string. There is however no "magic" in T-SQL to recognize which conversion style would match your string - that's entirely up to you.
With this query here, you can list out all the CONVERT styles for T-SQL:
DECLARE #Today DATETIME = GETDATE()
SELECT
Default_100 = CONVERT(VARCHAR(50), #Today, 100),
US_101 = CONVERT(VARCHAR(50), #Today, 101),
ANSI_102 = CONVERT(VARCHAR(50), #Today, 102),
BritishFrench_103 = CONVERT(VARCHAR(50), #Today, 103),
Germany_104 = CONVERT(VARCHAR(50), #Today, 104),
Italian_105 = CONVERT(VARCHAR(50), #Today, 105),
Style106 = CONVERT(VARCHAR(50), #Today, 106),
Style107 = CONVERT(VARCHAR(50), #Today, 107),
Style108 = CONVERT(VARCHAR(50), #Today, 108),
Default_with_ms_109 = CONVERT(VARCHAR(50), #Today, 109),
USA_110 = CONVERT(VARCHAR(50), #Today, 110),
Japan_111 = CONVERT(VARCHAR(50), #Today, 111),
ISO_112 = CONVERT(VARCHAR(50), #Today, 112),
Europe_default_with_ms_113 = CONVERT(VARCHAR(50), #Today, 113),
Style114 = CONVERT(VARCHAR(50), #Today, 114),
ODBC_canonical_120 = CONVERT(VARCHAR(50), #Today, 120),
ODBC_canonical_with_ms_121 = CONVERT(VARCHAR(50), #Today, 121),
ISO_8601_126 = CONVERT(VARCHAR(50), #Today, 126),
ISO_8601_with_timezone_Z_127 = CONVERT(VARCHAR(50), #Today, 127),
Hijri_130 = CONVERT(VARCHAR(50), #Today, 130),
Hijri_131 = CONVERT(VARCHAR(50), #Today, 131)
If your source string does not match any of the predefined styles - you're out of luck, and it'll take a lot more string parsing and T-SQL code to convert your string to a valid DATETIME.
If your string matches one of the styles, you can convert it like this:
DECLARE #Date DATETIME
SET #Date = CONVERT(DATETIME, '30.04.2012', 104)
The best way is to write a function that takes the string and converts it to a non ambiguous date string (yyyy-mm-dd). This can then be converted to a datetime. There's now magical way of converting various formats to date format.

Getting the Starting and ending date of week? In Sql server?

In sql, how to get the starting and ending date of week for the particular month. Can you help me.
The following will work whatever you consider the first day of the week (sunday, monday etc), just ensure you use SET DATEFIRST if you want to change from the default. SET DATEFIRST 1 will make the first day of the week monday.
SELECT DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekStart],
DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekEnd]
EDIT
I have just re-read you request and I think you may be after something different to what I have given above. If you want the day of the week of the first and last of the month this will do the trick:
SELECT DATENAME(WEEKDAY, DATEADD(DAY, 1 - DATEPART(DAY, GETDATE()), GETDATE())) [FirstDayOfMonth],
DATENAME(WEEKDAY, DATEADD(DAY, - DATEPART(DAY, DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))) [LastDayOfMonth]
Your requirement is unclear: what do you consider to be the first and last days of a week? Sunday and Saturday? Monday and Friday? Monday and Sunday? But the best solution for many date-related queries is to create a calendar table. That gives you the ability to easily set the first and last days of weeks, months and years according to your definition and even in multiple formats if necessary.
This is especially useful if you work with concepts like financial years, business days, and so on where the definitions vary by country and even company. It is also the easiest solution when there are exceptions to your logic, e.g. the first week of the year has different rules than a 'normal' week.
With a calendar table you could pre-populate the first and last days of the week and then your query could be something like this:
-- first/last days stored as dates in their own columns
select FirstDayOfThisWeek, LastDayOfThisWeek
from dbo.Calendar
where BaseDate = #SomeDate
-- first/last days stored as bit flags
select max(BaseDate)
from dbo.Calendar
where BaseDate <= #SomeDate and IsFirstDayOfWeek = 0x1
Below is a case statement you can use to create a Weekbegin for your date value. It will make your weeks start on Monday or Tuesday or Wed... etc. dependant on what day of the week you have set as #pDate.
Create parameters
DECLARE #pDate Date = NULL --Replace with your date, which will also be the first day of the week
DECLARE #pDatePart SMALLINT = DATEPART(dw, #pDate)
Then put this case Statement after your select
CASE
WHEN DATEPART(dw, CAST(DATEVALUE AS DATE)) BETWEEN #pDatePart AND 7 THEN DATEADD(d, (DATEPART(dw, CAST(DATEVALUE AS DATE))*-1)+#pDatePart, CAST(DATEVALUE AS DATE))
WHEN DATEPART(dw, CAST(DATEVALUE AS DATE)) BETWEEN 1 AND #pDatePart-1 THEN DATEADD(d, (DATEPART(dw, CAST(DATEVALUE AS DATE))*-1)+(#pDatePart-7), CAST(DATEVALUE AS DATE))
END
AS DynamicWeekBegin
You can always get the final date of the week, just by using dateadd(d, 6, THE CASE STATEMENT)
Here i given
Week,month,quarter,half year,year start and end date.
Select CONVERT(varchar(50), GETDATE(),105) 'GETDATE' ,
CONVERT(varchar(50), DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)),105) [WeekStart],
CONVERT(varchar(50),DATEADD(DAY, 8 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) ,105)[WeekEnd],
CONVERT(varchar(50),DATEADD(dd, -DAY(getdate()) + 1, getdate()),105) MonthStart,
CONVERT(varchar(50),DATEADD(dd, -DAY(DATEADD(mm, 1, getdate())), DATEADD(mm, 1, getdate())),105) MonthStart,
CONVERT(varchar(50), DATEADD(q, DATEDIFF(q, 0, GETDATE()), 0),105) AS 'QStart Date',
CONVERT(varchar(50), DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, GETDATE()) + 1,0)),105) AS 'QEnd Date',
CONVERT(varchar(50), CAST(CAST(((((MONTH(GETDATE()) - 1) / 6) * 6) + 1) AS VARCHAR) + '-1-' + CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME),105) StartOfHalfYear,
CONVERT(varchar(50), CAST(CAST(((((MONTH(GETDATE()) - 1) / 6) * 6) + 6) AS VARCHAR) + '-1-' + CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME),105)EndOfHalfYear,
CONVERT(varchar(50), DATEADD(yy, DATEDIFF(yy,0,getdate()), 0),105) AS StartOfYear,
CONVERT(varchar(50), DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1),105) AS EndOfYear