Converting GETDATE() to Hijri date to yyyymmdd - sql

I am trying to get the Hijri GETDATE() and convert it into this format yyyymmdd
I have already tried this script
SELECT CONVERT(VARCHAR(10), GETDATE(), 131)
but it gives me this format ( 16/06/1438 ) and what I actually need is (1438/06/16)

SQL Server does not offer a wealth of formatting options for such dates, so just construct it yourself:
SELECT (RIGHT(CONVERT(VARCHAR(10), GETDATE(), 131), 4) + '/' +
CONVERT(VARCHAR(5), GETDATE(), 131)
) as hj_yyyymmdd
Oops. Right idea, wrong implementation:
SELECT (RIGHT(CONVERT(VARCHAR(10), GETDATE(), 131), 4) +
SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 131), 3, 4) +
LEFT(CONVERT(VARCHAR(10), GETDATE(), 131), 2)
) AS hj_yyyymmdd

Use below query :
SELECT FORMAT ( GETDATE(), 'yyyy/MM/dd', 'ar-SA' )

Another option is to convert to varchar then date then varchar again.
Using format() with 'ar-SA' seems to return 1 day earlier than using convert() with style 131.
select Method='multiconvert'
,conversion = convert(varchar(10)
,convert(date,convert(varchar(12),getdate(),131),103),112)
union all
select 'format'
, format ( getdate(), 'yyyyMMdd', 'ar-SA' )
union all
select 'style131'
,convert(varchar(12),getdate(),131)
rextester demo: http://rextester.com/LIX82417
returns
+--------------+--------------+
| Method | conversion |
+--------------+--------------+
| multiconvert | 14380616 |
| format | 14380615 |
| style131 | 16/06/1438 |
+--------------+--------------+

If you are having SQL Server 2012 and above,
SELECT FORMAT(GETDATE()+1,'yyyy/MM/dd','ar')
It will give you the below result for the date 2017/03/14
1438/06/16

Try with 120:
SELECT CONVERT(VARCHAR(10), GETDATE(), 120)

You can do that using the following query:
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120),'-','/')

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)

Convert varchar MMDDYYYY to MM/DD/YYYY datetime and select the most recent date only

Okay, so I have kind of a weird issue... the dates in the table have been entered in as string values MMDDYYYY and I'm trying to have the displayed as MM/DD/YYYY in a report and only select the most recent date pertaining to an ID, because some ID's may have multiple dates.
Example of my table:
ID | MyDate |
------+----------+
1 | 01302014 |
1 | 04222014 |
2 | 01302014 |
What I want to see when I select and insert into a temp table is this:
ID | MyDate |
------+-----------+
1 | 4/22/2014 |
2 | 1/30/2014 |
I know that storing dates as string values is a poor practice especially when storing them as MMDDYYYY, but does anyone have a solution to this nightmare?
EDIT
I forgot to mention that some fields might be NULL. Not sure if that makes a difference or not, but I think it does if I try to flip the dates using Right, Left, Convert.
This question is for almost a year ago, nut probably someone can find it useful.
You need to CONVERT your string to DATE format and use a ROW_NUMBER function to window your result set.
Create table
DECLARE #tbl TABLE(Id INT, myDate VARCHAR(8))
Sample data
INSERT #tbl
SELECT 1 , '01302014' UNION ALL
SELECT 1 , '04222014' UNION ALL
SELECT 2 , '01302014'
Query
;WITH C AS(
SELECT ROW_NUMBER() OVER (PARTITION BY Id ORDER BY CONVERT(DATETIME, (SUBSTRING(myDate, 5, 4) + '.' + SUBSTRING(myDate, 1, 2) + '.' + SUBSTRING(myDate, 3, 2)), 101) DESC) AS Rn
,Id
,CAST(CONVERT(DATETIME, (SUBSTRING(myDate, 5, 4) + '.' + SUBSTRING(myDate, 1, 2) + '.' + SUBSTRING(myDate, 3, 2)), 101) AS DATE) AS myDate
FROM #tbl
)
SELECT Id, myDate
FROM C
WHERE Rn = 1
SQLFiddle Demo
Using a CONVERT like the following code snippet will work on any SQL Server regardless of language and/or locale configuration.
DECLARE #OldDate varchar(8);
SELECT #OldDate = '04252012';
SELECT CONVERT(datetime, substring(#OldDate,5,4) + '-' + substring(#OldDate,1,2) + '-' + substring(#OldDate,3,2) + 'T00:00:00')
You could try this:
convert(date,SUBSTRING (MyDate,1,2)+'/'+SUBSTRING (MyDate,3,2)+'/'+SUBSTRING (MyDate,5,4),101)
Select CONVERT(datetime,RIGHT('01302014',4) +
LEFT('01302014',2) +
SUBSTRING('01302014',3,2))
'2014-01-30 00:00:00.000'
;with Cte as (Select Id,CONVERT(datetime,RIGHT(MyDate,4)+LEFT(MyDate,2)+SUBSTRING(MyDate,3,2)) as sDate, C=ROW_NUMBER()
over(PARTITION By Id Order by MyDate desc)
From #Temp)
select *
from Cte
where C=1
First do a conversion to a datetime datatype, then convert it to a format you wish to:
select id, convert(varchar, max(convert(datetime,right(mydate, 4)+left(mydate,4))), 101)
from #t
group by id
Although I can't understand why would it not suffice to just convert it do datetime and leave the formatting where it belongs, to the client.
Select id,convert(varchar(11),cast(dateValue as Date),101)
From
(
Select id,MAX(cast(MyDate As Date)) as dateValue
From tableName
Group By id
) t
The simplest solution is mySQL str_to_date() function.
STR_TO_DATE(`MyDate`, '%m%d%Y')
This will convert it to a DATETIME which you then format as required
DATE_FORMAT(STR_TO_DATE(`MyDate`, '%m%d%Y'), '%c/%e/%Y')
Complete Query:
Select ID, Case When IsDate(RIGHT(MyDate,4)+LEFT(MyDate,2)+SUBSTRING(MyDate,3,2)) = 1
THEN Convert(varchar(10), Convert(datetime, RIGHT(MyDate,4)+LEFT(MyDate,2)+SUBSTRING(MyDate,3,2)), 101)
ELSE Null END AS MyDate FROM YourTable a where Case When IsDate(RIGHT(MyDate,4)+LEFT(MyDate,2)+SUBSTRING(MyDate,3,2)) = 1
THEN Convert(varchar(10), Convert(datetime, RIGHT(MyDate,4)+LEFT(MyDate,2)+SUBSTRING(MyDate,3,2)), 101)
ELSE Null END = (Select Max(Case When IsDate(RIGHT(MyDate,4)+LEFT(MyDate,2)+SUBSTRING(MyDate,3,2)) = 1
THEN Convert(varchar(10), Convert(datetime, RIGHT(MyDate,4)+LEFT(MyDate,2)+SUBSTRING(MyDate,3,2)), 101)
ELSE Null END)
FROM YourTable b
Where a.ID = b.ID
)
This is a modified version from Dimitris Kalaitzis' answer.
First, you will need to convert your MyDate into String. The data for example (01302014) is not treated as a string and the leading zero will be removed when you convert it. Therefore, use CAST to make MyDate as a String and add a leading 0 to it. Then you find the right 8 characters so to get rid of the leading zero for months from Oct to Dec.
Here is the code that should work for you:
CONVERT(Date, SUBSTRING(RIGHT('0' + CAST(MyDate AS VARCHAR(10)), 8), 1, 2) + '/' + SUBSTRING(RIGHT('0' + CAST(MyDate AS VARCHAR(10)), 8), 3, 2) + '/' + SUBSTRING(RIGHT('0' + CAST(MyDate AS VARCHAR(10)), 8), 5, 4), 101)
Hope this helps.
You first have to add the slashes to be able to convert it to a date, use STUFF
Then convert it to a DATE and select the MAX group by ID
Query:
SELECT ID, FORMAT(MAX(CONVERT(DATE,STUFF(STUFF(MyDate,3,0,'/'),6,0,'/'))),'MM/dd/yyyy') AS MyDate FROM TableName
GROUP BY ID

Select * from table where date selected is between

i trying to build the following query to select * from table where the minDate is 03-02-2014 and the maxDate is 01-03-2014
but something i missing.
hope that someone can help me with this.
SELECT * From table Where
SUBSTRING(mydate, 1, 10) >= REPLACE('03-02-2014','-','/') AND
SUBSTRING(mydate, 1, 10) <= REPLACE('01-03-2014','-','/')
Note:
My Date column is of type varchar with a value like this --> 03/02/2014 18:13:16
im working in sql server management studio (t-sql)
From your comments, it seems that the mydate column of your table is in the British format.
Read this article about date conversion in SQL SERVER to understand more about date conversions.
Also updated my answer with the date conversions for this format.
Try something like
SELECT * FROM table
WHERE CONVERT(DATE, SUBSTRING(mydate, 1, 10), 103) >= CONVERT(DATE, '03/02/2014', 103)
AND CONVERT(DATE, SUBSTRING(mydate, 1, 10), 103) <= CONVERT(DATE, '01/03/2014', 103)
you can do something like:
Select * From Table
Where CONVERT( Datetime, mydate ,110 ) between CONVERT( Datetime, #min ,110 ) and between CONVERT( Datetime, #max ,110 )
I don't have SSMS available right now, but for a quick try you might try this
SELECT * From table Where
CAST( SUBSTRING(mydate, 1, 10) as date) BETWEEN CAST( SUBSTRING('START DATE', 1, 10) as date)
AND CAST( SUBSTRING('END DATE', 1, 10) as date)

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

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)