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

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)

Related

Import or convert an ISO 8601 date extended with timezone data in TSQL

I need to import a flat file into an SQL Server table and it has timestamp data that is in this format:
20171207T000131.000-0600
I have imported as a string, tried to convert, but I am not having any luck.
Well, as I wrote in the comment, SQL Server uses DateTimeOffset to store time-zone aware date and time.
The problem is that you need to translate the no-delimiters ISO 8601 format you use now to the human-readable version of ISO 8601 format - YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm], in order to convert that value into a DateTimeOffset.
You do that by adding the delimiters where they are needed, using STUFF:
Stuff inserts a new string into the existing string, starting in the specified index, instead of the existing substring of the specified length. Since you do not want to remove any existing part of the original string, you use length 0.
I've also added in my demo suggestions of ways to convert that data into date and datetime2 (Not to DateTime, there's a bug with that data type!), in case you do not need accurate information (the time zone alone can easily account for a date change):
DECLARE #DTOString varchar(100) = '20171207T000131.000-0600'
SELECT CAST(LEFT(#DTOString, 8) As Date) As [Date],
CAST(
STUFF(
STUFF(
STUFF(
LEFT(#DTOString,19)
, 14, 0, ':')
, 12, 0, ':')
,9, 1, ' ') -- Replacing the T with a space
As DateTime2) AS [DateTime2], -- NOT to DateTime! there's a bug!
CAST(
STUFF(
STUFF(
STUFF(
STUFF(
STUFF(#DTOString, 23, 0, ':')
, 14, 0, ':')
, 12, 0, ':')
, 7, 0, '-')
, 5, 0, '-')
As DateTimeOffset) As [DateTimeOffset]
Result:
Date DateTime2 DateTimeOffset
07.12.2017 07.12.2017 00:01:31 07.12.2017 00:01:31 -06:00
In case you need only the date import it as a string and execute the following query
select cast(substring('20171207T000131.000-0600',1,8) as date)
i am extracting '20171207' and converting it to date
Try this:
SELECT CONVERT(DATETIME,
STUFF(STUFF(STUFF(STUFF(STUFF(STUFF('20171207T000131.000-0600',20,5,''),14,0,':'),12,0,':'),9,1,' '),7,0,'-'),5,0,'-'),120)

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

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

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

Create "BETWEEN/AND"-capable DATETIMEs from given DATETIME

I want to create two DATETIME variables I can use to check with BETWEEN AND when given just one DATETIME in a stored procedure on SQL Server 2008.
So, when I get 2012/12/31 15:32:12 as input, I want to generate two new variables out of that, being #from = 2012/12/31 00:00:00 and #to = 2012/12/31 23:59:59.
These two variables are used to check if the records lie between them - that is, are on the same day as the input date.
I fooled around using CAST and CONVERT, but I don't really konw how to manipulate the dates in the way I want.
Should I do this another way? Or are there functions I'm not aware of?
Now it is version independedt
declare #from datetime, #to datetime
SET #from = convert(varchar, convert(datetime, '2012/12/31 15:32:12', 111), 112)
SET #to = DATEADD(day, 1, #from)
select * from yourtable where test date >= #from AND date < #to
You can;
declare #input datetime = '2012/12/31 15:32:12'
declare #from datetime = dateadd(day, 0, datediff(day, 0, #input))
declare #to datetime = dateadd(second, -1, dateadd(day, 1, #from))
>>>
2012-12-31 00:00:00.000 2012-12-31 23:59:59.000
Be careful of accuracy on your #to. 23:59:59.001 is a valid date but won't show up in your range if you subtract an entire second.
It is more common to set your #from and then use < #from + 1 instead of BETWEEN. (The plus adds whole days in SQL).
First convert your input date to a varchar using the appropriate date format(111 in this case), For the to date, append the midnight hour
Then cast your varchar back to datetime.
Example :
SELECT #from = CAST(CONVERT(VARCHAR(10), GETDATE(), 111) AS DATETIME)
,#to = CAST(CONVERT(VARCHAR(10), GETDATE(), 111)+' 23:59:59:997' AS DATETIME)
Here is a useful chart of datetime formats with brief explanations.
http://www.sql-server-helper.com/tips/date-formats.aspx

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