SQL Server 2008 - date format issue while inserting - sql-server

I am trying to insert date into a table but the date and format of inserted date is messed up. The datatype in the table is Date. My insert script is as below.
insert into Trans(ID, TDate, Description)
values(1, CONVERT(datetime, 25-02-2012, 101), 'Opening')
I am trying to insert in dd/MM/yyyy format and I want it in the same format in my table. But in my table the date is 1894-07-22 !!
I want the date to be inserted exactly as the format I wish and I want to see the inserted date as 25-02-2012 in the table.
What is wrong here ? Can somebody help ?

Try CONVERT(datetime ,'25-02-2012', 103)
use some single quotes around the value
choose your format accordingly (101 is US, meaning mm/dd/yyyy)
see http://msdn.microsoft.com/en-us/library/ms187928(v=sql.100).aspx for more details

you should use single quotes around your date. If you want date format of dd/mm/yyyy format then you will want to use the convert(datetime, '25-02-2012', 103)
insert into Trans(ID,TDate,Description)
values(1,CONVERT(datetime,'25-02-2012',103),'Opening')
if you use convert(varchar, getdate(), 101) the format of the date will be mm/dd/yyyy.
There are several helpful links to use as a reference for datetime conversions:
How to format datetime & date
Tips & tricks SQL Server Date formats

insert into emp
(EMPNO,ENAME,DEPTNO,JOB,SAL,COMM,MGR,HIREDATE)values
(7839,'KING',10,'PRESIDENT',5000,NULL,NULL,'17-11-81')

Related

how to compare date using where statement?

i have a table called dbo.reminder in that i'm having a column name rdate i have declared that as
NVARCHAR(50)
i want to sort the table using where condition with the present date how can i do that?
i have tried this query
SELECT rdetail,
rid,
rdate = CONVERT(VARCHAR, CONVERT(DATETIME, rdate, 103), 103)
FROM dbo.reminder
where rdate='23/01/2017'
but its not sorting please help me regarding this ?
Do not store date as NVARCHAR. Change your rate datatype to date and try this way
SELECT rdetail,rid,rdate
FROM dbo.reminder
where rdate = convert(date,'23/01/2017' ,103)
Convert the varchar date input to date and check with your rate column.
Better to pass imput in YYYYMMDD or YYYY-MM-DD format which is universal and does not require any conversion
If you cannot change the datatype then
SELECT rdetail,rid,rdate
FROM dbo.reminder
where convert(date,rdate,103) = convert(date,'23/01/2017' ,103)

How to search by Date in this DD/MM/YYYY format from TimeStamp column in SQL Server?

I have a table called Transaction. One of the column name is Time,datatype is TimeStamp. So the data is looking like this 2015-01-14 23:22:11.000.
Now I want to search by Date in where clause in this format DD/MM/YYYY
for example
select *
FROM Transaction
WHERE Date='25/12/2016' // DD/MM/YYYY
Thanks
Your question is not completely clear, but it appears that you have the need to compare date data in the format DD/MM/YYYY against a timestamp column in your table. One option is to use the CONVERT function to convert both the input and your timestamp column to a common DATETIME format, and then do the comparison.
SELECT *
FROM Transaction
WHERE CONVERT(DATETIME, DATEDIFF(DAY, 0, TIME)) = CONVERT(DATETIME, '25/12/2016', 103)
This will return all records whose date components are '2016-12-25'. Note that both sides of the comparison would have a time component set to midnight of that day.
select *
FROM Transaction
WHERE Date=CONVERT(NVARCHAR(50), '25/12/2016', 103) DD/MM/YYYY
Use canonical format (YYYY-MM-DD) on date columns:
SELECT *
FROM Transaction
WHERE Date=convert(datetime, '2016-12-25')
You can also use time ranges with canonical format YYYY-MM-DD HH:MI:SS:
SELECT *
FROM Transaction
WHERE Date BETWEEN convert(datetime, '2016-12-25 00:00:00')
AND convert(datetime, '2016-12-25 23:59:59')
You have to pass date in dd/mm/yyyy format, take parameter datatype as varchar and convert date like in the following SQL statement
SELECT CONVERT(varchar(25),CreateDate,103) as CreateDate, [UserID],[FirstName]+' '+[LastName] AS 'Name',[NomineeName],[City],[UserDocument]
FROM [dbo].[UserMaster]
WHERE CONVERT(varchar(25),[CreateDate],103) between convert(varchar(25),#datefrom,103) and convert(varchar(25),#dateto,103)

Convert and sort varchar Date dd-MMM-yyyy

I'm using a table that has the date as varchar. example dateformat:
17-Jun-2015
I have tried the following ways to convert and sort the date(dd-MMM-yyyy) to dateTime.
SELECT date, name, author
FROM sometable
ORDER BY CONVERT(DATETIME, date, 106) DESC
I have also tried converting the date in the select statement. doesn't work. The error is
Conversion failed when converting date and/or time from character string.
The conversion types through some similar questions but I have not found any solutions to the format I have. Is there some way of selecting the delimiter between the day month and year??
I also had a browse through this link which has the formats for datetime formats. 106 was the closest to my varchar date. Only my date in the table has '-' between day month and year.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Appreciate any help.
It should be absolutely fine to use 106 to convert your date format.
But I guess your table contains some of the invalid values in the column causes the error, try to spot them out by TRY_CONVERT:
SELECT date, name, author, TRY_CONVERT(datetime, date, 106) AS convertresult
FROM sometable
WHERE TRY_CONVERT(datetime, date, 106) IS NULL AND date IS NOT NULL
I would use date style 113 to convert to datetime format like this:
DECLARE #date VARCHAR(20) = '17-Jun-2015';
SELECT CONVERT(VARCHAR(20),CAST(#date AS DATETIME),113)

sql server date stored in table comparing with the system date

really struggling to convert the below date format 10.11.2013 in the format
of the system date.
I have a request to compare column data with the system date.
In the column the date is storing as DD.MM.YYYY
Or we need to convert system date (getdate()) to the date values stored in the database table.
Thanks in Advance
Regards,
Dev
try this:
convert system date (getdate()) to the date values stored in the
database table.
Gives formats as DD.MM.YYYY
select CONVERT( CHAR(10),GETDATE(),104) -- outputs 11.09.2014
with Your Column
select CONVERT( CHAR(10),MyColumn,104)

SQL Server: How to query date in a certain format

I am pretty new to SQL and hope someone here can help me with the following:
I have a table in SQL Server with one column storing dates and I have a simple stored procedure to select data from this table.
Currently it fetches the date including hours and minutes.
How can I achieve that it fetches the date in format yyyy-mm-dd (without the hours and minutes) ?
My column is called "logDate" and my table is called "logTable".
Thanks for any help with this, Tim
If you only want to store the date, convert the column to a date type, not a datetime.
If you want to keep the date and time in the data, but just display the date
select convert(date, logDate) from logTable
If you want to return the date as a string, use convert
select convert(varchar(10), logDate, 120) from logTable

Resources