Question on Converting ints to datetimes in TSQL - sql-server

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

Related

Creating a datetime with year and month as inputs

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)

Why is there a space when concatenating varchars in this SQL 2008 query?

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 ;

Convert to Datetime MM/dd/yyyy HH:mm:ss in Sql Server

How to convert given date format to MM/dd/yyyy HH:mm:ss
I tried this one below but not achieved. Can anyone help me?
SELECT CONVERT(VARCHAR(20), GETDATE(), 120)
Supported by SQL Server 2005 and later versions
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
+ ' ' + CONVERT(VARCHAR(8), GETDATE(), 108)
* See Microsoft's documentation to understand what the 101 and 108 style codes above mean.
Supported by SQL Server 2012 and later versions
SELECT FORMAT(GETDATE() , 'MM/dd/yyyy HH:mm:ss')
Result
Both of the above methods will return:
10/16/2013 17:00:20
Try below:
SELECT CONVERT(VARCHAR(20), GETDATE(), 101)
use
select convert(varchar(10),GETDATE(), 103) +
' '+
right(convert(varchar(32),GETDATE(),108),8) AS Date_Time
It will Produce:
Date_Time 30/03/2015 11:51:40
Declare #month as char(2)
Declare #date as char(2)
Declare #year as char(4)
declare #time as char(8)
declare #customdate as varchar(20)
set #month = MONTH(GetDate());
set #date = Day(GetDate());
set #year = year(GetDate());
set #customdate= #month+'/'+#date+'/'+#year+' '+ CONVERT(varchar(8), GETDATE(),108);
print(#customdate)

SQL server stored procedure between statement

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)

combine date and time column problem

Using SQL Server 2005
Date Time
20060701 090000
20060702 020000
20060703 180000
...
Date and Time datatype is varchar
Tried Query
select Convert(datetime, Convert(char(10), date, 103) + ' ' + Convert(char(8), time, 108), 103) from table
SELECT
CAST(
DATEADD(dd, 0, DATEDIFF(dd, 0, date)) + ' ' +
DATEADD(Day, -DATEDIFF(Day, 0, time), time)
as datetime) from table
It showing error as out of range value.
How to solve this issue.
Need Sql Query Help
First off, why are you storing a DATETIME in a VARCHAR?
This should be able to help
DECLARE #Table TABLE(
Val VARCHAR(20)
)
INSERT INTO #Table (Val) SELECT '20060701 090102'
INSERT INTO #Table (Val) SELECT '20060702 020000'
INSERT INTO #Table (Val) SELECT '20060703 180000'
SELECT *,
CAST(SUBSTRING(Val,1,8) + ' ' + SUBSTRING(Val,10,2) + ':' + SUBSTRING(Val,12,2) + ':' + SUBSTRING(Val,14,2) AS DATETIME)
FROM #Table
I came across a similar issue a few years ago, when importing HL7 messages. Here is a copy of the function I used. It creates a DateTime string with the time component correctly separated into hh:mm:ss, which is needed for the cast to DateTime.
CREATE FUNCTION fn_StringDateTietoDateTime
(
#Date varchar(15)
)
RETURNS datetime
AS
BEGIN
DECLARE #Result DATETIME
SET #Result = NULL
If len(#Date) > 0
BEGIN
SELECT #Result = CAST(SUBSTRING(#hl7date, 1, 8) + ' ' + SUBSTRING(#hl7date, 10, 2) + ':' +
SUBSTRING(#date, 12, 2) + ':' + SUBSTRING(#date,14, 2) AS DATETIME)
END
RETURN #RESULT
END

Resources