Convert varchar column to smalldatetime - sql-server

I have a table with 800+ records. In this table I have a column named 'Data' of varchar(10) datatype which contains dates in dd.MM.yyyy format.I want to convert it to smalldatetime.
I've tried converting it using Enterprise Management Studio Express, but I receive this error:
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.
How can I convert it?

I think you'll need to do a little string manipulation to get this to work as I think SQL is expecting 'MM.dd.yyyy'. So, update your table to flip-flop the month and day first, then the conversion should go through.
update YourTable
set Data = SUBSTRING(Data,4,3) + LEFT(Data,3) + RIGHT(Data,4)

You can use:
SELECT ID, CAST(VarcharCol As SmallDateTime) as DateTimeCol From Test1
This will return a table with varcharcol values as smalldatetime
Then update the content of varcharcol with the new values.

If you don't want implicit conversion to smalldatetime, you should use CONVERT and the argument style.
dd.MM.yyyy format correspond to style 104.
For example :
SELECT CONVERT(smalldatetime, '31.12.2018', 104) AS "Result"
Result
------
2018-12-31 00:00:00

Related

MS SQL error : conversion of a varchar data type to a datetime data type resulted error

I am using MS SQL Server. One table column is defined as order_date varchar(25) and is stored in a format like 05/11/2015 07:54:16
In my select query, I am trying to convert that into a date format like (yyyy-mm-dd HH:MM:SS:000, e.g. 2015-05-11 08:03:10.000
I have tried with
select CONVERT(varchar(50), CAST(order_date AS datetime),121) from <table>
In my table I have around 500 records, but after fetching 10 records in my expected format, I get this error error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Is there any issue with my conversion?
There are a couple of problems here. The first is your data type choice, but I'll just repeat my comment for that: ""am trying to convert that into date format "* This is totally the wrong approach. Stop storing dates as a varchar use a date and time data type. The reason you have this error is because your poor data type choices. Fix that and get the presentation layer to work about the formatting."
Now, moving on. You have your expression below:
CONVERT(varchar(50), CAST(order_date AS datetime),121)
Firstly, as your value is a varchar, you need to tell SQL Server the format it is in; dd/MM/yyyy hh:mm:ss (I guess as '05/11' is ambigous)) is not unambiguous. What you have is the UK style, which is style 103:
CONVERT(datetime,'05/11/2015 07:54:16',103)
Now you can convert that to your ISO format:
CONVERT(varchar(23),CONVERT(datetime,'05/11/2015 07:54:16',103),121)
This returns the varchar value '2015-11-05 07:54:16.000'
I'd try to see what the value of order_date is at the time of the error. Perhaps what you assume to be a datetime string in mm/dd/yyyy... format is actually in dd/mm/yyyy....
I.e., the following outputs 2015-05-11 07:54:16.000 as my system is set to datetime format of mm/dd/yyyy.
declare #order_date varchar(100) = '05/11/2015 07:54:16'; -- mm/dd/yyyy...
print CONVERT(varchar(50), CAST(#order_date AS datetime),121);
the following throws an error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
set #order_date = '13/09/2015 07:54:16'; -- dd/mm/yyyy...
print CONVERT(varchar(50), CAST(#order_date AS datetime),121);

SQL Server: convert StartDATE 20140804 Nvarchar to Datetime2

I'm using SQL Server 2008 and I did an import from a flat file. I couldn't import the datetime column properly so I specified it temporarily as a nvarchar(50).
Now I want to convert it to datetime2 format. However when doing so, I get the error
Conversion failed when converting date and/or time from character string.
The data that is currently in my nvarchar(50) column looks like this:
20140804
And I need to convert it to:
2014-08-04 00:00:00.0000000.
Note that I do not only want to convert one date, but all StartDATE values in my table and insert them to another table
Any help is appreciated.
Insert into targettab(datecol,<othercols....>)
select cast(datefield as datetime2),<othercols...> from sourcetab
You can use cast function
you need to convert to char first because converting to int adds those days to 1900-01-01
select CONVERT (datetime,convert(char(8),rnwl_efctv_dt ))
here are some examples
select CONVERT (datetime,5)
1900-01-06 00:00:00.000
select CONVERT (datetime,20100101)
blows up, because you can't add 20100101 days to 1900-01-01..you go above the limit
convert to char first
declare #i int
select #i = 20100101
select CONVERT (datetime,convert(char(8),#i))
SELECT convert(varchar, StartDATE , 113) from ur table

Convert varchar to datetime in sqlserver table

In SQL Server I have a column called "dateCreation" defined as varchar(50) and I would like to convert it to datetime type. If I change the column's type to datetime with the MS SQL Server Management Studio I get the following warning:
'atPropostes' table
- Warning: Data might be lost converting column 'dateCreation' from 'varchar(50)'.
Then If I execute this:
SELECT CONVERT(Datetime, dateCreation, 120) as dateCreation from atPropostes -- to convert it to Datetime
I get the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
What would be the best way to
(1) change the column type to datetime and
(2) convert all the registers from varchar(50) to Datetime type?
Thanks!
I assume that you are using SQL Server 2012
Try like this
SELECT TRY_CONVERT(DATETIME, dateCreation, 120) AS dateCreation
FROM atPropostes
Note: Please make sure that your date stored in varchar type have consistent date format. It should be something like yyyy-mm-dd or mm-dd-yyyy or.....

SQL Server Converting varchar to datetime

I got a problem in SQL Server with converting a varchar to datetime. I would like to convert/update whole column [datelog] in table:
[dd.mm.yyyy hh:mm:ss]` to `[yyyy-mm-dd hh:mm:ss]
In SQL Server 2012+ you can use PARSE or TRY_PARSE to parse a text value according to a specific culture.
Assuming your text follows the German culture ('de-DE') you can parse it to datetime with :
select PARSE('24.11.2015 13:10:55' as datetime using 'de-DE')
eg:
select PARSE(datelog as datetime using 'de-DE')
The real solution though would be to use the correct field type, ie datetime. It's almost guaranteed that someone, somewhere will either enter text with the wrong format or try to convert the text using the wrong culture.
Date types on the other hand, have no format, they are simply binary values. Using them is faster, safer and easier.
Tricky solution,
DECLARE #inputDate AS VARCHAR(20)='21.11.2015 06:59:00' -- [dd.mm.yyyy hh:mm:ss]
SET #inputDate = REPLACE(#inputDate ,'.' ,'/')
SELECT CONVERT(VARCHAR(24) ,CONVERT(DATETIME ,#inputDate ,103) ,121) OutputDate -- [yyyy-mm-dd hh:mm:ss]
Still you need to change as per your table columns.
temp table with one column of type varchar
create table #temp3 (someDate varchar (30))
insert into #temp3 values ('23.03.1989 15:23:43')
using a combination of concat, substring and right
select concat
(
SUBSTRING(someDate,7,4),'-', SUBSTRING(someDate,4,2),'-',SUBSTRING(someDate,1,2), ' ', right(someDate, 8)
)
from #temp3
gives: 1989-03-23 15:23:43

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value in SQL Server 2005

I have written a view in which date from table which is converted as
convert(varchar, date, 103)
and taken alias as DATE1 and then I have written query as follows :
select DATE1
from date_demo
WHERE MONTH(DATE1) = '12'
I want to retrieve only specific records with the month = 'december' that is 12
I got an error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
This is in SQL Server 2005 pls help me
As david suggested ,you should be using the date column instead of converting to string then back to datetime column.
However when you convert it back to datetime ,you need to specify the Date Time Style
select DATE1 from date_demo
WHERE MONTH(convert(datetime,DATE1,103))= '12'
Moreover you need to specify the length of varchar data type Read this artcile bad habits to kick declaring varchar without length

Resources