I have a database with dates in the following long integer format:
20100101000000
Where that would be Jan 1st, 2010, 00:00:00 for the time of day.
I want to be able to convert this to normal SQL Datetime syntax and back. Is this possible? I can only get this far:
SELECT CAST(CURRENT_TIMESTAMP as int);
Which returns '40556' - not exactly what Im after.
You could use substring to convert your string to yyyy-MM-dd HH:mm:ss notation, and cast that to a datetime:
select convert(datetime,
substring(sub.Dt,1,4) + '-' +
substring(sub.Dt,5,2) + '-' +
substring(sub.Dt,7,2) + ' ' +
substring(sub.Dt,9,2) + ':' +
substring(sub.Dt,11,2) + ':' +
substring(sub.Dt,13,2))
from (
select '20100101000000' as Dt
) sub
You can stuff it with the spaces and colons required:
select
stuff(stuff(stuff('20100101000000',9,0,' '),12,0,':'),15,0,':') STR,
convert(datetime,stuff(stuff(stuff('20100101000000',9,0,' '),12,0,':'),15,0,':')) DT;
The result is
STR | DT
20100101 00:00:00 | 2010-01-01 00:00:00.000
The first one shows the string it is converted to, the 2nd the datetime value.
To go in reverse
select
convert(char(8),getdate(),112) + replace(convert(varchar(30),getdate(),108),':','');
Replace the constants '20100101000000' and "getdate()" with field names where required if selecting from a table.
Related
I am converting a date using CONVERT(varchar,DateOfBirth,101) for birthdates.
I want to show these dates with the current year, I've tried REPLACE but you can't use wildcards with it and when I use DATEPART, it doesn't format with the right digits for month and day. I also can't add years because they are wildly different birthdates. Thanks.
If you want to display the date as a string in 101 format for current year, one option uses a direct format():
format(DateOfBirth, 'MM/dd/2020')
You can compute the current date dynamically:
format(DateOfBirth, concat('MM/dd/', year(getdate())))
On the other hand, if you want your result as a date, then you could use datefromparts():
datefromparts(year(getdate()), month(DateOfBirth), day(DateOfBirth))
If it is a datevalue, you can use FORMAT function. If it is a character value, you can use RIGHT and REPLACE.
DECLARE #dateValue DATETIME = '05/12/1999'
DECLARE #dateCharValue VARCHAR(12) = '05/12/1999'
SELECT FORMAT(#dateValue, 'MM/dd/2020')
SELECT REPLACE(#dateCharValue, RIGHT(#dateCharValue,4),2020)
--Result
05/12/2020
This could helped you:
The code CONVERT(varchar(5),GETDATE(),1) return this 05/27 and then just add the year of the date
SELECT CONVERT(varchar(5),GETDATE(),1) + '/' + cast(year(getdate()) as varchar)
Or
SELECT CONVERT(varchar(5),GETDATE(),1) + '/' + convert(varchar,year(getdate()))
The result of both:
05/27/2020 --(This is my current date n.n )
This work but if you use a string something like your example DateOfBirth will be the variable and if this is a string (DateOfBirth = '5/27/1987') you need to convert the string DateOfBirth to Date:
SELECT CONVERT(varchar(5),convert(date,DateOfBirth),1) + '/' + cast(year(GETDATE()) as varchar)
Or
SELECT CONVERT(varchar(5),convert(date,DateOfBirth),1) + '/' + convert(varchar,year(GETDATE()))
The Result of Both :
05/27/2020
I want to convert DATETIME to VARCHAR (month/day/year) like this:
10/09/2018 12:00:00.000
I tried using
Convert(VARCHAR(MAX),[Date & Time Added]),121)
but it returns
yyyy-mm-dd hh:mi:ss.mmm
I need the / format with time, I am Using SQL Server 2012.
You can use the FORMAT function:
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy HH:mm:ss.fff')
-- 10/09/2018 00:58:52.557
Complete list of format specifiers is actually available in the .NET documentation.
If FORMAT function is unavailable you could simply format in a known format and use string functions to re-arrange the year, month, day and time parts. For example:
SELECT SUBSTRING(DateStr, 6, 2) + '/' + SUBSTRING(DateStr, 9, 2) + '/' + SUBSTRING(DateStr, 1, 4) + ' ' + SUBSTRING(DateStr, 12, 12)
FROM (
SELECT CONVERT(VARCHAR(23), GETDATE(), 126) -- ISO8601 / yyyy-mm-ddThh:mi:ss.mmm
) AS CA(DateStr)
-- 10/09/2018 01:12:50.833
SELECT CONVERT(VARCHAR(10), Date, 101) + ' ' + CONVERT(VARCHAR(12), Date, 114)
FROM (
SELECT GETDATE()
) AS CA(Date)
-- 10/09/2018 01:19:38:463
This is something that would usually be done in the presentation layer, but if you need to do it in the data layer you can use FORMAT (documentation here)
Syntax is: FORMAT ( value, format [, culture ] )
So in your case (edited to add AM/PM designation per your comments): FORMAT([Date & Time Added], 'dd/MM/yyy hh:mm:ss.fff tt')
Custom date format string options are detailed here
If a table has 3 columns for a person's date of birth (ex: BirthMonth, BirthDay, BirthYear), what is the best way to combine them into one column with a format of MM/dd/yy and (obviously) a date datatype? Thank you.
You say "...put them into one field with a format of MM/dd/yy and (obviously) a date datatype? " but a date type does not have a display format. That's a presentation concern.
SQL Server 2012 onwards:
DATEFROMPARTS(year, month, day)
or
cast((DATEFROMPARTS(year, month, day) as date) to cast to pure date and remove default time portion
e.g.
SELECT DATEFROMPARTS(2010, 12, 31) AS Result;
Simply, combine all 3 columns into YYYY-MM-DD and then convert this string into Datetime.
For example:
Select
cast(cast(Year as string) + '-' +
cast(Month as string) + '-' +
cast(Day as string) as datetime)
Now you can format it any date format.
For SQL Server 2012, use the DATEFROMPARTS() function as suggested by Mitch. For prior versions:
Assuming BirthMonth, BirthDay, and BirthYear are all integral values, you can cast them:
CAST(CAST(BirthYear AS varchar) + '-' + CAST(BirthMonth AS varchar) + '-' + CAST(BirthDay AS varchar) AS DATETIME)
Alternatively, you can do it like this:
DATEADD(MONTH, BirthMonth - 1, DATEADD(YEAR, BirthYear - 1900, BirthDay - 1));
This has the advantage of not doing any string conversions and it's not dependent on any date format. This utilizes the fact that SQL Server's internal representation for the date is the number of days since 1 January 1900.
If BirthMonth, BirthDay, and BirthYear are valid string values, then you can cast them directly:
CAST(BirthYear + '-' + BirthMonth + '-' + BirthDay AS DATETIME)
And if they are guaranteed to have 2 digits for both the month and day (and 4 or 2 digits for the year), then you can cast them in a more compact way without hyphens:
CAST(BirthYear + BirthMonth + BirthDay AS DATETIME)
The data from source has string data type (JAN-2014) I want valid date and convert string to date. How can I do this in SSIS 2005?
Example : Jan-2014.
I want convert this date to 201401 type. Can anyone explain how I can go about this..
Try this:-
SUBSTRING(Col,FINDSTRING(Col,"-",1) + 1,4) +
RIGHT("0" +
(DT_WSTR,2)DATEPART("mm",
(DT_DATE)(SUBSTRING(Col,1,4) +
SUBSTRING(Col,FINDSTRING(Col,"-",1) +
1,4)
)),2)
The above expression will convert Jan-2014 to 201401 string format.
If I have a date string in the format of CMMDDYY
C = 0, 1 or 2 where 0 represents 19, 1 represents 20 and 2 represents 21
Example:
1022511 would be 02/25/2011 in mm/dd/yyyy format.
Is there a way to convert this into mm/dd/yyyy format in sql server?
Is CMMDDYY even a valid date format? I haven't found much information on it.
The Year algorithm is simply: (19 + int(C))*100 + int(yy)
Try this:
declare #st nvarchar(7)
set #st = '1030609'
select
cast(
substring(#st,2,2) + '/'
+ substring(#st,4,2) + '/'
+ cast((19 + cast(substring(#st,1,1) as int) )* 100
+ cast(substring(#st,6,2) as int) as nvarchar)
as datetime)
This outputs:
It doesn't sound like a built in date format but you should be able to find it on msdn if it is. If not you could write a function in SQL server that parses a string in that format to a DateTime object