How to convert date and time in SQL Server - sql

I have the following columns in a table:
Signed_In_Date Signed_Out_Time
11/1/2005 12:00:00 am 11/1/2005 10:27:00PM
I would like to convert them to the following output:
Signed_In_Date Signed_Out_Time
11/1/2005 10:27:00PM
Is there a function or conversion code in SQL Server that would do it?

Assuming that the columns you're referring to are DATETIME columns, I would use the code below:
Date Only
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
Time Only
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
You can see the queries in action / play with them here.

For Sign_In_Date Use
select CONVERT(VARCHAR(10),'11/1/2005 10:27:00PM',108)
Ouput:
11/1/2005
For Sing_Out_Time
declare #time time
set #time=cast('11/1/2005 10:27:00PM' as Time)
select convert(varchar(10),#time,100)
Output:
10:27PM

try that :
select CONVERT(VARCHAR(10),Signed_Out_Time,108) ,-- 108 is d/M/yyyy if you want mm/dd/yyy you should use 101
CONVERT(VARCHAR(8),Signed_In_Date,103)

You can use CONVERT to change datetime format to your own desirable format:
SELECT CONVERT(VARCHAR(10),Signed_In_Date,101) as 'Signed_In_Date',
CONVERT(VARCHAR(10),Signed_Out_Time,108) as 'Signed_Out_Time';
For more date format, go over this link:
http://www.sql-server-helper.com/tips/date-formats.aspx

Related

Remove the zeros using SQL Server

Hello I am using SQL Server and I have this column :
data
6/19/2019 3:10:12 PM
12/23/2016 5:02:15 AM
I wanted to extract only the time so I used the following query :
select try_parse([data] as time using 'en-US') from Table_1
But the problem is I get as a result this :
15:10:12.0000000
05:02:15.0000000
All is okay except the 0 I mean I would like to get :
15:10:12
05:02:15
Thank you very much for your help !
You can use TIME() with the correct scale to get your desired results.
select Convert(time(0) , Data ) as time from Table_1
time(0) means a scale of 0, which has a precision of 8. This results as HH:MM:SS. Using another scale, like 1, would give you a single digit for the fractional second. You can read up on all of the scales in the MS Docs
Declare #data varchar(28)='12/23/2016 5:02:15 AM'
select Convert(varchar(20),Cast(#data as time),24)
Try this
select CONVERT(varchar(8), data, 108) from Table_1
that only show time (17:00:59) but you can add + between them if you want full format date as dd/MM/yyyy hh:mm:ss
select CONVERT(varchar(10), data, 101) + ' ' + CONVERT(varchar(8), data, 108) from Table_1
They will work if data using DATETIME type
You can extract time using below query
SELECT CONVERT(VARCHAR(8), cast('12/23/2016 5:02:15 AM' as time),108)
The output is- 05:02:15. I can hope this query/answer is self explanatory.

SQL Server: how to change data entry from VARCHAR to DATETIME?

I have below sample data:
03202012 as date but the column datatype is Varchar.
I want to convert it to 2012-03-20 00:00:00.000 as Datetime.
I tried using
CAST(CONVERT(CHAR(10), Column, 101) AS DATETIME)
But I get an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Complete code snippet to test:
DECLARE #Column VARCHAR(MAX) = '03202012'
SELECT CAST(CONVERT(CHAR(10), #Column, 101) AS DATETIME)
Use yyyyMMdd format, that always works:
DECLARE #myDateString varchar(10) = '03202012';
SELECT cast( substring(#myDateString, 5, 4)+
substring(#myDateString, 1, 2)+
substring(#myDateString, 3, 2) AS datetime);
I found below script help me solved my concern.
SELECT convert(datetime, STUFF(STUFF('31012016',3,0,'-'),6,0,'-'), 105)
Result: 2016-01-31 00:00:00.000
Thanks all for the effort. :D
In MySQL, you can use the STR_TO_DATE function to convert a string to a date. For your example, it would look like this
STR_TO_DATE("03-02-2012", "%m-%d-%Y");
Note that the format part of the string must match the format part of the date.
Edit: Just found out this is for SQL Server, but I assume this will work there as well.

How to filter Time portion of a DateTime column

I want to retrive data from table according to a particular time so I use this query below.
select * from a
where convert(smalldatetime,date,datepart(hh,date)) ='09:12:00'
but I get no row as result. So what should I do for my expected result?
You can convert directly to varchar as below
SELECT * FROM a
WHERE CONVERT(VARCHAR(8), [date], 108) = '09:12:00'
Try this:
SELECT *
FROM a
WHERE CONVERT(VARCHAR(8), [DATE],108) = '09:12:00'
Your datatype is already smalldatetime so you have to convert to varchar and compare with string.
EDIT:
Answer explanation:
You have smalldatetime inside [DATE] column. For example 2015-10-01 09:12:00. To compare only time you need to convert to string which contains only time. For this reason you will use command CONVERT from TSQL.
It will convert smalldatetime to 8 characters string. As a result you will have 09:12:00 and then you compare it with your string.
In Sql server 2008 you can convert to TIME. By specifying TIME(0) you have the desired format:
SELECT *
FROM a
WHERE CAST(date as time(0)) ='09:12:00'
//CONVERT(data_type(length),expression,style)
SELECT * FROM a
WHERE CONVERT(varchar(8),[columnName],108) = '09:12:00'

Change SQL date format

I have a column with this format 20150228 and I need to change it to 02/28/2015. Is there any function in SQL to do it.
Thanks
You can use this assuming the date is stored as a varchar:
SELECT CONVERT(VARCHAR(50),CONVERT(DATE,'20150228',112),101)
It returns:
02/28/2015
Use FORMAT function:
SELECT CONVERT(NVARCHAR(10), GETDATE(), 101)

How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2?

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