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)
Related
I was tasked with setting up a control number based on month and date that are passed into a stored procedure. I pass a month and a year but I need to pad the month so that 1-9 show up as 01-09 as the number of characters must stay the same for import. My result should yield Z_0+month+year as a 9 character field, such as Z_0201901 for January 2019.
I used this post How can I get the month number (not month name) from a date in SQL Server? to pad the 0's in month. Specifically SELECT RIGHT('00' + RTRIM( CAST( DATEPART( MONTH, GETDATE() ) AS varchar(2)) ) , 2).
My issues is that I somehow get a space inserted when I concatenate a month and a year even though each variable individually does NOT include a space.
Consider:
DECLARE #Mon char(3),
#Year char(6)
SET #Mon = '5'
SET #Year = '2019'
SET #Mon = RIGHT('00' + RTRIM(CAST(#Mon as varchar(2))), 2)
SET #Year = RIGHT('00' + RTRIM(CAST(#Year AS varchar(4))), 5)
SELECT #Year
SELECT #Mon
SELECT #Year+#Mon
SELECT 'Z_' + #Year + #Mon ;
Expected result is that I would get Z_0201905 but I get Z_02019 05.
Initially I thought it is because I declared #Mon as char(3) but even changing that to char(2) has the same behavior.
Changing #Year to char(5) and adding a 0 to SELECT 'Z_0' + #Year + #Mon; yields the same result.
Setting #Mon variable to something higher than 9, such as 12, still yields the same result of having a space.
Running SELECT ##VERSION yields: Microsoft SQL Server 2008 (SP3) - 10.0.5538.0 (X64) Apr 3 2015 14:50:02 Copyright (c) 1988-2008 Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (VM)
I tried using CONCAT but through Stack Overflow found that the function isn't available until SQL Server 2012.
What is causing the space? It doesn't make any sense to me.
You've declare #Year as char(6). Thus it will always have 6 characters. Any characters not used when setting the string will be padded with white spaces. Thus your #Year string becomes '02019 ' when you set it to '02019'. Declare it as char(5) or a varchar. Then the value would be '02019' so that when the strings are concatenated you will end up with your desired 'Z_0201905'.
Short answer
DECLARE #Mon char(2),
#Year char(5)
I've updated your script and changed the output so you can see what is going on.
DECLARE #Mon char(3),
#Year char(6)
SET #Mon = '5'
SET #Year = '2019'
SET #Mon = RIGHT('00' + RTRIM(CAST(#Mon as varchar(2))), 2)
SET #Year = RIGHT('00' + RTRIM(CAST(#Year AS varchar(4))), 5)
--SELECT #Year
--SELECT #Mon
--SELECT #Year+#Mon
SELECT 'Z_' + #Year + #Mon ;
SELECT 'Z_' + '-->' + #Year + '<-- -->' + #Mon + '<--' as [has extra spaces] ;
go
DECLARE #Mon char(2),
#Year char(5)
SET #Mon = '5'
SET #Year = '2019'
SET #Mon = RIGHT('00' + RTRIM(CAST(#Mon as varchar(2))), 2)
SET #Year = RIGHT('00' + RTRIM(CAST(#Year AS varchar(4))), 5)
--SELECT #Year
--SELECT #Mon
--SELECT #Year+#Mon
SELECT 'Z_' + #Year + #Mon ;
SELECT 'Z_' + '-->' + #Year + '<-- -->' + #Mon + '<--' as [no extra spaces] ;
DECLARE #Mon varchar(3),
#Year varchar(5)
SET #Mon = '5'
SET #Year = '2019'
SET #Mon = RIGHT('00' + RTRIM(CAST(#Mon as varchar(2))), 2)
SET #Year = RIGHT('00' + RTRIM(CAST(#Year AS varchar(4))), 5)
SELECT #Year
SELECT #Mon
SELECT #Year+#Mon
SELECT 'Z_' + #Year + #Mon ;
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)
Error in SQL. Can not find it.
DECLARE #year VARCHAR (4),
#month VARCHAR (2),
#day VARCHAR (2),
#weekday VARCHAR (2),
#hour VARCHAR (2),
#archivePath VARCHAR (128),
#archiveName VARCHAR (128),
#archiveFullName VARCHAR (128)
SET #year = CAST(DATEPART(yyyy, GETDATE()) AS VARCHAR)
SET #month = CAST(DATEPART(mm, GETDATE()) AS VARCHAR)
SET #day = CAST(DATEPART(dd, GETDATE()) AS VARCHAR)
SET #weekday = CAST(DATEPART (dw, GETDATE()) AS VARCHAR)
SET #hour = CAST(DATEPART (hh, GETDATE()) AS VARCHAR)
SET #archivePath = 'd:\1c_new\backupdb\'
SET #archiveName = 'TransactionLog_' + #year + '_' + #month + '_' + #day + '_' + #hour + '.bak'
SET #archiveFullName = #archivePath + #archiveName
BACKUP LOG [xxx] TO DISK = #archiveFullName WITH INIT , NOUNLOAD , NAME = N'Ежечастный лог транкзаций', SKIP , STATS = 10, DESCRIPTION = N'Ежечастный лог транкзаций', NOFORMAT
Just a hunch, try changing N'Ежечастный лог транкзаций' to something like 'NORMAL STRING'
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)
I have a stored procedure that takes an int for the desired month and year as parameters. It uses these to compare some datetime values from the tables I'm pulling from. I need to conver them into DateTime values. I'm trying to do something like this:
CONVERT(DATETIME, CONVERT(varchar(4), #year) + '-' + Convert(varchar(2),#month) + '-01 00:00:00', 102))
which builds the "2009-6-1 00:00:00" string (for those parameter values). This doesn't seem to work though, any other suggestions on how to do this? I think I'm going about this too much like a programmer...
This is Sql Server 2008.
SELECT CAST(CAST(#Year AS VARCHAR(4)) + RIGHT('0' + CAST(#Month AS VARCHAR(2)), 2) + '01' AS DATETIME)
Should do it
What about:
DECLARE #year int;
DECLARE #Month int;
SET #year = 2010;
SET #Month = 1
SELECT CAST(LTRIM(#year * 10000 + #Month * 100 + 1) AS smalldatetime)
Use DATEADD.
DATEADD(month, #month - 1, DATEADD(year, #year - 1900, 0))
Filter dates using the YEAR and MONTH functions.
SELECT *
FROM Sales
WHERE YEAR(Sales.PurchaseDate) = #year
AND MONTH(Sales.PurchaseDate) = #month