I'm wondering if anyone can help me?
I've got a table with two string columns that are technically dates (they are both formatted in the following way dd-mm-yyyy).
I need these to be in datetime format (specifically yyyy-mm-dd hh:mm:ss.mmm)
I've include a small screenshot of what the data looks like.
Any help here would be brilliant. I need this for work and we're all stumped!
I'm using SSMS
Try the below query
DECLARE #Date1 VARCHAR(50) = '31-01-2016'
SELECT CONVERT(datetime, #Date1, 103)
Here is a simple way
Declare #String varchar(25) = '17/02/2013'
Select convert(datetime, #String, 103)
Returns 2013-02-17 00:00:00.000
Ok to update your data and assuming you have a new DATETIME field to
Update YourTable set YourNewDateField = convert(datetime, YourDateStringField, 103)
Related
I am trying to figure out why this example does not work as expected. I am using SQL Server 2017.
DECLARE #testDate DATETIME = CONVERT(VARCHAR(10), GETDATE(), 110)
SELECT CONVERT(VARCHAR(10), GETDATE(), 110)
SELECT #testDate
The first select gives the correct format but instead the second select gives incorrect format. The same happens when i try to cast it from NVARCHAR. Can someone explain to me why this is happening? It's like the variable is not properly storing the result of convert.
EDIT: You can see the results below the first select returns a different format than the second one.
EDIT2: Thank you for your answer but my question was quite specific on why those 2 selects return different results and it was mainly educational. Not to analyze all the business requirements, I gave 3 lines and i asked why I am getting those results. Simple as that. What i did not understood/notice was that the datetime object was casting the result of the CONVERT to a different format by itself.
You are converting Varchar to DateTime
See these examples:
DECLARE #testDate2 DATETIME = '08-27-2019'
SELECT #testDate2
DECLARE #testDate3 DATETIME = '2019-08-27'
SELECT #testDate3
SQL always convert values to default DateTime format.
Both of them converted to 2019-08-27 00:00:00.000
In this case SELECT CONVERT(VARCHAR(10), GETDATE(), 110), your selecting formated Nvarchar. style 110 => mm-dd-yyyy
If you want to store converted DateTime you can use like this:
DECLARE #testDate4 VARCHAR(10) = CONVERT(VARCHAR(10), GETDATE(), 110)
I want to convert an integer to a date format. I am using the following script:
SELECT CAST(7549 as datetime)
It returns me a date 1920-09-02
However, 7549 actual date is 1988-08-31
Can anyone please help me how to perform correct mapping?
See the reverse
DECLARE #YourDate AS Datetime
SET #YourDate = '1920-09-02'
SELECT CAST(#YourDate AS INT) --7549
SET #YourDate = '1988-08-31'
SELECT CAST(#YourDate AS INT) -- 32384
If you want the result as '1988-08-31 00:00:00.000', try with the following scripts.
SELECT CAST(7549 as datetime)-CAST('1832-01-03' as datetime)
OR
SELECT CAST(7549 as datetime)+CAST(24835 as 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.
I have got this line
Declare #Startdate as Varchar(50)
Set #StartDate = dateadd(dd,-1,convert(datetime, convert(varchar, getdate(),101)))
This returns me
2014-05-19 00:00:00.000
Now I want to convert above to
20140519
Can someone please help.
Regards
How about this..
Declare #Startdate as Varchar(8)
Set #StartDate = CONVERT(VARCHAR(8), GETDATE()-1, 112)
SELECT #StartDate
RESULT: 20140519
t-sql's convert method lets you format dates in various ways.
112 looks like the style code you want.
I have one column capturedatetime(Char(30)):
2006-04-25T15:50:59.997000 PM
And I want to convert it and load it at other table column which have is in DateTime. either by T-sql or SSIS which ever way.
I have tried with:
select CONVERT(datetime, '2006-04-25T15:50:59.997000 PM', 126)
But it creates an error:
Conversion failed when converting date and/or time from character string
Late update:
In this column I also have other data that is in a completely different format:
29-JAN-10 08.57.41.000000 PM
(1) STOP storing datetime data in string columns! This is nothing, nothing, nothing but trouble.
(2) Why on earth does your column get data in two different string formats that aren't even valid? Why does the string use 24 hour time and have AM/PM suffix? Why use a regional string format and Y2K disaster like 29-JAN-10?
Here is one way, but it's awfully ugly. I highly recommend you fix the SSIS process to give you valid datetime values in the first place, if not as datetimes, at least as valid ISO strings (yyyy-mm-ddThh:mm:ss.nnn):
DECLARE #x TABLE (d CHAR(30));
INSERT #x SELECT '2006-04-25T15:50:59.997000 PM'
UNION ALL SELECT '29-JAN-10 08.57.41.000000 PM';
SET LANGUAGE ENGLISH; -- this is important, else style 6 may not work
SELECT
CASE WHEN d LIKE '__[0-9]%' THEN
CONVERT(DATETIME, LEFT(d, 23))
WHEN d LIKE '[0-9][0-9]-%' THEN
CONVERT(DATETIME, CONVERT(CHAR(8),
CONVERT(DATETIME,REPLACE(LEFT(d,9),' ','-'),6),112)
+ ' ' + REPLACE(SUBSTRING(d,11,8),'.',':')
+ ' ' + RIGHT(RTRIM(d),2))
END
FROM #x;
The conversion for 126 requires no spaces ... I've got it to work like this:
declare #T varchar(50)
declare #dt datetime
set #T = '2006-04-25T15:50:59.997'
set #dt = convert(datetime,#t,126)
select #T, #dt
select convert(datetime,left('2006-04-25T15:50:59.997000 PM',23))
or
select convert(datetime,left(capturedatetime,23))
If you use cast, you do not even need to supply a format. Code snippet below tested on SQL 2012 Developer version.
declare #var_string varchar(50) = '2006-04-25T15:50:59.997';
declare #var_datetime datetime = cast(#var_string as datetime);
select #var_string as my_string, #var_datetime as my_variable;