I tried the below sample Function ... and i tried in many other ways but still i am getting the Error as "Conversion failed when converting Datetime from character string".. All i am trying is from this function i am returning a Datetime but somewhere here its still string ...
Please advise
declare #V_Year nvarchar (4), #Test datetime , #t1 as datetime , #t2 as datetime
declare #Mon varchar(5) , #dayz varchar (5) , #GMTStart varchar(20)
set #Mon = '03'
set #t1 = cast ( #Mon as datetime)
set #dayz = '01'
set #t2 = cast ( #dayz as datetime)
SELECT #V_Year = DATEPART(year, getdate())
set #GMTStart = #Mon + '-' + #dayz + '-' + convert(nvarchar,#V_Year)
set #Test = Cast(#GMTStart as datetime)
select #Test
Your code is creating a datetime value of March 3 in the current year.
The interpretation of date strings is dependent on set dateformat unless you use the format yyyymmdd.
Removing all the stuff from your code that does not seem to do anything useful will leave you with this.
select cast(cast(year(getdate()) as char(4))+'0301' as datetime)
I've changed your code a bit:
declare
#V_Year nvarchar (4),
#Test datetime ,
#t1 as datetime ,
#t2 as datetime
declare
#Mon varchar(8), --here need to be varchar(8) because datetime has 8 digits
#dayz varchar(5),
#GMTStart varchar(20)
set #Mon = '20130101' --here you must provide full date, not a part, because is not possible to convert just part of date into full date
set #t1 = cast (#Mon as datetime) --this will work with
--set #dayz = '01'
--set #t2 = cast (#dayz as datetime) --this line not work with just day, must provide full date
SET #V_Year = DATEPART(year, getdate()) --here first set a variable, and then select
SELECT #V_Year
set #GMTStart = #Mon + '-' + #dayz + '-' + convert(nvarchar, #V_Year)
set #Test = Cast(#GMTStart as datetime)
select #Test
Related
Take a look at this code portion.
DECLARE #SQL VARCHAR(MAX), #DateWithDotsVARCHAR(10)
SELECT #DateWithDots = REPLACE(#Date, '-', '.')
IF OBJECT_ID('tempdb..##WL_Klijenti') IS NOT NULL
DROP TABLE ##WL_Klijenti
SELECT #SQL = '
SELECT *
INTO ##WL_Klijenti
FROM OPENROWSET (''SQLOLEDB'',''Server=
(local);TRUSTED_CONNECTION=YES;'',''SET FMTONLY OFF; SET NOCOUNT ON;
EXEC
'+DB_NAME()+'.dbo.sp_kbbl_WachLista_Priprema ''''' + #DateWithDots+
''''', ''''' + #DateWithDots + ''''', 0'')
AS tbl'
... the rest is less important
Second dateWithDots is not needed as user input but here I will have to pass the
last day of the last year instead, in reference to the first #dateWithDots user will input.
(This is due to some balance sheet calculations, everything works fine here I just have to set this adjustment.)
So somehow I will have to identify the current year beforehand, YEAR can be taken from the first #DateWithDots as this is requested user input parameter.
How can it be accomplished ?
SOLUTIONS:
#Cool_Br33ze's approach...
DECLARE #dateWithDots NVARCHAR(10)
SET #dateWithDots = '2018.01.18' --<< User Inputted date
SELECT LastDayLastYear = CAST(DATEADD(YEAR,
DATEDIFF(YEAR, -1, CAST(#dateWithDots AS DATE) )-1, -1) AS DATE)
My approach...
DECLARE #LastDay VARCHAR(MAX)
SET #LastDay = CONVERT(VARCHAR(4),SUBSTRING(#Datum,1,4)-1) + '.12' + '.31';
USE the correct the Datatypes for Dates
DECLARE #dateWithDots DATE
SET #dateWithDots = GETDATE() --<< User Inputted date
SELECT LastDayLastYear = CAST(DATEADD(YEAR,
DATEDIFF(YEAR, -1, #dateWithDots )-1, -1) AS DATE)
Returns
2017-12-31
Using NVARCHAR for Dates
DECLARE #dateWithDots NVARCHAR(10)
SET #dateWithDots = '2018.01.18' --<< User Inputted date
SELECT LastDayLastYear = CAST(DATEADD(YEAR,
DATEDIFF(YEAR, -1, CAST(#dateWithDots AS DATE) )-1, -1) AS DATE)
Returns
2017-12-31
--Using Dynamic SQL - NVARCHAR is preferred over VARCHAR
DECLARE #SQL NVARCHAR(MAX) ;
SET #SQL = N'SELECT CAST(DATEADD(YEAR,DATEDIFF(YEAR, -1, CAST(#dateWithDots AS DATE) )-1, -1) AS DATE)'
EXEC sys.sp_executesql #SQL, N'#dateWithDots NVARCHAR(10)', #dateWithDots
i have a stored procedure like this:
ALTER procedure [dbo].[startandenddtime]
#startdate nvarchar(100)
as begin
declare #date3 nvarchar(100) = cast(CONVERT(varchar(100), #startdate + ' 16:59:59', 120) as datetime)
select date3 as startdate
end
If i pass my startdate as 2013-05-08 i am getting out put as :
but i want to get out put as 2013-05-08 16:59:59.000..so how i can convert this format
is this date time i can store in nvarchar varibale
Try like this
DECLARE #StartDate NVARCHAR(100)='2013-05-08'
SELECT CONVERT(VARCHAR,#StartDate+'16:59:59', 121)
MORE
SQL Fiddle
try this
ALTER procedure [dbo].[startandenddtime]
#startdate nvarchar(100)
as begin
declare #date3 nvarchar(100) = cast(CONVERT(VARCHAR(24),#startdate ,121)) as datetime)
select date3 as startdate
end
For more in details see this, http://technet.microsoft.com/en-us/library/ms187928.aspx
try this
Select cast(CONVERT(varchar(100), #startdate + ' 16:59:59') as date)
Or
Select cast(CONVERT(varchar(100), #startdate + ' 16:59:59') as datetime)
Try this :
Declare #dt varchar(20);
Declare #startdate varchar(20)='2013-05-08';
SET #dt= CONVERT(VARCHAR(20), CONVERT(datetime,#startdate+' 16:59:59'), 100)
Select #dt;
Just don't cast varchar value to datetime
edit: If you don't get what i meant in my comment:
convert(varchar, convert(datetime, #startdate + ' 16:59:59', 120), 120)
I've created the following stored procedure on sql:
CREATE PROCEDURE Sp_generate_report (#YEAR INT,
#MONTH INT)
AS
DECLARE #CATEGORY VARCHAR(20)
DECLARE #BEGIN_DATE DATE
DECLARE #END_DATE DATE
BEGIN
SELECT #BEGIN_DATE = Cast(( Cast(#YEAR AS VARCHAR) + '-'
+ Cast(#MONTH AS VARCHAR) + '-' + '1' ) AS DATE)
SELECT #END_DATE = Cast(( Cast(#YEAR AS VARCHAR) + '-' +
Cast(#MONTH AS VARCHAR) + '-' + '32' ) AS DATE)
SELECT TOP 1 #CATEGORY = Name
FROM dbo.Profitible_categories(#BEGIN_DATE, #END_DATE)
INSERT INTO dbo.MONTHLY_SUMMARY_REPORTS
VALUES (#CATEGORY)
END
The procedure was created successfully, but when I try to execute it with the following command:
EXECUTE SP_GENERATE_REPORT 2012, 7
I get this error message:
Msg 241, Level 16, State 1, Procedure SP_GENERATE_REPORT, Line 8
Conversion failed when converting date and/or time from character
string.
thanks in advance for the help.
You can calculate the last day of the month without all that nasty string concatenation or trying to guess which month it is and pick the last day.
CREATE PROCEDURE dbo.Sp_generate_report
#YEAR INT,
#MONTH INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE #BEGIN_DATE DATE = DATEADD(MONTH, #MONTH-1, CONVERT(DATE,
CONVERT(CHAR(4), YEAR) + '0101'));
DECLARE #END_DATE DATE = DATEADD(DAY, -1, DATEADD(MONTH, 1, #BEGIN_DATE));
INSERT INTO dbo.MONTHLY_SUMMARY_REPORTS
SELECT TOP (1) Name
FROM dbo.Profitible_categories(#BEGIN_DATE, #END_DATE);
END
GO
I want to select from table where date with between
but problem is that in database there is not datetime column there is separated dateTime year, month and day
does it possible to create new variable inside stored procedure something like that?
checkin = new DateTime(year, month, day)
this year month and day are columns in datatable
You can convert string to DateTime using the function below:
convert(datetime, '02/15/2012', 101) -- mm/dd/yyyy
You could try something like:
-- dateadd formula borrowed from
-- http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx
with includeDate as (
select column1,
column2,
column3,
dateadd(month,(([year]-1900)*12)+[month]-1,[day]-1) as date
from yourTable
)
select *
from includeDate
where date between #startDate and #endDate
Of course in your stored procedure, you would populate the query with the values from your table, the code below is an example of converting the separate date fields into one datetime value.
I don't know the data type of your day, month and year fields but the following should work if you have an int value:
declare #day int
declare #month int
declare #year int
declare #fulldate smalldatetime
set #day = '1'
set #month = '9'
set #year = '2012'
SELECT #fulldate = Convert(smalldatetime, Cast(#month as varchar(2)) + '/'
+ Cast(#day as varchar(2)) + '/' + Cast(#year as varchar(4)), 101)
select Convert(varchar(10), #fulldate, 101)
If the values are stored as a string:
declare #day varchar(2)
declare #month varchar(2)
declare #year varchar(4)
declare #fulldate smalldatetime
set #day = '1'
set #month = '9'
set #year = '2012'
SELECT #fulldate = Convert(smalldatetime, #month + '/'
+ #day + '/' + #year, 101)
select Convert(varchar(10), #fulldate, 101)
I have the following integer type values in a SQL script: #year, #month, #day. Now I want to convert those into a datetime value. Should be easy, right?
Well, I just went through the SQL documentation and was very surprised that I couldn't find any way to do this, other than converting to a string and then to a datetime.
declare #dt datetime
set #dt= convert(varchar,#year)+'/'+convert(varchar,#month)+'/'+convert(varchar,#day)
This is horrible! Surely there has to be a way to convert straight from the int values to the datetime?
Not out of the box, but you could create a UDF that does it, for example:
create function ints2date (#year int, #month int, #day int)
returns datetime
as begin
declare #foo varchar (10)
set #foo = convert (varchar, #year) + '-' +
convert (varchar, #month) + '-' +
convert (varchar, #day)
return convert (datetime, #foo)
end
go
select dbo.ints2date (2000,1,1)
You can also do it in a more convoluted (but probably slightly faster) way using dateadd/datepart. An example of this can be found at Create a date with T-SQL (stackoverflow.com).
You can do it without converting to string like this. It's not a single function, which would be nice, but it works:
DECLARE
#year SMALLINT,
#month TINYINT,
#day TINYINT,
#d DATETIME
SET #year = 2010
SET #month = 6
SET #day = 23
SET #d = '1900-01-01'
SELECT
DATEADD(dy, #day - 1, DATEADD(mm, #month - 1, DATEADD(yy, #year - 1900, #d)))
Another shortcut method
DECLARE
#year SMALLINT,
#month TINYINT,
#day TINYINT,
#d DATETIME
SET #year = 2010
SET #month = 6
SET #day = 23
SELECT cast(cast(#year*10000+#month*100+#day as char(8)) as datetime)
SELECT cast(ltrim(#year*10000+#month*100+#day) as datetime)