Converting dd-mm-yyyy string to date - sql

I am passing a date in string format 11-09-2013 in dd-mm-yyyy format from JSP. My query is:
select convert(varchar(10),'11-09-2013',106)
But it is not giving my expected output, which is:
11-SEP-2013

DECLARE #d CHAR(10) = '11-09-2013'; -- dd-mm-yyyy
SELECT UPPER(REPLACE(CONVERT(CHAR(11), CONVERT(DATETIME, #d, 103), 106), ' ', '-'));
-- if 2008+ you can use DATE instead:
SELECT UPPER(REPLACE(CONVERT(CHAR(11), CONVERT(DATE, #d, 103), 106), ' ', '-'));
-- if 2012 you can use:
SELECT UPPER(FORMAT(CONVERT(DATE, #d, 103), 'dd-MMM-yyyy'));
Result in all three cases:
11-SEP-2013
However, this can be very expensive to convert to strings etc. like this. Why do you need this output format, and where is it going? If this is being presented at the client you are much better off using the string formatting capabilities at the client.

Related

Convert MonthName-Year to Year-MonthNumber

I have a custom date string in SQL. I need to convert the date into custom format. Below is the example for it.
SQL Input : 'January-2019' (Month Name-Year format)
Output : '201901' (Year-Month Number format, Month Number must be two digit)
Since you're using SQL 2008 you can use CONVERT(DATE, ...) + CONVERT(VARCHAR, ...):
DECLARE #input AS VARCHAR(20) = 'January-2019'
SELECT CONVERT(VARCHAR(6), CONVERT(DATE, '01-' + #input), 112)
Note that you need to prepend 01- to the string.
You can also try this.
DECLARE #input AS VARCHAR(20) = 'January-2019'
Select Convert(Varchar(6), Cast(Replace(#input, '-', ' 01 ') as DATE), 112)
Live Demo

Date convert not working with style 103

I have saved my date as a nvarchar(50) datatype in SQL Server. When I run this query:
select [Client_code], Date_of_receipt
from [T_Receving]
I am getting output like this:
but I want to filter my records by particular date, so I wrote a query like this
select
convert(date, [Date_of_receipt], 103) as 'Date_of_Receipt'
from
[T_Receving]
where
convert(date, [Date_of_receipt], 103) between '2015-03-06' and '2018-05-06'
but its showing an error
Conversion failed when converting date and/or time from character string
You have to convert to datetime and then convert back to varchar
declare #dtv varchar(20) = '2018-17-04'
declare #dtvdt datetime = convert(datetime, #dtv, 103)
select #dtvdt;
select convert(varchar(20), convert(datetime, #dtv, 103), 103), #dtv
where convert(datetime, #dtv, 103) between '2015-03-06' and '2018-05-06'
You clearly have some bad data. Use try_convert():
select try_convert(DATE, Date_of_receipt, 103) as Date_of_Receipt
from T_Receving
where try_convert(DATE, Date_of_receipt, 103) between '2015-03-06' and '2018-05-06' ;
In SQL Server 2008, you have to work harder to find the culprits. You can start with:
select date_of_receipt
from T_Receving
where date_of_receipt not like '[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]'
This will find most instances of bad formats. If it still persists, you will have to dig deeper to find bad day or month numbers.

How do I convert text to datetime in SQL Server?

I want to convert text to datetime in SQL Server.
I have text which in the format dd/mm/yyyy, but I want to convert this text format to datetime yyyymmdd.
I am using this method.
Convert(varchar(8), Right(text, 4) + Substring(text, 4, 2) + Left(text, 2), 112)
BUT I need a simpler query.
Answer:
SELECT FORMAT(CONVERT(date, '30/01/2018', 103), 'yyyyMMdd')
Explanation:
So this one is a little trickier because you've got DD/MM/YYYY, so you need to CONVERT first:
SELECT CONVERT(date, '30/01/2018', 103)
Result: 2018-01-30
Then if you are using SQL Server 2012+ use FORMAT()
SELECT FORMAT(CONVERT(date, '30/01/2018', 103), 'yyyyMMdd')
Result: 20180130

SQL VarChar to Date

hi
i am trying to convert a VarChar date field (e.g. 20100320) to a real date field like
'dd/mm/yyyy' (e.g. 20/03/2010).
I have tried two ways:
a)
(SELECT MIN(CAST(A.DateOfAction AS Date)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
b)
(SELECT MIN(CONVERT(DATE, A.DateOfAction, 103)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
both producing the result like
yyyy-mm-dd (e.g. 2010-03-20)
but i want the result like
dd/mm/yyyy (e.g. 20/03/2010)
any help will be appreciated.
thanks.
Try this:
select convert(varchar(8), convert(datetime, min(a.DateOfAction), 112), 103)
Your problem is that once you have a date format, SQL Server will dump it out in its default date format, which you've discovered is yyyy-mm-dd. You need to convert from date to varchar to get the format you want. But to convert from date, you need to first convert to date! 112 is the format for yyyymmdd, and 103 is the format for dd/mm/yyyy, so this is why you need these formats. (Books Online reference for date formats)
Declare #date nvarchar(100)
set #date = '20100320'
select convert(varchar, CONVERT(datetime, #date, 109), 103)
You can use
convert(varchar, CONVERT(datetime, A.DateOfAction, 109), 103)

Convert SQL DateTime format

How can I display a DATETIME value (2010-12-02 15:20:17.000) as 02/12-2010 15:20?
For SQL Server:
select stuff(convert(varchar, getdate(), 105), 3, 1, '/') + ' ' + left(convert(varchar, getdate(), 8), 5)
DateTime is a DateTime is a DateTime - it just holds a date and time and doesn't have any string representation, really.
See the CAST and CONVERT topic in the SQL Server Books Online for details - it shows all supported date formats that SQL Server supports.
For your source format (2010-12-02 15:20:17.000) you could probably use style no. 121
DECLARE #source VARCHAR(50)
SET #source = '2010-12-02 15:20:17.000'
DECLARE #Date DATETIME
SELECT #Date = CONVERT(DATETIME, #source, 121)
SELECT #Date
but your target format is a bit odd..... I don't see any "out of the box" style that would match your needs. You'll need to use some string manipulation code to get that exact format.
Use MSSQL's build-in function to convert datetime to string with format,
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] --2/5/12
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] --5/2/2012
You need to create custom function to get various format to use like this;
SELECT dbo.ufn_FormatDateTime(GETDATE(),'YYYY-MM-DD HH:mm:SS tt')
--Output : 2012-02-05 01:58:38 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'(dddd) mmmm dd, yyyy hh:mm:ss.fff tt')
--Output : (Sunday) February 05, 2012 01:58:38.723 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'dd/MM/yyyy')
--Output : 05/02/2012
SELECT dbo.ufn_FormatDateTime(GETDATE(),'yyyy MMM, dd (ddd) hh:mm:ss tt')
-- Output : 2012 Feb, 05 (Sun) 01:58:38 AM
Get the code snippet from this link.
http://www.tainyan.com/codesnippets/entry-62/sql-server-date-time-format-function.html
http://msdn.microsoft.com/en-us/library/ms189491.aspx
Is this what you're looking for?
Assuming Oracle:
select TO_CHAR(SYSDATE, "dd/mm-yyyy HH24:mi")
from DUAL;
Assuming SQL Server:
select STR(DATEPART(DAY, GETDATE()), 2)
+ '/'
+ STR(DATEPART(MONTH, GETDATE()), 2)
+ '-'
+ STR(DATEPART(YEAR, GETDATE()), 4)
+ ' '
+ STR(DATEPART(HOUR, GETDATE()), 2)
+ ':'
+ STR(DATEPART(MINUTE, GETDATE()), 2);
Little example I use for Germany and Switzerland: dd.mm.yyyy hh:mm
SELECT CONVERT(varchar, GETDATE(), 104) + ' ' + LEFT(CONVERT(varchar, GETDATE(), 108), 5)