Convert to Datetime MM/dd/yyyy HH:mm:ss in Sql Server - 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)

Related

Convert nvarchar to datetime 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())

converting date time format in sql

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)

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)

Homework - Getting DateTime in custom format in MS SQL

How do I select a value from a DateTime column in the DD-MM-YYYY HH:MI:SS (HH is 24-hour) format?
P.S.: The return vale of the SELECT query can be any datatype.
Seeing as this is homework, just some tips:
You need to read up on the Convert Sql function. Then, you need to realise that your requirement is a mix of two different formats (one for the date; hint - Italian) and one for the time part. Simply Convert your date into the two formats and concatentate them with a space in between
Your code will end up looking like the following:
select
CONVERT(VARCHAR(10), your_date, date_format ) + ' ' + CONVERT(VARCHAR(10), your_date,time_format )
from ...
Here is how to do it
DECLARE #dt DATETIME
SET #dt = GETDATE()
PRINT CONVERT(varchar, #dt, 103) + ' ' + CONVERT(varchar, #dt, 108)
In a select statement it would look like:
SELECT CONVERT(varchar, yourDateField, 103) + ' ' +
CONVERT(varchar, yourDateField, 108) as YourFieldName, ...
FROM ...
WHERE ...

Converting DateTime to nvarchar, just month and year

How to convert the datetime value to nvarchar and want to format it "Month, Year" e-g October 1st 2009 value should get converted to "October, 2009"
use this:
select CONVERT(nvarchar(50), DATENAME(m, getdate())
+ ', '
+ DATENAME(yyyy, getdate())
)
OUTPUT:
--------------------------------------------------
October, 2009
(1 row(s) affected)
Try this
DECLARE #DateTime DATETIME
SET #DateTime = '01 Oct 2009'
SELECT #DateTime
SELECT DATENAME(mm, #DateTime) + ', ' + CAST(DATEPART(yy, #DateTime) AS VARCHAR(4))
One way would be to use datename to extract the pieces you needed in name format, so:
select Convert(nvarchar,datename(m,getdate())) + N', ' + Convert(nvarchar,datename
(yy,getdate()))
And replace getdate() with your date variable / field.
The DateName function will provide the formatting you require:
DATENAME(m, date) + ', ' + DATENAME(yyyy, date)
Converting to nvarchar of a specific size can be done through the cast function:
CAST(value AS nvarchar[30])
Try the following query:
Select case Convert(int, day(getdate())) when 1 then '1st' when 2 then '2nd'
when 3 then '3rd' else Convert(varchar, day(getdate()))+'th' end +' '+ Convert(varchar, Datename(m,getdate()))+' ' +Convert(varchar, Datename(yy,getdate())) as Date
You can replace getdate() with any other date.
Please check if it helps.

Resources