I have this string: '30/05/2010', and I would like to enter it to a smallDatetime field.
In the database it should look something like this 2010-05-30 15:33:25
Any Idea how?
TY
use
select convert(smalldatetime,'30/05/2010',103)
SET DATEFORMAT DMY
SELECT CAST('30/05/2010' as smalldatetime)
Where do you want the time aspect to come from? The convert above will append 00:00 (midnight) for smalldatetime because:
the string has no time information
smalldatetime resolves to a minute resolution
You need to use the datetime field type if you want in this format 2010-05-30 15:33:25. If you want only the date, use the date type only.
You can use cast('05/30/2010' as smalldatetime).
If you need to have exact 15:33:25 time then you can use several dateadd calls, e.g. select dateadd(hh, 15, cast('05/30/2010' as smalldatetime)) returns 2010-05-30 15:00:00.
Related
I have a column that has date and time mixed together i.e. 1/31/1960 12:00:00AM and I would like to convert them into two-column through SQL function:
(a) DD/MM/YYYY i.e 31/01/1969
(b) HH:MM i.e 12:00
Thanks in advance.
Take a look at the CONVERT() function, specifically the style for datetime. Additionally, we can CAST() the DATETIME as a TIME data type to extract the time.
Assuming your original column is a DATETIME data type, you can run
SELECT
CONVERT(NVARCHAR(24),{DateField},103)
,CAST({DateField} AS TIME)
If it is string, you can cast it then convert
SELECT
CONVERT(NVARCHAR(24),CAST('1960-01-31' AS DATETIME),103) AS ReportingDate
,CAST(CAST('1960-01-31' AS DATETIME) AS TIME) AS ReportingTime
If you're on a sufficiently recent version of SQL Server, the FORMAT() function is your friend.
DECLARE #d DATE = GETDATE();
SELECT FORMAT(#d, 'dd/MM/yyyy');
Note, that second argument is a .NET formatting string, so you should be able to use whatever is available from the Framework.
I can't seem to figure this one out and tried searching stackoverflow.
I have two lines where I want to convert the date. I didn't create these datatypes or tables and don't want to change the date types in case it messes something up.
I have one table where one of the columns is of type (datetime2(7), not null).
I have another table where the column is of type (datetime, null).
when I do a query they return a time (respectively) that looks like
2015-01-25 10:11:50.9050000
2015-01-19 10:24:42.323
The above is before I do any conversion or casting.
I'd like to convert the format of those to
dd/mm/yy hh:mi:ss:mmmAM
So I used the code below (respectively)
SELECT TagName, CONVERT(DateTime, EventStamp, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
and
SELECT TagName, CONVERT(DateTime, DateTime, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
But it doesn't seem to work. It drops the ss (seconds) part and the month/day/year portion does't come out according to style 131. Anyone know why? It looks like below
25/01/2015 10:11 AM
19/01/2015 10:24 AM
I tried a bunch of way and it doesn't seem to work. I'm running MS SQL server 2012 and just doing the query in SQL Server Manager Studio.
If you want to convert to an output format, you want to convert from a datetime to a string.
Try this:
SELECT TagName, CONVERT(varchar(255), EventStamp, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
Your code tries to convert from datetime to datetime, which isn't what CONVERT is meant to do. If you wish to control the string representation of a date in your output, convert to nvarchar instead: CONVERT(NVARCHAR(25), DateField, 131). If you'd like to change the default string representation of a datetime field on a per-session basis, use the SET DATEFORMAT command. https://msdn.microsoft.com/en-us/library/ms189491.aspx
if i try to cast string date to datetime like
select cast('12/01/2010' as datetime) then
it works
but if i try to cast like
select cast('22/01/2010' as datetime) then it is giving error.
again if i try to cast string date to datetime like
select cast('2010/12/01' as datetime) then it works
but if i try to cast like
select cast('2010/25/01' as datetime) then it is giving error. my requirement is whatever way user input date that should be successfully converted to datetime. please tell me best solution
In your case the sql server assumes, that date format id mm/dd/yyyy - US format,
you want to use French format, so use convert
select CONVERT(DATETIME, '12/01/2010', 103)
instead you'll get an error because there in no such a month number - 22
The best solution when passing datetime as string - to use short(without timezone) ISO format:
yyyyMMdd HH:mm:ss.ffff
I recommend you to use fixed '20111231' format. You do not need to use cast, convert or similar command.
I'm working on a data warehouse project and would like to know how to (preferably in a Derived Column component in a Data flow) strip the date piece off of a SQL datetime record.
Once I have the datetime converted to just a time I am going to do a lookup on the time to find the related time record in a time dimension table.
Can someone give me a simple function to accomplish this inside a derived column transform?
Example: Transform a datetime such as "12/02/2008 11:32:21 AM" into simply "11:32:21 AM".
I would just do a cast to DT_DBTIME type (using Derived Column transform, or Convert type transform). DT_DBTIME contains just (hours, minutes, seconds) part of the date/time, so you'll get rid of the date part.
If you need to do this in a variable expression Michael's solution won't work, but you can use the following expression:
(DT_DATE)(DT_DBDATE)GETDATE()
(DT_DBDATE) converts the current date and time to a date only. But the new datatype is not compatiple with SSIS's datetime. Therefore you'll have to use (DT_DATE) for converting to a compatible type.
Courtesy of this solution belongs to Russel Loski who has posted it in his blog:
http://www.bidn.com/blogs/RussLoski/ssas/1458/converting-datetime-to-date-in-ssis
Actually if you reverse the first 2 expressions like this: (DT_DBDATE)(DT_DATE)GETDATE()
instead of (DT_DATE)(DT_DBDATE)GETDATE(), then you will TRUNCATE the time off the date field.
If the DT_DATE expression is before the DT_DBDATE expression, you will still have the time portion in your output, but it will be reset to all zeroes.
Ran into this with writing a report for a scheduling app, needed the time that was stored as part of a datetime data type. I formated the datetime as 0 which gives you this mon dd yyyy hh:miAM (or PM), and just did a substring of that which returned the time only in an AM/PM format.
Example below.
DECLARE #S DATETIME = GETDATE()
SELECT SUBSTRING(CONVERT(NVARCHAR(30), #S , 0) , 13 , 10) AS ApptTime
, CONVERT(NVARCHAR(30), #S , 0) AS ApptDate
I personally use a series of functions for this. E.g.:
ALTER FUNCTION [dbo].[TIMEVALUE]
(
#Datetime datetime
)
RETURNS datetime
AS
BEGIN
RETURN (#Datetime - CAST(ROUND(CAST(#Datetime AS float), 0, 1) AS datetime))
END
I'd love to claim all the credit but it should really go to this guy.
DECLARE #DateNow smalldatetime
SET #DateNow='12:30'
select #DateNow
-------------------------------------OR--------------------------------------
select CAST( '12:30' as datetime )
Result: 1900-01-01 12:30:00.000 (i don't want this)
But i need this result in time format not string not datetime?
Result: 12:30 (i want this)
Like José said, you can use CONVERT to display a datetime as date. MSDN has a list of all possible formats. For example format 8 is hh:mi:ss:
select convert(varchar(32),getdate(),8)
12:51:21
Now, you can cut the seconds off by specifying less characters:
select convert(varchar(5),getdate(),8)
12:51
Another often used format is 121, yyyy-mm-dd hh:mi:ss.mmm(24h):
select convert(varchar(32),getdate(),121)
2009-05-08 12:51:21.987
Where you can pick the time part like:
select substring(convert(varchar(32),getdate(),121),12,5)
12:51
Or to combine the string trickeries:
select right(convert(varchar(16),getdate(),121),5)
12:51
Right? Right!
There's a TIME type in SQL Server 2008, but in previous versions, you can represent it as a varchar for display.
This is how you can retrieve the time portion of a DATETIME field in the format you want.
DECLARE #DateNow smalldatetime
SET #DateNow = GETDATE() -- 2009-05-08 12:58:02.680
SELECT CONVERT(CHAR(5), #DateNow, 8)
-- this returns the time portion as 12:58
You can use the CONVERT function.
By the way, there's no "TIME" datatype in SQL Server. So, the converted result will always be a STRING.
EDIT: There's no "TIME" datatype in SQL Server versions < SQL Server 2008.
If you need to compare, add and/or subtract time (only) values, think about the possibility to use a INT to store the "TIME" value and then to CONVERT it to CHAR when displaying the result.