How to convert Date format in SQL Server? - sql-server

Basically I want to convert date format from 'yyyy-mm-dd' to 'mm/dd/yyyy'.
Now here is the thing :
I used this to select current date.
Select GetDate();
Which shows the format as : 2015-07-10 14:29:30.227
Then :
Select CONVERT(VARCHAR, GETDATE(), 101)
It is working and displaying the result as : 07/10/2015
Now when I try to do this:
Select CONVERT(DATE, CAST('2015-06-15' AS DATE), 101)
And it does not show the desired result. Instead it shows the same format as : 2015-06-15
Any suggesstion would be of great help!!

Try this:
Select CONVERT(Varchar(10), CAST('2015-06-15' AS DATE), 101)

Related

Format date as 'DD Month YYYY'

Just have a mssql query that has a date in the format:
'2016-03-22 00:00:00.000'
I need to format it as:
'22 March 2016'
I'm using SQL Server 2012. I've tried googling and the usual 106, 112 codes don't seem to work.
Is there a specific code format I can use?
Try the FORMAT function:
SELECT FORMAT(GETDATE(), 'D', 'en-gb')
If your version does not support the FORMAT function, you can do it by concatenating the date parts:
SELECT
RIGHT('00' + CAST(DATEPART(DAY, GETDATE()) AS VARCHAR(2)), 2) + ' ' +
DATENAME(MONTH, GETDATE()) + ' ' +
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4))
Use Format Function in SQL Server 2012
SELECT FORMAT(GETDATE(),'dd-MMMM-yyyy')
See Image for your datatype ref
Try this query:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, '2016-03-22 00:00:00.000'), 106)
You need to convert into DATETIME then you will get your desired output
More Datetime Sql formate
SQLFiddle
This may help you
Try Convert function for SQL server:
select convert(varchar(11),cast('2016-03-22 00:00:00.000' as datetime), 106)
How to change day, month, year in PHP:
echo $purchase_date=date('F-Y',strtotime('2022-03-22 00:00:00.000'));
Answer:
March-2022

Convert Datetime format to varchar in SQL Server

I have this code in my view in SQL Server to convert datetime to varchar.
convert(datetime, Product_Info.created_date, 101)
but I get this error
Update cannot proceed due to validation errors.
Please correct the following errors and try again.
SQL70569 :: A column name is required.
What's wrong? Please advice.
Change CONVERT(DATETIME to CONVERT(VARCHAR(10)
For MM/DD/YYYY format (USA):
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]
Gets you '02/12/2016'
For MMDDYYYY:
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]
Gets you '02122016'

Out of range when converting data SQL Server

I have a varchar column with string like date dd.mm.yyyy and I need it to convert to same string only with different format like yyyy.mm.ddT00:00:000
SOLVED: 1st Test:
SELECT REPLACE(CONVERT(varchar, CAST(table.DATE AS datetime), 126),'-','.') AS date
FROM tabletest.DBO.TABLE
Result:
I get 5 values (from more than 1000) and msg:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
SOLVED: 2nd Test:
SELECT REPLACE(CONVERT(varchar, (RIGHT(GREMPG1.DAT,4)+SUBSTRING(GREMPG1.DAT,3,4)+LEFT(GREMPG1.DAT, 2) + ), 126),'-','.') AS Datums
FROM tabletest.DBO.GREMPG1
Cannot find solution on how to get second part to this.
Maybe there is some shortest way yo convert columns record format?
Thanks.
Edited:
For second query:
Old value: 01.03.2014
New value: 2014.03.01
Need to get: 2014.03.01T00:00:00
For your first test try this way -
SELECT REPLACE(CONVERT(varchar, CONVERT(datetime,table.DATE,103), 126),'-','.') AS date FROM tabletest.DBO.TABLE
For your 2nd try -
SELECT REPLACE(CONVERT(varchar, CAST((RIGHT(GREMPG1.DAT,4)+SUBSTRING(GREMPG1.DAT,3,4)+LEFT(GREMPG1.DAT, 2)) AS DATETIME), 126),'-','.') AS Datums FROM tabletest.DBO.GREMPG1
Use this:
SELECT REPLACE(CONVERT(varchar, CONVERT(datetime,'15.02.2012',104), 126),'-','.') AS date
You get confision with the date when you cast. 15.02.2012 is calculated as mm.dd.yyyy and because there is not month 15 you get error.
So you query will look like:
SELECT
REPLACE(CONVERT(varchar, CONVERT(datetime,table.DATE,104), 126),'-','.') AS date
FROM
tabletest.DBO.TABLE;
Your SELECT seems to work. Try this:
declare #d VARCHAR(100)='13.12.2015';
SELECT REPLACE(CONVERT(varchar, CAST(#d AS datetime), 126),'-','.') AS date
/*
Result: 2015.12.13T00:00:00
*/
If it does NOT work, I'd assume a culture issue (date formats are handled quite differently according to your system's language and country).
If it works, I'd assume at least one value in your db being bad...

Convert varchar dd/mm/yyyy to dd/mm/yyyy datetime

I'm trying to convert a date in a varchar column in the dd/mm/yyyy format into the datetime dd/mm/yyyy format, so then I can run date range queries on the data.
So far I have the following which is not working
CONVERT(varchar, CAST(date_started AS datetime), 103)
I have also tried
convert(date_started as datetime, 103)
I need to make sure the output is as dd/mm/yyyy as we're in the UK and not the mm/dd/yyyy format
I think you are after this:
CONVERT(datetime, date_as_string, 103)
Notice, that datetime hasn't any format. You think about its presentation. To get the data of datetime in an appropriate format you can use
CONVERT(varchar, date_as_datetime, 103)
Try this code:
CONVERT(varchar(15), date_started, 103)
I think that more accurate is this syntax:
SELECT CONVERT(CHAR(10), GETDATE(), 103)
I add SELECT and GETDATE() for instant testing purposes :)
If you want to return a format mm/dd/yyyy, then use 101 instead of 103:
CONVERT(VARCHAR(10), [MyDate], 101)
You can do like this:
SELECT convert(datetime, convert(date, '27-09-2013', 103), 103)

Convert a SQL Server datetime to a shorter date format

I have a datetime column in SQL Server that gives me data like this 10/27/2010 12:57:49 pm and I want to query this column but just have SQL Server return the day month and year - eg. 2010 10 27 or something like that.
What are the functions I should be researching?
Should I be trying to convert to another date data type? Or simply convert it to a string?
Have a look at CONVERT. The 3rd parameter is the date time style you want to convert to.
e.g.
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) -- dd/MM/yyyy format
Try this:
print cast(getdate() as date )
If you need the result in a date format you can use:
Select Convert(DateTime, Convert(VarChar, GetDate(), 101))
In addition to CAST and CONVERT, if you are using Sql Server 2008, you can convert to a date type (or use that type to start with), and then optionally convert again to a varchar:
declare #myDate date
set #myDate = getdate()
print cast(#myDate as varchar(10))
output:
2012-01-17
If you have a datetime field that gives the results like this 2018-03-30 08:43:28.177
Proposed: and you want to change the datetime to date to appear like 2018-03-30
cast(YourDateField as Date)
With SQL Server 2005, I would use this:
select replace(convert(char(10),getdate(),102),'.',' ')
Results: 2015 03 05
The shortest date format of mm/dd/yy can be obtained with:
Select Convert(varchar(8),getdate(),1)
Just add date keyword.
E.g. select date(orderdate),count(1) from orders where orderdate > '2014-10-01' group by date(orderdate);
orderdate is in date time.
This query will show the orders for that date rather than datetime.
Date keyword applied on a datetime column will change it to short date.
For any versions of SQL Server: dateadd(dd, datediff(dd, 0, getdate()), 0)
The original DateTime field : [_Date_Time]
The converted to Shortdate : 'Short_Date'
CONVERT(date, [_Date_Time]) AS 'Short_Date'

Resources