Use sql server Convert Function to convert hijri to gregorian date - sql-server

I have simple table on my sql server and in my table have a date field,and into date field save a hijri date,i want use the sql server convert function to convert hijri date to gregorian date.
how can i do this?
i use this query in sql server:
update k
set time=CONVERT(datetime,GETDATE(),101)
and i insert this "1392/4/21" in time field ,and when convert sql server return this "2014/11/5",when i use online date convertor,and insert this "2014/11/5",convert date is "1393/2/14" !!is this the correct resualt?

Here is a sample how you could do it:
select convert(datetime, value, 131)
from (
values ('13/01/1436 9:54:59:767AM')
) samples(value)
There are several blogs about this, likethis one:
http://blogs.msdn.com/b/wael/archive/2007/04/29/sql-server-hijri-hijra-dates.aspx
and this one:
http://raresql.wordpress.com/2013/05/08/sql-server-how-to-convert-gregorian-dates-to-hijri-date-with-formatting/

Related

Get date in date format from SQL Server

I have a table in SQL Server with a date column set to default date, however, when I fetch the data from the server using VBA I get the date column in string format. Is there any way I get the date as in date format?
Can you you advise how you are bringing in the data? EG from inbuilt connectors or via a recordset via VBA objects?
A quick hack which I wouldn't advise without really testing would be this :
Select DateDiff(DD, '1899-12-30', date_column) AS [column] from TABLE_x
What is this doing?
In Excel dates start 01-01-1900. When you change a date to 'General' you find it's actually a number with a format. The above date difference will give you the excel number version of the Date.
EG to get today 29/11/2019 which will be 43798 when cell format is 'General'.
Select DateDiff(DD, '1899-12-30', '2019-11-29') AS [column]
More info here
Non MS site explaining the situation
MS Office Info touching on the subject
Please note this basing your question on a SQL formatted date.

Convert datetime to date using a function

I have one column in my database that I need to convert from datetime to the date data type using a SQL Server function. I can't figure out how to get the correct syntax. Could I get some help with the syntax so IntelliSense will stop yelling at me and I can get the query to run?
CREATE FUNCTION fChangeDateFormat (#date01 date)
RETURNS DATE
AS
RETURN(
SELECT
Convert(DateTime, OrderDate, 101)
FROM
Orders
WHERE
OrderDate = #date01
)
You can use convert as,
select CONVERT(date,#date01,101) as dd from Orders
If you are using SQl server 2012 + use FORMAT,
'd' is to get the short date format.
SELECT FORMAT(#date01,'d','en-US') as dd from Orders

SQL Server - Calculate Age from date of birth in Varchar

Using SQL Server 2008 R2
I have a database of my friend's project. He used varchar to store date of birth of members because he was getting errors parsing entries to date. I want to show the age using a procedure code.
the format of date stored in mm/dd/yyyy where today's date is stored as 3/4/2016 . I wanted to use DATEDIFF function but it is not a good option with varchar.
Create procedure spCalculateAge
#dob varchar(10)
as
Begin
Select DATEDIFF(year,Convert(date,#dob),getdate()) as Age
End
I used this and it worked. Before it was giving conversion error but opening and closing SQL Server caused it to work.

Inserting CDate to SQL Server 2012 Flip Day and Month

I have a problem inserting a date from a VB.net Program to a SqlServer2012 instance.
First here is how i generate the data (Vb.net)
ExitTime = CDate("1.1.1970 00:00:00").AddSeconds(currentField).ToLocalTime
We add this value to a stored procedure (Vb.net)
With comsql5.Parameters.AddWithValue("#ExitTime", ExitTime)
In the Sql Server stored procedure
#ExitTime datetime, [...]
[...]
Insert into [table] ([ExitTime]) VALUES (#ExitTime)
Here is the output of the exit time in the vb.net
Exit Time : 08/07/2014 2:06:31 PM
Here is the same row in the Sql server database
2014-08-07 14:06:31.000
What I would like to see in the database is 2014-07-08 14:06:31.00
Because another part in the program does a check on the field but as a String... and it does not match because it flip the month and day
EDIT: TO be clear, I can't change the other part that does the comparison as a string. I know this is a poor way to compare datetime.
Thank for your time
Have you tried using the Convert function?
SELECT CONVERT (VARCHAR, getdate(), 121);
Check this links for more information MSDN - CAST and CONVERT and SQL Server Datetime Format

T-SQL: How to update just date part of datetime field?

In SQL Server 2008 I need to update just the date part of a datetime field.
In my stored procedure I receive the new date in datetime format. From this parameter I have to extract the date (not interested in time) and update the existing values date part.
How can I do this?
One way would be to add the difference in days between the dates to the old date
UPDATE TABLE
SET <datetime> = dateadd(dd,datediff(dd,<datetime>,#newDate),<datetime>)
WHERE ...

Resources