Change Dateformat for nvarchar by sql script - sql

I have to change the datetime format for a given string in a SQL query.
The database is SQL Server 2005, the column is of type nvarchar(1024).
Atm the data is like 2014-05-21, given the date from today as example.
I should write a script in which I can convert from 2014-05-21 to 21.05.2014.
What will be the fastest / easiest way to do this?

You can give a try as below :-
SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY]
For more information :-
http://www.sql-server-helper.com/tips/date-formats.aspx

Related

HOW TO INSERT DATE WITH DOTS FORMAT IN SQL

I want to insert a date like 31.12.2021 in SQL, and I get something wrong
what should I write to make it works?
In sql-server you can use convert(varchar, [date you want to convert], 104) if your column is a date datatype as shown in microsoft sql-server cast and convert docs.

SQL Server dates

I use Classic ASP and SQL Server 2012. I have a program that inputs into the database using now(). Originally it was formnatdatetime(now(),2).
For the majority of time everything was fine but for some reason (which is why I'm asking) occasionally it would put the date in the database in the wrong format. So instead of ddmmyyyy it would be mmddyyyy.
I cannot see how or why when the code is the same, the database is the same. I assume now() or getdate() in TSQL is server specific.
You could use the format function
SELECT FORMAT(GETDATE(),'ddMMyyyy')
Results:
11042015
MS SQL Server 2012 does not know NOW() function! As Gordon Linoff mentioned, you need to use GETDATE() instead!
I'd suggest to read this: Date and Time Data Types and Functions (Transact-SQL) together with: CAST and CONVERT (Transact-SQL)
SELECT CONVERT(VARCHAR(30), GETDATE(), 103) AS CustomDateFormat
-- returns: dd/mm/yyyy
Note that sometimes, sql server can't convert date stored as a string, so you need to use SET DATEFORMAT, especially when sql server uses different date format:
SET DATEFORMAT dmy;
SELECT CONVERT(VARCHAR(30), '11/04/2015', 121) AS NewDateTime

What format is this date and how can I convert it to a regular datetime?

Amazon for sellers provides order reports. I'm trying to import one of these order reports into a Sql Server database:
Their date fields look like this:
2014-04-30T12:17:28-07:00
2014-04-30T12:24:43-07:00
2014-04-30T12:25:34-07:00
2014-04-30T12:46:02-07:00
2014-07-27T13:10:02-07:00
2014-07-27T13:12:09-07:00
2014-07-27T13:20:42-07:00
2014-07-27T13:23:25-07:00
2014-07-27T13:29:10-07:00
2014-07-27T13:36:16-07:00
2014-07-27T13:51:41-07:00
I cannot figure out which data type to assign this date.
How do I convert this field to be a regular datetime? The solution could be SQL or SSIS or a combination.
Try this.....
SQL Server
SELECT CONVERT(DATETIME,
CONVERT(DATETIME2, '2014-04-30T12:17:28-07:00')
)
RESULT: 2014-04-30 12:17:28.000 --<-- SQL SERVER DATETIME
SSIS
Convert your input column to SSIS Data type DT_DBTIMESTAMP2
Add a derived column task and use the following expression
(DT_DBTIMESTAMP)Input_Column_Name
essentially you are doing the same thing but result will be the same.

How to convert varchar to date

I am working in SQL Server 2008 R2
I need to convert a varchar(50) field in a view to a date format.
I have a view that uses the following to create the field delivered_date:
convert(varchar(50),[delvd_dt],110) as [delivered_date]
The formatted field looks like : 2012-03-11 16:24:42.0000000
I need the results to be a date so that I can check for dates within a range. I would prefer to do it within the view so that I can use SSRS to create the range for the report.
Have you tried cast()? The following returns the right date for me:
select CAST('2012-03-11 16:24:42.0000000' as DATE)
Perhaps you can simply truncate the millis and use CONVERT:
SELECT Convert(datetime, LEFT('2012-03-11 16:24:42.0000000', 19), 102) AS [Date]
Demo
This works even in MS SQL-Server 2005.
CAST and CONVERT (Transact-SQL)
Try this link website shows several formatting options.
Example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 105)

Date conversion failed from string

I have a date saved in the format DD/MM/YYYY from a flat file. "20/04/2013"
When I try to insert it into my SQL Server database it changes the value to MM/DD/YYYY.
So of course there in no month 20 and my code fail. How can I work around this?
I have tried stuff like this and I had no luck.
SELECT CONVERT(datetime, CONVERT(varchar, '20/04/2013', 101))
just do this directly,
SELECT CONVERT(datetime, '20/04/2013', 103)
On SQL Server you need to use the SET DATEFORMAT option. (See http://msdn.microsoft.com/en-us/library/ms189491.aspx).
In your case, you would need to issue the following command before executing the above SELECT-statement:
SET DATEFORMAT dmy
Alternatively, it is possible to globally change the DATEFORMAT setting for the server.
Definition and Usage
The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
select convert(datetime, '20/04/2013', 103)
enter link description here