Varchar date conversion in SQL - sql

My date field has value format to be : Feb 15 2019. Is there a way to convert this to MMDDYYYY format?
Desired output: 02152019
Query I tried: SELECT convert(varchar, getdate(), 112) - this query is showing YYYYMMDD format :(
Any help?

select format( convert(date, 'Feb 15 2019'), 'MMddyyyy')
-- results to: 02152019
-- But again, your application is to take care about format!!!

Try this query,
select replace(convert(varchar, getdate(),101),'/','')

You can try this
Select convert(varchar(12), convert(date, 'Feb 15 2019'), 112)
or
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]
This will give output - 20190215.
You can pass different value like 101. For different output see Here.

Related

Getting Month and Day from a date

It is possible simplify this query (MSSQL)?
SELECT RIGHT(STR(MONTH('2014-05-02'))+100,2) + '-'
+ RIGHT(STR(DAY('2014-05-02'))+100,2)
I want month and day from a date but always with leading zeros if only have one digit in the month / day. In this date 2014-05-02 I want only 05-02. I can do that with this query but I don't know if there is a simply way...
SELECT CONVERT(CHAR(5), GETDATE(), 10)
Result:
05-23
Please try:
select RIGHT(CONVERT(nvarchar(10), GETDATE(), 126), 5)
For the example,
select RIGHT(CONVERT(nvarchar(10),CONVERT(DATETIME, '2014-5-2'), 126), 5)
Try this...
SELECT LEFT(CONVERT(VARCHAR(10), GETDATE(), 110),5)
OR
SELECT LEFT(CONVERT(VARCHAR(8), GETDATE(), 10),5)
Both return the same result as follows
RESULT: 05-23
I like using format.
If you have a string:
select format(convert(date, '2014-05-23', 126),'MM-dd');
If you have a date:
select format(GetDate(),'MM-dd');
This could really Help you.
DECLARE #A DATETIME = GETDATE()
SELECT CONVERT(VARCHAR(5),#A,110)

Converting dd-mm-yyyy string to date

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.

SQL SERVER DATETIME FORMAT

Studying SQL Server there is something I am not sure of:
A datetime field with the value:
2012-02-26 09:34:00.000
If I select out of the table using:
CAST(dob2 AS VARCHAR(12) ) AS d1
It formats it as:
Feb 26 2012
What I am unsure of his how or why SQL Server formats DateTime like that. If you use datetime2 it does not - anyone know why?
The default date format depends on the language setting for the database server. You can also change it per session, like:
set language french
select cast(getdate() as varchar(50))
-->
févr 8 2013 9:45AM
try this:
select convert(varchar, dob2, 101)
select convert(varchar, dob2, 102)
select convert(varchar, dob2, 103)
select convert(varchar, dob2, 104)
select convert(varchar, dob2, 105)
select convert(varchar, dob2, 106)
select convert(varchar, dob2, 107)
select convert(varchar, dob2, 108)
select convert(varchar, dob2, 109)
select convert(varchar, dob2, 110)
select convert(varchar, dob2, 111)
select convert(varchar, dob2, 112)
select convert(varchar, dob2, 113)
refernces: http://msdn.microsoft.com/en-us/library/ms187928.aspx
http://www.w3schools.com/sql/func_convert.asp
Compatibility Supports Says that
Under compatibility level 110, the default style for CAST and CONVERT operations on time and datetime2 data types is always 121. If your query relies on the old behavior, use a compatibility level less than 110, or explicitly specify the 0 style in the affected query.
That means by default datetime2 is CAST as varchar to 121 format. For ex; col1 and col2 formats (below) are same (other than the 0s at the end)
SELECT CONVERT(varchar, GETDATE(), 121) col1,
CAST(convert(datetime2,GETDATE()) as varchar) col2,
CAST(GETDATE() as varchar) col3
SQL FIDDLE DEMO
--Results
COL1 | COL2 | COL3
2013-02-08 09:53:56.223 | 2013-02-08 09:53:56.2230000 | Feb 8 2013 9:53AM
FYI, if you use CONVERT instead of CAST you can use a third parameter to specify certain formats as listed here on MSDN
In MS SQL Server you can do:
SET DATEFORMAT ymd
case when isdate(inputdate) = 1
then convert(datetime, cast(inputdate,datetime2), 103)
else
case when isdate(inputdate) = 0
then convert(datetime, cast(inputdate,datetime2), 103)
This is my favorite use of 112 and 114
select (convert(varchar, getdate(), 112)+ replace(convert(varchar, getdate(), 114),':','')) as 'Getdate()
112 + 114 or YYYYMMDDHHMMSSMSS'
Result:
Getdate() 112 + 114 or YYYYMMDDHHMMSSMSS
20171016083349100
to change the date format by using sql syntax you should use this query
SELECT DATE_FORMAT(`<columnName>`, '%d/%m/%Y') FROM schemaname.tablename;
ex:-
for suppose i have a schema named as bugloo and the table name is tbl_company
and in this tbl_company i have a column all are in the date format %yy/%mm/%dd and column name is createdDate and the query should like this
SELECT DATE_FORMAT(`createdDate`, '%d/%m/%Y') FROM bugloo.tbl_company;
after running this query my output date would be converted to %dd/%mm/%yyyy

Date format date-month-year

I have a table which has a datetime column. I want to show date in date-month-year forma.I am using SQL Server 2008.
You need to convert datetime column
select convert(varchar,datecolumn,103) from yourtable
Some datetime convertions:
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]
/* YYYY/MM/DD
2015/07/11 */
SELECT CONVERT(VARCHAR(10), GETDATE(), 112) AS [YYYYMMDD]
/* YYYYMMDD
20150711 */
-- SQL convert date string to datetime - time set to 00:00:00.000 or 12:00AM
PRINT CONVERT(datetime,'07-10-2012',110) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'2012/07/10',111) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'20120710', 112) -- Jul 10 2012
You can learn all DateTime convertion from here
FORMAT can be used for this;
SELECT FORMAT(GETDATE(), 'dd-MM-yyyy') --20-09-2021
in your case;
SELECT FORMAT(datetime, 'dd-MM-yyyy') AS datetime --20-09-2021
Edit;
Sorry to inform that I've just seen you mentioned SQL Server 2008 this code works for SQL Server 2012 for those who use 2012 and ends up in here!

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)