Convert nvarchar to datetime sql server - sql-server

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())

Related

Using DATEDIFF to assign dynamic interval

I have the following code, which works as expected.
DECLARE #startdate datetime2 = '2007-05-05';
DECLARE #enddate datetime2 = '2007-05-04';
SELECT DATEDIFF(year, #startdate, #enddate);
I now want to be able to use a variable to switch from "year, week, days, etc. So I tried this, but it didn't work. Any suggestions?
DECLARE #startdate datetime2 = '2007-05-05';
DECLARE #enddate datetime2 = '2007-05-04';
DECLARE #interval varchar = 'year'
SELECT DATEDIFF(#interval, #startdate, #enddate);
I get:
Msg 1023, Level 15, State 1, Line 25
Invalid parameter 1 specified for datediff.
Thank you,
Raul Gonzalez
You have to use dynamic sql for this. Once you start using dynamic sql you open yourself up to sql injection. Here is how you can do this and protect yourself against sql injection. The case expression may seem a little odd but if you have a value in your #interval that is not allowed the entire sql string will be NULL and no harm will come to your database. Also, please notice that you did not specify a length for #interval. For variables the default length is 1 so it would only be 'y' not 'year' as you expected it to be. Always specify the scale and precision of variables.
DECLARE #startdate datetime2 = '2007-05-05';
DECLARE #enddate datetime2 = '2007-05-04';
DECLARE #interval varchar(10) = 'year'
declare #SQL nvarchar(max)
set #SQL = 'select datediff(' +
case #interval when 'year' then 'year'
when 'week' then 'week'
when 'days' then 'day'
end
+ ', #startdate, #enddate)'
exec sp_executesql #SQL, N'#startdate datetime2, #enddate datetime2', #startdate, #enddate
You could do it dynamically:
declare #datediff varchar(max)
set #datediff = 'SELECT DATEDIFF(' + #interval + ',' + '''' + cast(#startdate as varchar(10)) + '''' + ',' + '''' + cast(#enddate as varchar(10)) + '''' + ')'
exec (#datediff)
Now let's say that I have the following:
DECLARE #startdate datetime2 = '2017-11-03';
DECLARE #enddate datetime2 = '2018-11-03';
DECLARE #interval varchar(10) = 'month'
declare #SQL nvarchar(max)
set #SQL = 'select datediff(' +
case #interval
when 'year' then 'year'
when 'month' then 'month'
when 'week' then 'week'
when 'day' then 'day'
end
+ ', #startdate, #enddate)'
exec sp_executesql #SQL, N'#startdate datetime2, #enddate datetime2',
#startdate, #enddate
The result is "12"
Now I'd like to have 12 values displayed as:
2017/11/03
2017/12/03
2018/01/03
2018/02/03
2018/03/03
2018/04/03
2018/05/03
2018/06/03
2018/07/03
2018/08/03
2018/09/03
2018/10/03

datetime format in sql server 2008

I have used below code to convert a datetime to string,
DECLARE #StartDate datetime = '08/07/2015 12:10 AM'
set #StartDate = dateadd(hour,12, #StartDate);
select CONVERT(VARCHAR(10),#StartDate, 101) + RIGHT(STUFF(CONVERT(VARCHAR(32), #StartDate,100), 18, 0, ' '),8)
but I am getting output as "08/07/201512:10 PM" , there is no space between date and time, How can I correct this?
If i correctly understood your problem then there is small correction required in your code. I added +' '+ i.e. a blank space between your date convert and right stuff. Complete code is as given below.
DECLARE #StartDate DATETIME = '08/07/2015 12:10 AM'
SET #StartDate = DATEADD(HOUR,12, #StartDate);
SELECT CONVERT(VARCHAR(10),#StartDate, 101) +' '+
RIGHT(STUFF(CONVERT(VARCHAR(32),#StartDate,100), 18, 0, ' '),8)
Result
08/07/2015 12:10 PM
i.e. space between date and time also space between 12:10 and PM
To cover new case provided :
DECLARE #StartDate DATETIME = '08/07/2015 2:10 AM'
SET #StartDate = DATEADD(HOUR,12, #StartDate);
SELECT CONVERT(VARCHAR(10),#StartDate, 101) +' '+
LTRIM(RIGHT(STUFF(CONVERT(VARCHAR(32),#StartDate,100), 18, 0, ' '),8))
Result
08/07/2015 2:10 PM
i.e. no extra space when time is like 2:10 PM
Here is one way to do it:
DECLARE #StartDate datetime = '2015-08-07T00:10:00';
SET #StartDate = dateadd(hour,12, #StartDate);
SELECT #StartDate As StartDate,
CONVERT(CHAR(10), #StartDate, 101) + ' ' + -- DateString,
SUBSTRING(CONVERT(CHAR(19), #StartDate, 100), 13, 5) + ' ' + -- TimeString
RIGHT(CONVERT(CHAR(19), #StartDate, 100), 2) As DateString -- AM/PM
Result:
StartDate DateString
----------------------- -------------------
2015-08-07 12:10:00.000 08/07/2015 12:10 PM
The following snippet will produce the output you've indicated in the question.
SET DATEFORMAT MDY;
DECLARE #StartDate DATETIME = '08-07-2015 12:10 AM';
SET #StartDate = DATEADD(HOUR, 12, #StartDate);
SELECT CONVERT(VARCHAR, #StartDate, 103) + ' ' +
CONVERT(VARCHAR, CAST(#StartDate AS TIME), 108) +
CASE WHEN DATEPART(HOUR, #StartDate) < 12 THEN ' AM' ELSE ' PM' END;
N.B. As others have pointed out, you would be better off using ISO format for input dates.
Updated dateformat from DMY to MDY and explicitly adding AM/PM to the end.

Comparing DateTIme Column value with DateTime String SQL Server

I am trying to compare DateTime string to a DateTime Value Column in DB but its returning me 0 records Here is the query
DECLARE #p_date DATETIME
SET #p_date= CONVERT( DATETIME, '9/1/2015 10:06:22 PM', 131 )
SELECT UpdateUserId, UpdateTimeA
From SubmitSheets
WHERE CONVERT( DATETIME, UpdateTimeA, 131 ) = #p_date
the value in UpdateTimeA column is 2015-09-01 22:06:22.447
Like I said in my comment, I'm not sure 131 is the right style to convert to datetime (at least, I get a very different value).
If you want to compare dates without the second fraction, you'll have to compare as strings. Converting both dates to a varchar(19) will cut off the fractions.
Finally found a way
SELECT UpdateUserId,UpdateTimeA
From SubmitSheets
WHERE CONVERT(VARCHAR(16),UpdateTimeA,100) = CONVERT(VARCHAR(16),CONVERT( DATETIME, '9/1/2015 10:06:22 PM',101),100)
I think the #p_date parameter is not in same format. Try this:
DECLARE #p_date DATETIME
SET #p_date= CONVERT( DATETIME, '9/1/2015 10:06:22 PM')
SELECT UpdateUserId, UpdateTimeA
From SubmitSheets
WHERE CONVERT(DATETIME, (CONVERT(VARCHAR, UpdateTimeA, 101) + ' ' + CONVERT(VARCHAR, UpdateTimeA, 108)), 131) = CONVERT(DATETIME, #p_date, 131)
So another tryout may be
An example:
DECLARE #a TABLE
(
ID int,
datet datetime
)
INSERT INTO #a
(ID, datet)
VALUES (1 , CONVERT(DATETIME, GETDATE(), 131))
,(2 , CONVERT(DATETIME, GETDATE(), 131))
,(3 , CONVERT(DATETIME, GETDATE(), 131))
,(4 , CONVERT(DATETIME, GETDATE(), 131))
,(5 , CONVERT(DATETIME, GETDATE(), 131))
,(6 , CONVERT(DATETIME, GETDATE(), 131))
,(7 , CONVERT(DATETIME, GETDATE(), 131))
,(8 , CONVERT(DATETIME, GETDATE(), 131))
,(9 , CONVERT(DATETIME, GETDATE(), 131))
SELECT ID, datet, ConvDatetime
FROM
(
SELECT ID, datet
,CONVERT(DATETIME, (CONVERT(VARCHAR, datet, 101)
+ ' '
+ CONVERT(VARCHAR, datet, 108)), 131) AS ConvDatetime
FROM
#a
) AS dt
WHERE
dt.ConvDatetime= CONVERT(DATETIME,
(CONVERT(VARCHAR, GETDATE(), 101)
+ ' '
+ CONVERT(VARCHAR, GETDATE(), 108))
, 131) -- CONVERT( DATETIME, '9/1/2015 10:06:22 PM', 131 )

Convert int MMDDYYYY into datetime MM-DD-YYYY

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

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)

Resources