SQL query to convert the value to MM/DD/YY and HH:MM:SS - sql-server

My table TRANS contains T_STAMP column with the value '20170721154922' is a YYYYMMDDHHMMSS format. I am trying to write a query to display as MM/DD/YY in one column and HH:MM:SS in another column.
I am trying to convert this with the method CONVERT(VARCHAR(10),GETDATE(),10)
but I need full snippet to execute.
Could you help .

The following query can be return the exact format you required, the date as MM/DD/YY and time as HH:MM:SS:
SELECT CONVERT(VARCHAR(8), CONVERT(DATE, LEFT(T_STAMP, 8)), 1) AS DateValue,
CONVERT(VARCHAR(8), CONVERT(TIME,
SUBSTRING(T_STAMP, 9, 2) + ':'
+ SUBSTRING(T_STAMP, 11, 2) + ':'
+ SUBSTRING(T_STAMP, 13, 2)), 8) AS TimeValue
FROM Trans
Output:
DateValue | TimeValue
-----------|-----------
07/21/17 | 15:49:22

The following query will convert a value from the given format (YYYYMMDDHHMMSS) to DATE and TIME
DECLARE #T_STAMP NVARCHAR(100)='20170721154922'
SELECT CONVERT(DATE,LEFT(#T_STAMP,8)) T_DATE
,CONVERT(TIME,SUBSTRING(#T_STAMP,9,2)+':'
+SUBSTRING(#T_STAMP,11,2)+':'
+SUBSTRING(#T_STAMP,13,2)) T_TIME
As you mentioned the values are stored in YYYYMMDDHHMMSS format, so hope the values always be in fixed length (length 14).
Note:- As commented by Jens, Storing timestamps as formated strings is no good idea. Better store it in a timestamp datatype.

SELECT FORMAT(GETDATE() , 'MM/dd/yyyy HH:mm:ss')
the above would return something like this(21/07/2017 16:02:20).

select date_format(str_to_date('20170721154922', '%Y%m%d%H%S'), '%m%d%y');

'20170721154922' is a very very bad format to store date...
DECLARE #d varchar(14) = '20170721154922';
DECLARE #date DATETIME = CONVERT(datetime2, LEFT(#d, 8), 112);
DECLARE #time DATETIME = CONVERT(datetime2, CONCAT(SUBSTRING(#d, 9, 2), ':', SUBSTRING(#d, 11, 2), ':', SUBSTRING(#d, 13, 2)), 108);
SELECT FORMAT(#date, 'MM/dd/yy') date, FORMAT(#time, 'HH:mm:ss') time

Related

Cast '2016-07-2914:50:13.75300' varchar to datetime T-SQL

I have varchar '2016-07-2914:50:13.75300' and want to convert it to datetime data type.
I tried with select cast('2016-07-2914:50:13.75300' as datetime)
but I am getting
Conversion failed when converting date and/or time from character string.
Insert a space between DDHH and lose the last two MS digits:
select cast(left(stuff('2016-07-2914:50:13.75300', 11, 0, ' '), 23) as datetime)
why don't you simply use
select stuff('2016-07-2914:50:13.75300', 11, 0, ' ') ?
Different approach using SUBSTRING, of course STUFF is the way to go.
DECLARE #TIME VARCHAR(50) = '2016-07-2914:50:13.75300'
SELECT CAST(SUBSTRING(#TIME, 1, 10) AS DATETIME) +
CAST(SUBSTRING(#TIME, 11, LEN(#TIME) - 10) AS TIME)
Result:
2016-07-29 14:50:13.753

T-SQL : separating datetime into date and time and then converting to numeric

My requirement is much more specific than what I found - I have a table that stores a datetime value. I need to retrieve that value, separate it into a date and a time, and then convert both those values into integer values. This is the field I'm trying to retrieve:
release_date = 2016-06-28 07:04:17.960
This needs to be split like so:
--numeric date as yyyymmdd from above value would be:
#my_numeric_date = 20160628
--numeric time as hhmmss from above value would be:
#my_numeric_time = 70417
Is there a relatively straightforward way of achieving this?
The last time I needed this, I used these two statements:
CONVERT(INTEGER, CONVERT(VARCHAR, release_date, 112)),
CONVERT(INTEGER, REPLACE(CONVERT(VARCHAR, release_date, 108), ':', ''))
So, to save it in variables use:
SELECT
#my_numeric_date = CONVERT(INTEGER, CONVERT(VARCHAR, release_date, 112)),
#my_numeric_time = CONVERT(INTEGER, REPLACE(CONVERT(VARCHAR, release_date, 108), ':', ''))
Declare #Date DateTime = GetDate()
Select DateInt = (Year(#Date)*10000)+(Month(#Date)*100)+Day(#Date)
,TimeInt = (DatePart(HH,#Date)*10000)+(DatePart(MINUTE,#Date)*100)+DatePart(SECOND,#Date)
Returns
DateInt TimeInt
20160628 104510
-- The DateTime was 2016-06-28 10:45:10.017

Convert String of datetime(201004301342) into Datetime value(103 Format)

I Have a string like as given below
201004301342
Need to convert it into dd/mm/yy format
Can anyone help me please?
This will change the value to a datetime. A datetime has no format until you convert back to a string again.
You can use stuff to change the string value to 20100430 13:42 and then cast to datetime.
declare #Date varchar(12)
set #Date='201004301342'
select cast(stuff(stuff(#Date, 11, 0, ':'), 9, 0, ' ') as datetime)
if you want a string and start string is always with this format then
declare #date varchar(50)
set #date = '201004301342'
SELECT convert(varchar, cast(substring(#date, 1, 8) as datetime), 103)

VARCHAR TO DATE

Currently i have a column called date as varchar instead of date time. the format is currently 'sep12'. How can i transfer this to a date type?
I know this is the template but I'm not sure what to change the numbers to due to my current format as MMMYY.
CAST (
SUBSTRING(date, 3, 2) + '/' +
SUBSTRING(date, 1, 2) + '/' +
SUBSTRING(date, 5, 4)
AS DATETIME)
any help would be greatly appreciated
At the moment when ordering by the date field it is ordering like apr09, apr10. aug09, aug10 but I need it to be in year order
First, storing dates in this manner is a terrible idea. But if you know that the year will always start with 20 then you can use something like this (see SQL Fiddle Demo):
declare #dt varchar(10) = 'Sep12'
select convert(datetime, left(#dt, 3) + '01 20' + right(#dt, 2), 100)
If pulling data from your table:
select convert(datetime, left(yourDateField, 3) + '01 20' + right(yourDateField, 2), 100)
from yourTable

SQL Converting character to datetime

Does anybody now how to convert this character to date or datetime:
'10022011'
Quick & dirty:
select convert(datetime, stuff(stuff('10022011', 5,0,'-'),3,0,'-'))
However, you might want to consider converting the string into ISO standard date format:
declare #d char(8)
select #d = '10022011'
select convert(datetime, substring(#d,5,4) + '-' + substring(#d,3,2) + '-' + substring(#d, 1, 2))
in order to avoid ambiguity. 10-02-2011 has different meanings depending on which side of the pond you are.
SELECT convert(datetime, STUFF(STUFF('10022011',3,0,'-'),6,0,'-'), 103)
the number at the end is the Sql format you want the date to output

Resources