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
Related
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');
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 have searched but i havent get the format like 21-Sep-12 date format in sql.I get every other format but i didnt get this type of Format.so,How can i get the format like this in Date?
You should rather do teh formatting on the UI/Report side.
Why do you need in in this format?
But if you must, try
SELECT REPLACE(CONVERT(VARCHAR(9), GETDATE(), 6), ' ', '-') AS [DD-Mon-YY]
SQL Fiddle DEMO
Here is a nice list of formats
SELECT REPLACE(CONVERT(VARCHAR(9), GETDATE(), 6), ' ', '-') AS [DD-Mon-YY]
Declare #dt datetime=getdate()
select REPLACE(convert(varchar(20),#dt,6),' ','-')
There is a field which is date type in a table. The format of this field is mm/dd/yy. Is there any way to convert it to dd-Month name-yy?
Best Regards,
Without any hassle, you can use CONVERT to get "dd MONTHNAME yyyy" format:
SELECT CONVERT(VARCHAR, GETDATE(), 106)
e.g. "25 Jan 2010"
If you want your exact format, you may need a bit of manual fiddling around like:
SELECT CAST(DAY(GETDATE()) AS VARCHAR) + '-' + LEFT(DATENAME(mm, GETDATE()), 3) + '-' + RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR), 2)
e.g. "25-Jan-10"
Update:
In fact, a shorter way to achieving this is:
SELECT REPLACE(CONVERT(VARCHAR, GETDATE(), 6), ' ', '-')
e.g. "25-Jan-10"
Take a look at the CAST and CONVERT functions:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
example:
select convert( char(8), getdate(), 5 )
output:
dd-mm-yy
Source: compuspec date format conversion
you can try convert() ?
Maybe you need to differentiate between storing datetime values in SQL Server and displaying them. Here is a great article that explains what's going on behind the scenes: The ultimate guide to the datetime datatypes
I can not convert field run_date on sysjobhistory table from yyyymmdd to mmddyyyy.
select CONVERT(varchar(10),run_date),101) as Date from sysjobhistory
Please correct me. Thanks.
declare #dt date ='20161025'
select #dt,CONVERT(varchar(15),#dt,101)
See here
Date type does not have yyyymmdd nor mmddyyyy. This type only valid for string (as varchar, nvarchar). Plus, you have extra ) after rundate.
The correct one should be:
select CONVERT(varchar(10),run_date,101) from sysjobhistory
where 101 is style argument: https://msdn.microsoft.com/en-us/library/ms187928.aspx
shouldn't it be: CONVERT (varchar(10), run_date, 101). What I am trying to say is that you have an extra parenthesis after run_date.