I have the date in this format 2017-02-03 and I want that my date should be in this format 01-Mar-2016 while making select command.
I am using SQL Server 2012
Check this website. This will be helpful.
select replace(convert(varchar, getdate(), 106),' ','-')
What is done here is: First convert it into '01 Mar 2016' and then replace white spaces(' ') with '-'. Which will give desired output as '01-Mar-2016'
Use SQL Server Format function where you can specify .NET format string
For instance for current date
SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy');
Also you can specify culture here if needed
SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy', 'en-US');
If you dates are stored as string you'd better fix this but you can just CAST or CONVERT them instead. I.e.
SELECT FORMAT(CAST('2017-10-11' AS date), 'dd-MMM-yyyy');
Related
In my aplication I use a DateEdit control and show the date using this format dd/MM/yyyy so the date is save correctly when I use mssql-server 2014 (spanish) but whe I use mssql-server 2008 (english) the date is stored with this fomat MM/dd/yyyy so what I need to know is if there is a way to store the date with the format dd/MM/yyyy no matter the versiĆ³n of mssql-server I use or the lenguage I use?
EDIT: to explain better this is the problem, this text 03/07/2017 is stored in the sqlserver 2014 as july 3 of 2017 and in the sqlserver 2008 is stored as march 7 of 2017.
Convert.ToDateTime(String).ToString("dd-MM-yyyy")
Check the defualt language on your MS SQL Server 2008, and if its possible, you can change to Spanish: How to change Date Format after installing SQL Server
Also, you can format your query output with the CONVERT function (#ZLK post the link) using the format 103: CONVERT (DATETIME,[YOURDATE],103)
Following solution has two options. You can use one as per your requirements.
DECLARE #Date AS TABLE(val VARCHAR(30));
INSERT INTO #Date VALUES('12/04/2016');
INSERT INTO #Date VALUES('10/01/2015');
INSERT INTO #Date VALUES('17/03/2017');
--Option 1
SELECT TRY_PARSE(val AS DATE USING 'en-gb') 'Option 1' FROM #Date;
--Option 2
SELECT CONVERT(VARCHAR(10),TRY_PARSE(val AS DATE USING 'en-gb'),103) 'Option 2' FROM #Date;
And the outputs are:
As others said: SQL doe not store dates as we humans do. But thats another matter.
I think what you need is to inform SQL the format your date has.
The best option is to use ISO 8601 format: yyyy-mm-ddThh:mi:ss.mmm.
So, if before saving the date you just format it with this format, SQL (no matter the version or the language) will recognize it as begining with a year, followed by a month and so on. The key of this subject is using first 4 digits.
This way, 2017-01-03 will always be January 03, 2017 and never March 01, 2017. Hope this help you.
Using this you can change the date Format
select FORMAT(GETDATE(), 'dd/MMM/yyyy', 'en-us')
I have a date in table as "26052016" in format DDMMYYYY
I want to convert this date to "YYYYMMDD" format.
Any idea
I have tried this method
select CONVERT(varchar(8),[doc-date],112) FROM C034_PDK_ParallelBillingSourceExtract
But this is gives me the same date as a result.
Please help me
I can find this way, i don't know if any other way exist or not..
declare #date nvarchar(max)='01052016'
select convert(varchar(8),cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date),112)as [YYYYMMDD]
Clear Code:
declare #date nvarchar(max)='01052016'
declare #date1 date
set #date1 =cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date)
select convert(varchar(8),#date1,112)as [YYYYMMDD]
If you are using Sql version< 2012 then you need to skip CONCAT and use + for string concatination.
SELECT CONVERT(VARCHAR(8), doc-date, 112) AS [YYYYMMDD] from
C034_PDK_ParallelBillingSourceExtract
Check ... this should work correctly.
Thanks
SELECT CONVERT(VARCHAR(8), '26052016', 112) AS [YYYYMMDD] from
C034_PDK_ParallelBillingSourceExtract
Try like this,
SELECT substring([doc-date], 5, 4) + substring([doc-date], 3, 2) + substring([doc-date], 1, 2) AS [YYYYMMDD]
FROM C034_PDK_ParallelBillingSourceExtract
There are differet ways to do it.
The best way is to use substring method as you know the character positions are going to remain same.
For Example
Suppose your date is - 31122015
Pick the portions of date using substring method and concatenate them
select SUBSTRING('31122015',5,4) + SUBSTRING('31122015',3,2) + SUBSTRING('31122015',1,2)
The result would be - 20153112
Since SQL Server 2016 we have a couple of handy tools for this:
Use DATEFROMPARTS and SUBSTRING to convert odd date formats from any arrangement within a Varchar to an actual date:
SELECT DATEFROMPARTS(SUBSTRING('31122015',5,4), SUBSTRING('31122015',3,2), SUBSTRING('31122015',1,2))
Use FORMAT to Convert an actual date to YYYYMMDD:
SELECT FORMAT(MyDate, 'yyyyMMdd')
watch out for the yyyyMMdd, that's the only part of MS SQL that is case-sensitive. Lower case mm is "minutes" and upper case MM is "Month", upper case YYYY or DD is nothing, and will just add letters to your output!
I'm using following line of code in SQL:
REPLACE(CONVERT(VARCHAR(11),getdate(),106), ' ','-')
to convert given date to MMM-DD-YYYY format, this code is working perfectly fine if current date is more that 10th.
But I'm getting error when its tries to convert single digit date, e.g when I'm converting date 9 jan 2014. It is converted to Jan--9-2014.
Which is actually wrong as its contain one extra -.
Please help me to resolve this issue.
CONVERT DATE IN dd-mmm-yyyy FORMAT (Eg. 01-Jun-2018)
REPLACE(CONVERT(CHAR(11), DOB, 106),' ','-') AS DOB
As of SQL Server 2012+, you can use FORMAT(value, format [, culture ])
Where the format param takes any valid standard format string or custom formatting string
Example:
SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy')
Further Reading:
How to convert DateTime to VarChar
How to convert date to a format mm/dd/yyyy
sql server convert date to string MM/DD/YYYY
Try This.
select REPLACE(REPLACE(CONVERT(VARCHAR,getdate(),107), ' ','-'), ',','')
select REPLACE(replace(cast(getDate() as varchar(11)), ' ', ' '), ' ','-')
returns correct result Jan-9-2014 on your fiddle
here is the fiddle
How about this:
select REPLACE(REPLACE(REPLACE(cast(getDate() as varchar(11)),' ','{}'),'}{',''),'{}','-')
Output: Jan-9-2014
SQL Fiddle Demo
What about
select left(DATEname(m,getdate()),3)+'-'+
cast(DAY(getdate()) as varchar)+'-'+
CAST(year(getdate()) as varchar)
?
Try this
SELECT CONVERT(varchar(11),getdate(),103)
see this link for more details
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'
Its SQL Server 2000.
I am starting with a character string in the format DD/MM/YYYY
Here's the table: http://msdn.microsoft.com/en-us/library/ms187928.aspx
Sample:
select convert(datetime,'22/03/2005', 103)
SET DATEFORMAT dmy
SELECT CAST('22/03/2005' AS datetime)
or
SELECT convert(datetime,'22/03/2005', 103)
It depends your the context.
SQL Server understands '2010-06-21' as a date without requiring any convert/cast, so I would just use the string in the format 'yyyy-mm-dd' if that suits your needs.
Otherwise, the other responses using cast may be better if you need to compare with date fields containing hours as well.