I would like to convert a date time format to hh:mm using SQL Server.
For example: convert this value 2017-08-17 15:31:18.217 into 15:31.
If anyone knows how to do this, please help.
Here is one way to do it:
DECLARE #DateTime datetime = '2017-08-17 15:31:18.217'
SELECT CONVERT(char(5), #DateTime, 108)
Result: 15:31
SELECT FORMAT(cast('2017-08-17 15:31:18.217' as datetime),'hh:mm')
SELECT CONVERT(VARCHAR(5), GETDATE(), 108)
You may want to use TRY_CONVERT if supported,and you don't always have a in that column.
One thing, the value that gets converted needs to be of type DATETIME.
Please execute these 2 queries , you will get the hours and minutes of current time.
SELECT GETDATE()
SELECT CONVERT(VARCHAR(5), CAST(GETDATE() AS TIME))
I use SQL Server and I need to display a datetime data type in the following format:
dd/mm
day-month without the year, which is the most effective way?
Use 103 style in convert function and remove the year
SELECT LEFT(CONVERT(VARCHAR(15), Getdate(), 103), 5) --11/03
You'll find this site really helpful I think:
http://www.sql-server-helper.com/tips/date-formats.aspx
From that link, you can see this as a quick way to get DD/MM:
SELECT CONVERT(VARCHAR(5), GETDATE(), 3) AS [DD/MM]
I am using SQL Server 2008 and have the column MyDate that contains the date as 20141208. Its type is user-defined (UTypeDate:varchar(8)), and I want to convert it to 08/12/2014.
For example, given
MyDate (UTypeDate:varchar(8))
20141208
20141218
20141204
20141216
what I want is to write the query that will give me output as:
MyNewDate
08/12/2014
18/12/2014
04/12/2014
16/12/2014
How should I write the query for this?
Try this
SELECT CONVERT(VARCHAR(10), CAST('20141208 ' AS DATE), 103)
RESULT
I think you need an extra column on your desired format. Then use the following query.
SELECT VARCHARDATE,
CONVERT(VARCHAR(10), CAST(VARCHARDATE AS DATE), 103) NEWDATE
FROM YourTable
which forms the below result
You can apply your logic con NEWDATE columns as per your requirements.
Let me know for any changes
Check the sqlfIDDLE http://sqlfiddle.com/#!3/af366/1 (Click RUNSQL button when site not loaded properly)
try this
SELECT CONVERT(VARCHAR(10), CAST(MyDate AS DATE), 103) FROM TableName
For visual prrof
Table Design
Data
Result
My sample query is
SELECT D30.SPGD30_LAST_TOUCH_Y
from CSPGD30_TRACKING D30
My given date format is like "2013-01-01 00:00:00.000". I need to convert this date format to "mm/dd/yyyy hh:mm AM/PM". Do you have any idea about that?
I think there is no single format to give them both. Try this using Convert; Sql-Demo
declare #mydate datetime = getdate()
select convert(varchar(10),#mydate, 101) + right(convert(varchar(32),#mydate,100),8)
| COLUMN_0 |
----------------------
| 02/22/2013 9:36AM |
The FORMAT() function is available from version 2012 onwards.
Once upgraded, you can use
select FORMAT(#date,'MM/dd/yyyy hh:mm:s tt')
Use this
select CONVERT(VARCHAR(10), mydate, 101) + ' ' + RIGHT(CONVERT(VARCHAR, mydate, 100), 7) from tablename
Try this
SELECT convert(varchar(20), GetDate(), 0);
To extract only AM/PM
substring(convert(varchar(30), GetDate(), 9), 25, 2);
Fiddle
Use the following scenario for getting date,time,day,month,year,hours,minutes,seconds,AM/PM
:)
SELECT UpdatedOn ,
CONVERT(varchar,UpdatedOn,100) DateTime,
CONVERT(varchar,UpdatedOn,10) Date ,
CONVERT(varchar,UpdatedOn,108) Time ,
substring(CONVERT(varchar,UpdatedOn,106),1,2) Day,
substring(CONVERT(varchar,UpdatedOn,106),4,3) CMonth,
substring(CONVERT(varchar,UpdatedOn,105),4,2) NMonth,
substring(CONVERT(varchar,UpdatedOn,106),8,4) Year,
left(right(CONVERT(varchar,UpdatedOn,100),7),2) Hours_12,
substring(CONVERT(varchar,UpdatedOn,108),1,2) Hours_24,
substring(CONVERT(varchar,UpdatedOn,108),4,2) Minutes,
substring(CONVERT(varchar,UpdatedOn,108),7,2) Second,
right(CONVERT(varchar,UpdatedOn,100),2) AM_PM
FROM dbo.DeviceAssignSim
WHERE AssignSimId=55;
You can do it like this:
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]
For more information look this:
date-format
Use the convert method to format a datetime value. Example:
select convert(varchar(20), D30.SPGD30_LAST_TOUCH_Y, 101)
The third parameter determines the format. You can find the available formats in the cast and convert documentation.
Use this
Select (Convert(Varchar,GetDate(),101))+' '+(Right(('0'+(LTrim((Left((Right((Convert(Varchar,GetDate(),100)),7)),5))))),5))+' '+(Right((Convert(Varchar,GetDate(),100)),2))
I realize this is a 8 year old question but it was answered in many ways and none are simple enough. This is what I found to be simple and matches just about what the user is asking for (year is two digits, and seconds are present), assuming that the date he is getting is from GETDATE(), not that it matters but that is where my answer comes from.
SELECT CONVERT(varchar, D30.SPGD30_LAST_TOUCH_Y, 22) AS [DateTime]
12/16/20 10:19:18 AM
I am trying to save only date (ex: 2011/09/26 00:00:00) from getdate(). I am using sql server 2005.
My sql query is going to be like this:
Insert into Merchant(startdate)
values **today's date only**
How is it possible?
INSERT INTO Merchant
(startdate)
VALUES (DATEADD(DAY,0,DATEDIFF(DAY,0,GETDATE())))
select dateadd(dd,0, datediff(dd,0, GETDATE()))
However, if you move to SQL2008, I recommend changing your column to DATE instead of DATETIME.
I'm a little late to the party, but here are my 2 cents:
INSERT INTO Merchant
(startdate)
VALUES (cast(GETDATE() as Date))
That, of course, will only work from SQL Server 2008. I'll leave this answer, however, for learning purposes and future references.
You can try something like this:
EDIT - Revised to account for rounding after midday.
SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDATE())))
Here's another apporoach you can try:
SELECT CAST(CONVERT(VARCHAR, GETDATE(), 101) AS DATETIME)