How to get year of character hijri date SQL Server - sql

I searched and tried many examples unable to solve my hijri date is like,
19/07/1440
I tried this query
SELECT TOP 200
DATEPART(YEAR, EndDateHejri)
FROM
student
but I'm getting this error
Conversion failed when converting date and/or time from character string
I'm unable to solve error - hoping for your suggestions

I bit of Google-Fu and format 131 should help you convert Hijri dates into Gregorian Dates...
DECLARE #hijri DATETIME = CONVERT(datetime, ' 7/05/1421 12:14:35:727PM', 131)
SELECT #hijri
Unfortunately, all the date functions (DATEPART(), DATENAME(), even DATEADD(), etc) are all based on manipulating Gregorian dates. So you can't use them.
So, you're forced to use string manipulation.
DECLARE #hijri DATETIME = CONVERT(datetime, ' 7/05/1421 12:14:35:727PM', 131)
SELECT #hijri
SELECT DATEPART(year, #hijri)
-- Gives 2000 :(
SELECT RIGHT(CONVERT(VARCHAR(10), #hijri, 131), 4)
-- Gives 1421 :)
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=901209ae0cdcf38cdcdea8afad4fd034

Posting a different answer. As the OP is only after the year part, and they've stated that it's always in the format 00/00/yyyy why not just use RIGHT? So:
SELECT RIGHT(EndDateHejri,4) as HejriYear;

I tried answer #Vishnu Chandel it's working for me .
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103))
And full code is :
SELECT TOP 200
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103)) as year
FROM
student

Please try below code to get the correct output.
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103));

Related

Calculate Time Difference for Date/Time Variable

I have two variables 'triage_date_time' and 'checkin_date_time'. Both are formatted as, for example, 2018-12-31 14:13:00. Showing the year-month-day and hour-minute-second both within one cell.
I wanted to create a variable that calculates the time it takes from check-in to triage.
I attempted to use the following code:
SELECT DISTINCT datediff(minute, 'triage_date_time', 'checkin_date_time') as
checkin_to_triage
However, when running this code I get the following error... "Conversion failed when converting date and/or time from character string".
Any suggestions of how I can write a code that would calculate the minute difference of these two variables.
Thanks!
One problem is obviously the single quotes. Assuming that you are using SQL Server, variables start with #. So:
select datediff(minute, #triage_date_time, #checkin_date_time) as checkin_to_triage
If you are confused and really mean columns in a table, then:
select datediff(minute, triage_date_time, checkin_date_time) as checkin_to_triage
from t;
could it be that your field is a CHARACTER data type ?
cast your char to datetime
SELECT DISTINCT datediff(minute, CAST(triage_date_time AS datetime), CAST(checkin_date_time AS datetime)) as checkin_to_triage
Try with this query
DECLARE #triage_date_time DATETIME = '20181231 14:13:00'
DECLARE #checkin_date_time DATETIME = '20181231 16:13:00'
SELECT DATEDIFF (MINUTE, #triage_date_time, #checkin_date_time) AS 'checkin_to_triage'
Output :
checkin_to_triage
120

Convert time in SQL to 12 hour format WITH seconds on

I have a script that I am using to populate a time dimension table and I would like to have a column for the time in 12 hour format.
I know this can be done by doing something along the lines of
SELECT CONVERT(varchar(15),[FullTime],100)
Where [FullTime] is a column containing a TIME field in HH:MM:SS format.
But this gives the following result 2:30pm and I would like 2:30:47PM, note the inclusion of seconds.
I know I could build this up using substrings etc. but I wondered if there was a prettier way of doing it.
Thanks
SELECT GETDATE() 'Today',
CONVERT(VARCHAR(8), [FullTime], 108) 'hh:mi:ss'
Taken from here
This will give you a column 'today' followed by the time value you seek 'hh:mi:ss'
If having also milliseconds is not a problem for you, you can use
SELECT CONVERT(VARCHAR, [FullTime], 109)
Declare #tstTime datetime
set #tstTime = GetDate()
Select
#tstTime
,CONCAT(SUBSTRING(CONVERT(varchar(26), #tstTime, 109),1,20),RIGHT(CONVERT(varchar(26), #tstTime, 109),2))

Date format dd/mm in SQL Server

I use SQL Server and I need to display a datetime data type in the following format:
dd/mm
day-month without the year, which is the most effective way?
Use 103 style in convert function and remove the year
SELECT LEFT(CONVERT(VARCHAR(15), Getdate(), 103), 5) --11/03
You'll find this site really helpful I think:
http://www.sql-server-helper.com/tips/date-formats.aspx
From that link, you can see this as a quick way to get DD/MM:
SELECT CONVERT(VARCHAR(5), GETDATE(), 3) AS [DD/MM]

how to remove time from datetime

The field DATE in the database has the following format:
2012-11-12 00:00:00
I would like to remove the time from the date and return the date like this:
11/12/2012
First thing's first, if your dates are in varchar format change that, store dates as dates it will save you a lot of headaches and it is something that is best done sooner rather than later. The problem will only get worse.
Secondly, once you have a date DO NOT convert the date to a varchar! Keep it in date format and use formatting on the application side to get the required date format.
There are various methods to do this depending on your DBMS:
SQL-Server 2008 and later:
SELECT CAST(CURRENT_TIMESTAMP AS DATE)
SQL-Server 2005 and Earlier
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SQLite
SELECT DATE(NOW())
Oracle
SELECT TRUNC(CURRENT_TIMESTAMP)
Postgresql
SELECT CURRENT_TIMESTAMP::DATE
If you need to use culture specific formatting in your report you can either explicitly state the format of the receiving text box (e.g. dd/MM/yyyy), or you can set the language so that it shows the relevant date format for that language.
Either way this is much better handled outside of SQL as converting to varchar within SQL will impact any sorting you may do in your report.
If you cannot/will not change the datatype to DATETIME, then still convert it to a date within SQL (e.g. CONVERT(DATETIME, yourField)) before sending to report services and handle it as described above.
just use, (in TSQL)
SELECT convert(varchar, columnName, 101)
in MySQL
SELECT DATE_FORMAT(columnName, '%m/%d/%Y')
I found this method to be quite useful. However it will convert your date/time format to just date but never the less it does the job for what I need it for. (I just needed to display the date on a report, the time was irrelevant).
CAST(start_date AS DATE)
UPDATE
(Bear in mind I'm a trainee ;))
I figured an easier way to do this IF YOU'RE USING SSRS.
It's easier to actually change the textbox properties where the field is located in the report. Right click field>Number>Date and select the appropriate format!
SELECT DATE('2012-11-12 00:00:00');
returns
2012-11-12
Personally, I'd return the full, native datetime value and format this in the client code.
That way, you can use the user's locale setting to give the correct meaning to that user.
"11/12" is ambiguous. Is it:
12th November
11th December
For more info refer this: SQL Server Date Formats
[MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 101) from tbl
[DD/MM/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 103) from tbl
Live Demo
TSQL
SELECT CONVERT(DATE, GETDATE()) // 2019-09-19
SELECT CAST(GETDATE() AS DATE) // 2019-09-19
SELECT CONVERT(VARCHAR, GETDATE(), 23) // 2019-09-19
In mysql at least, you can use DATE(theDate).
You may try the following:
SELECT CONVERT(VARCHAR(10),yourdate,101);
or this:
select cast(floor(cast(urdate as float)) as datetime);
Use this SQL:
SELECT DATE_FORMAT(date_column_here,'%d/%m/%Y') FROM table_name;

Oracle to SQL server Date conversion

I would like to convert an Oracle SQL query into SQL server query.
But I encountered a problem with the following line :
AND to_date(to_char(M_DATE,'DD-MM-YYYY')) = '27/01/12'
M_DATE : DATE NOT NULL
I use
to_char(DATE,'DD-MM-YYYY')
in order to get their data like that : DD-MM-YYYY 00:00:00.000 (data are stocked like : 25/02/12 15:32:06.578)
So I searched on the Internet, but I didn't find any available solution. But I'm not an experienced SQL user, so if anybody know the solution..
Thanks
In general when removing any time values from a date I would use Date functions rather than converting to string
DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE()))
instead of
CONVERT(VARCHAR, GETDATE(), 103)
Although the end result is the same you are maintaining date format and while I have no specific results sets to prove it conclusively I have found this to be much quicker when dealing with large quantities of data.
In Oracle, I would remove the time element of a datetime using trunc - like so:
AND trunc(M_DATE) = ...
In SQLServer, I would convert to a date - like so:
AND convert(date,M_DATE) = ...
SELECT CONVERT(VARCHAR(25), GETDATE(), 131)
You could just do:
AND convert(varchar(8), M_DATE, 3) = '27/01/12'
Of course, that won't work if you have dates from other centuries.
I'm not sure what you mean by "data are stocked like"; be aware that the Microsoft SQL Server DATE type only has a precision of one day. If you want to have the time as well as the day, you should use the DATETIME2 type