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)
Related
I want to create a datetime with a value like:
2019-09-01 00:00:00.000
I have the values for year and month in variables #year and #month.
What is the best way to create a datetime with inputs?
I would use DATEFROMPARTS.
declare #year int = 2019
, #month int = 9
SELECT convert(datetime, DATEFROMPARTS(#year, #month, 1))
I'm include a #Day part too, as you may find it useful. If you do not want it, replace RIGHT('00' + LTRIM(#Day),2) with '01'
DECLARE #Year VARCHAR(4) = '2019';
DECLARE #Month VARCHAR(2) = '09'
DECLARE #Day VARCHAR(2) = '01'
SELECT CAST(CAST(#Year as char(4))
+ RIGHT('00' + LTRIM(#Month),2)
+ RIGHT('00' + LTRIM(#Day),2)
AS Datetime)
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
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 am using sql server 2005 and when I call my stored proc i am getting this error:
Error converting data type nvarchar to datetime.
So how can I pass in my datetime to run my stored proc?
create procedure [dbo].[myProc]
#id as int,
#date as datetime
as
begin
select id , DATEPART(day, #date)
from myTable
where convert(varchar,[date],101) = convert(varchar,#date,101)
and id =#id
end
DECLARE #return_value int
EXEC #return_value = [dbo].[myProc]
#id = 1,
#date = N'getdate()'
SELECT 'Return Value' = #return_value
GO
You should be using an open-ended date range instead of converting both sides of the equation to a regional string.
ALTER PROCEDURE [dbo].[myProc]
#id INT,
#date DATETIME
AS
BEGIN
SET NOCOUNT ON;
SELECT id, DATEPART(DAY, #date)
FROM dbo.myTable
WHERE id = #id
AND [date] >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, #date))
AND [date] < DATEADD(DAY, 1, DATEDIFF(DAY, 0, #date));
END
GO
Now, you can't just pass getdate() into a stored procedure call like that (the syntax is invalid), and you certainly can't pass it as a string. So a couple of options.
(a) Declare a variable up front, assign the current date/time to the variable, and pass the variable in:
DECLARE #d DATETIME;
SET #d = GETDATE();
EXEC [dbo].[myProc] #id = 1, #date = #d;
(b) Make the parameter optional, and then don't bother passing that param in:
ALTER PROCEDURE [dbo].[myProc]
#id INT,
#date DATETIME = NULL
AS
BEGIN
SET NOCOUNT ON;
SET #date = DATEADD(DAY, 0, DATEDIFF(DAY, 0,
COALESCE(#date, GETDATE())));
SELECT id, DATEPART(DAY, #date)
FROM dbo.myTable
WHERE id = #id
AND [date] >= #date
AND [date] < DATEADD(DAY, 1, #date);
END
GO
Now your call can just be:
EXEC dbo.myProc #id = 1;
Also, you shouldn't use 'single quotes' for column aliases - this syntax is deprecated. Use [square brackets] instead.
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)