Microsoft SQL server edit imported file - sql-server

I'm new in SQL server. I have imported an Excel file into my SQL Server Database. There is a column with the Name of myDate and it shows dates like: 20.07.2018.
I did right click on my table, then I chose "Select 1000 rows" and added the convert code there but it didn't work! I have the same result as before! (the Format didn't change). Is there anyone who knows why it didn't change?
Here is what I wrote:
select convert (varchar,[dateColumn],102) as date2
from [test1].[dbo].[change1]
Go

Having a column's name as myDate doesn't mean its datatype is date, datetime or datetime2. Apparently your data type is VARCHAR. You should use date, datetime or datetime2 for such a column.
For conversion you can do it like this:
SET DATEFORMAT DMY;
SELECT convert (varchar, TRY_CAST(myDate AS DATE),102) as date2
from [test1].[dbo].[change1];
(Again this is questionable why would you convert to a varchar).

Related

Converting text to date format SSIS

I am using SSIS to import data from Excel to SQL Server database.
The date column has nvarchar data type, and I want to change it to a date data type YYYY-MM-DD.
If the value is null then I want to get null.
I used [mycolumn] date, on the create a new table option on the OLE DB destination.
Is that right?
Add a Data Conversion Data Flow Component.
Right click, Show Advanced Editor...
Goto Input and Output Properties
Expand Data Conversion Output and click on the date column
Set FastParse to True in the Custom Properties
Make sure the data type is set to database date [DT_DBDATE]
Run the ssis package again the yyyymmdd dates should flow smoothly from nvarchar to date
Convert [mycolumn] to the needed DATE format before inserting it to the table. Try this:-
SELECT CONVERT(CHAR(10), [mycolumn],126)

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

SQLite DateTime Comparison With VARCHAR

I got some problem with SQLite DataTime Filtering.
I used System.Data.SQLite in my wpf project.
I can filter using the following sql query.
SELECT * FROM tblTest WHERE CAST(testDate As date) > CAST('28/09/2015' As date);
According to above sql, I get all the dates bigger than 28/09/2015. btw testDate field is Varchar data type in my sqlite table.
When I use the BETWEEN clause, I get nothing.
SELECT * FROM tblTest WHERE CAST(testDate As date) BETWEEN CAST('28/09/2015' as date) AND CAST('10/10/2015' as date);
How should I filter date in between?
dd/mm/yyyy is not a valid TimeString in SQLite.
The correct format is yyyy-mm-dd.
As reported here,
valid TimeStrings in SQLite are:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
And you don't need to cast to... date (which is not an existing data type in SQLite, and it will fall back to TEXT).
Reference on SQLite DataTypes: https://www.sqlite.org/datatype3.html
Also note that the semicolon (;) at the end of queries and commands is completely useless, since SQLite won't concatenate SQL statements.
In the end, your query should really be
SELECT * FROM tblTest WHERE testDate BETWEEN '2015-09-28' AND '2015-10-10'

Timestamp confusion in SQL Server

I have a TimeStamp (varchar(50),null) column in my SQL Server 2008 table which looks misleading by the name TimeStamp. I mean it appears as if it's a datatype timestamp but it's varchar.
But it has values like 201403240004 which looks like a date. Can I convert it into date and use?
Read online that timestamp is only a sequence of numbers and has nothing to do with date and time.
You can.
Providing that the format is YYYYMMDDHHmm, a simple way to do that would be:
SELECT CONVERT(DATETIME,
SUBSTRING([TimeStamp],1,4)+'-'+SUBSTRING([TimeStamp],5,2)+'-'
+SUBSTRING([TimeStamp],7,2)+' '+SUBSTRING([TimeStamp],9,2)+':'
+SUBSTRING([TimeStamp],11,2)+':00.000')
FROM Table
This will take this "timestamp" and first transform it to SQL-readable datetime string, i.e. for your example it would be 2014-03-24 00:04:00.000. Then, it will be easily converted to datetime.
Yes, your column should be convertible to DATETIME, but you may have to do the converison yourself if CONVERT() does support the format.
I can't tell from you example what the time format really is.
If it is YYYYMMDDHHMM them
SELECT CONVERT(DATETIME,LEFT('201403240004',8),112)
+CONVERT(DATETIME,SUBSTRING('201403240004',9,2)+ ':' + RIGHT('201403240004',2)+':00' ,108)

Resources