I would like to
convert int MMDDYYYY (e.g.-5112012) into datetime MM-DD-YYYY
and then convert datetime MM-DD-YYYY into YYYY-MM-DD
or if the above can be done in one step converting int MMDDYYYY into datetime YYYY-MM-DD?
For reference, earlier I converted int YYYYMMDD into datetime YYYY-MM-DD using the following syntax:
declare #date int;
set #date = 19900511
select CONVERT(datetime, convert(varchar(8),#date), 103)
DECLARE #date INT;
SET #date = 5112012
SELECT CONVERT(DATETIME, LEFT('0' + CONVERT(VARCHAR(8), #date), 2) + '-' + SUBSTRING( '0' + CONVERT(VARCHAR(8), #date), 3, 2) + '-' + RIGHT(#date,4))
That bails the water, now fix the leak; change the storage from int to a proper date or datetime :)
declare #date int
set #date = 19900511
select CONVERT(CHAR(10), CONVERT(datetime, convert(varchar(8),#date), 103), 21)
SQL Fiddle
The result is 1990-05-11
convert( datetime,
case when #MMDDYYYY = 0 then
null
else
substring(convert( varchar(9), 800000000 + #MMDDYYYY ) , 2, 2)
+ '/' + substring(
convert( varchar(9), 800000000 + #MMDDYYYY ) , 4, 2)
+ '/' + right(#MMDDYYYY,4) end ) as New_Date
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 try to convert 2016/07/15 (nvarchar format) to this datetime format 2016-07-15 00:00:00 in sql server. But i need the time part to be the current time. can any one help ?
This is my sp:
declare #DateTime varchar(50)
set #DateTime = '2016/07/15'
select convert(varchar, cast(#DateTime as datetime), 120)
You can use this:
declare #DateTime varchar(50)
set #DateTime = CONCAT('2016/07/15' , ' ', CONVERT(TIME, GETDATE()))
select #DateTime
select convert(varchar, cast(#DateTime as datetime2(7)), 120)
Or replace the CONCAT with:
set #DateTime = '2016/07/15' + ' ' + CONVERT(NVARCHAR(50), CONVERT(TIME, GETDATE()))
Try this...
Just concat time with your output
declare #DateTime varchar(50)
set #DateTime = '2016/07/15'
select convert(varchar, cast(#DateTime as datetime), 101) + ' ' + CONVERT(varchar, getdate(),108)
Try like this,
DECLARE #DateTime VARCHAR(50)
SET #DateTime = '2016/07/15'
SELECT DATEADD(day, 0, DATEDIFF(day, 0, #DateTime)) + DATEADD(day, 0 - DATEDIFF(day, 0, getdate()), getdate())
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)
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
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