Date in table1 is in 'yyyymmdd' format but date in table2 is in 'yyyy/mm/dd' format. and I want to insert the data directly from table1 into table2.
For ex. Date Format in table1 is '20130813' and date format in table2 is '08/13/2013 12:00:00.000 AM' . Here 12:00:00.000 AM is default timestamp. Can you please help in converting the date from '20130813' to '08/13/2013 12:00:00.000 AM' in sybase ?
I'm not having Sybase currently installed, but you could try
SELECT CONVERT(datetime, '20131031')
e.g.
INSERT INTO TABLEA SELECT CONVERT(datetime, TABLEBCOLUMN)
You could also try:
select cast(dateformat('20131031','yyyymmdd') as datetime)
The advantages are:
Not relying on implicit conversion to date
CONVERT function is not ANSI SQL
Related
Hi i've a column the_date which is having sample data like
1900-01-01 00:00:00.000
1990-01-01 00:00:00.000
1990-01-02 00:00:00.000
1990-01-03 00:00:00.000
1990-01-04 00:00:00.000
1990-01-05 00:00:00.000
1990-01-06 00:00:00.000
1990-01-07 00:00:00.000
now i just want to select only date only and displaying it into 103 style and convert the column into Date format. i've tried this syntaxconvert(varchar,THE_DATE , 103) but then the column is not in Date format.
any help please.
This Works try it once
SELECT CONVERT(varchar,CAST(DATE_TIME AS DATE),103)AS Date_Time From <yourTable>
It's a little confusing because regardless of the datatype, you will always get the same answer with CONVERT. I'll illustrate this with 2 declared variables, 1 datetime datatype, the other date datatype:
DECLARE #mydatetime datetime = '1990-01-01 00:00:00.000'
DECLARE #mydate date = '1900-01-01'
SELECT convert(varchar,#mydatetime, 103) as mydatetime
SELECT convert(varchar,#mydate, 103) as mydate
Produces:
mydatetime
01/01/1990
mydate
01/01/1900
So you don't need to cast to date, then to 103 format.
If you don't like the time with date in your table (datetime) then you can always ALTER the column in the table to the date datatype. This can be done using SSMS or you can do the SQL:
ALTER TABLE mytable ALTER COLUMN mycolumn DATE
Where DATE is the new datatype. And then only the date is stored in the table.
Converting a string (or an equivalent database type) to another type is called "parsing". Converting another type to string is called "formatting".
The DATE or DATETIME type (or equivalent type in the front-end) does not store dates as string and has therefore no format. A number is used to represent the date internally which counts the days since a reference date (1753-01-01 for SQL-Server). The time is represented as decimal fraction.
Of course you always see the date as formatted when you open the table, because it is converted to a text for display, but it is not stored as formatted.
So, what you have to do if your date is given as text, is to parse it using the 121 format (YYYY-MM-DD HH:MI:SS.MMM (24h)) to get a DATE (or DATETIME).
CONVERT(DATE, the_date, 121) or CONVERT(DATETIME, the_date, 121)
If the_date is already of DATE (or DATETIME) type and you want to display it in the 103 format (DD/MM/YYYY)
CONVERT(VARCHAR(10), the_date, 103)
If the date is given as DATETIME type and you just want to strip off the time part and return the result as DATETIME type again, you can do
DATEADD(dd, DATEDIFF(dd, 0, the_date ), 0)
Note that no date format is involved here, since the date does never appear as text.
thanks for all your answer but for anyone who has my same query i got the solution from here
solution
I have a column in a table updatedDate - which is a datetime data type.
Data sample:
2017-10-15 18:08:22.000
2017-10-15 18:07:44.000
2017-10-15 18:07:17.000
2017-10-15 18:07:10.000
2017-10-14 18:00:54.000
2017-10-13 17:59:23.000
2017-10-13 17:59:13.000
I would like to display a list of DISTINCT dates, in the format of dd/mm/yyyy, but for the life of me... I can't get it. I would think it should be:
SELECT DISTINCT convert(datetime,updatedDate,103)
FROM [tblStudentCourses]
ORDER BY updatedDate DESC
But it does not actually convert to the 103 format... it just gives it to me as the full date and time format as originally, without any CONVERT.
What I want to get would be:
15/10/2017
14/10/2017
13/10/2017
What am i doing wrong?
Thanks!
To display the date in DD/MM/YYYY format, you can use CONVERT(VARCHAR(10), DateColumn, 103).
To maintain the proper ordering, you can wrap it all in a subquery. For example:
SELECT DisplayDate
FROM (
SELECT DISTINCT
DisplayDate = CONVERT(VARCHAR(10), CAST(UpdatedDate AS DATE), 103),
ActualDate = CAST(UpdatedDate AS DATE)
FROM [tblStudentCourses]
) AS T
ORDER BY ActualDate;
Note: Cast the date column to date if it's datetime like your sample data.
SELECT DISTINCT convert(VARCHAR,updatedDate,103)
FROM [tblStudentCourses]
ORDER BY updatedDate DESC
You should use convert to varchar for formating to various date formats
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)
I have a table named 'Table1'.
It has 3 columns namely
1. vehicle Number
2. startdatetime
3. enddatetime
the table is as below->
vehicleno | startdatetime | enddatetime |
1 2013/07/16 00:00:00 2013/07/17 00:00:00
2 2013/07/16 00:00:00 2013/07/18 14:00:00
3 2013/07/17 12:19:00 2013/07/20 17:35:00
4 2013/07/19 10:24:56 2013/07/19 20:14:00
5 2013/07/15 08:10:00 2013/07/18 09:10:00
Now i want a o/p such that ,At the time of executing the query if the present datetime is between the startdatetime and enddatetime then the record should be displayed in my o/p table.
I have tried with the query..
select * from Table1 where startdatetime between '2013/07/17 00:00:00' and '2013/07/17 23:59:59' or enddatetime between '2013/07/17 00:00:00' and '2013/07/17 23:59:59'
but i didn't get the result i want.
Please help me out..
If you are looking for the current date why not use GETDATE?
Returns the current database system timestamp as a datetime value
without the database time zone offset. This value is derived from the
operating system of the computer on which the instance of SQL Server
is running.
Something like
select *
from Table1
where GETDATE() BETWEEN startdatetime AND enddatetime
SQL Fiddle DEMO
I would think you'd want to do:
select vehicleno ,startdatetime, enddatetime
from table1
where getUTCDate() between startdatetime and enddatetime
Then if you aren't in GMT you can do a dateAdd(hour, 5, getUTCDate()) if you are say EST.
It's always more correct to specify the columns you want to use in the select statement.
And at this point since we live in a global community I feel it's more appropriate to use getUTCDate() instead of just regular getDate() because getDate() just does the timezone that the server is located in and it will depend on where the server is if this works for your situation.
You can see the definition of GetUTCDate()
Try the following:
select *
from Table1
where GetDate() between startdatetime and enddatetime
GetDate() gets the current datetime
Just for the record, if you want to use literal dates in SQL, use ISO date format (YYYY-MM-DD hh:mm:ss). That is the only format guaranteed to be interpreted correctly regardless of your locale settings.
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')