how to convert nvarchar to time ,not datetime? - sql

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.

Related

SQL How to format M/DD/YYYY (1/31/1960) to DD/MM/YYYY (31/01/1969)

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.

SQL Server date conversion not is formatting the output

My SQL statement is supposed to add 5 days to the date column and reformat the output to MM/DD/YYYY.
The calculation is working, but the format won't change, no matter what value I use.
The date column is defined as varchar(128) (I've used datetime and varchar(128) in the convert statement).
What am I doing wrong?
SELECT
FSI_Date, DATEADD("d", +5, CONVERT(DATE, FSI_Date, 101)) AS NEWDAY,
FROM
DB_test
FSI_Date NEWDAY
---------------------------
12/12/2015 2015-12-17
SQL Server will convert mm/dd/yyyy to a date, so, you just need the dateadd() and then convert back to your desired format.
Remove the final convert(...,101) if you want a natural date.
Example
Select FSI_Date
,NewDay = convert(varchar(10),DateAdd(DAY,5,#FSI_Date),101)
From DB_Test
Returns
FSI_Date NewDay
12/12/2015 12/17/2015

Convert datetime string in datetime format in SQL server

I'm using MS SQL server and I have a date field of type text. The dates stored there are in this format
2017-03-01T18:23:02+0700
I'm trying to convert this field in a datetime field but I fail. I have tried
CONVERT(datetimeoffset,date, 127)
CONVERT(datetime,date, 127)
CONVERT(datetime2,date, 127)
but I keep getting
Conversion failed when converting date and/or time from character
string.
I think the problem is that according to ISO8601 the time offset must be in the format hh:mm while mine is hhmm. I don't mind keeping only the date (yyyy-mm-dd) if it is more easy.
I have read similar question but none matches exactly my case and I can't figure out the solution.
Try this
Declare #dt varchar(50)
set #dt = '2017-03-01T18:23:02+0700'
select convert(datetime, replace(LEFT(#dt, LEN(#dt) - 1), '+', '.'), 126)
If you need only date part then you can use below query
SELECT CAST(LEFT('2017-03-01T18:23:02+0700',10) as DATE)
Use Below query to convert datetime :
SELECT CONVERT(DATETIME,REPLACE(REPLACE('2017-03-01T18:23:02+070','T','
'),'+','.'),103)
For DATE only use below query :
SELECT CONVERT(DATE,REPLACE(REPLACE('2017-03-01T18:23:02+010','T','
'),'+','.'),102)
It doesn't seem to work in both ways (both raise error):
SELECT CONVERT(datetime,'2017-03-01T18:23:02+0700',127)
SELECT CONVERT(datetime,'2017-03-01T18:23:02+07:00',127)
It seems that it works only specifying Z as time zone:
SELECT CONVERT(datetime,'2017-03-01T18:23:02Z',127)

Conversion of varchar to date type in SQL

I am trying to convert varchar field which is in format dd.mm.yyyy to date type field in SQL SERVER 2008.
I used approach like convert(datetime, left(yourDateField, 2) + '01 20' + right(yourDateField, 4), 100) but unable to so.
Is there any way to convert Varchar string which is in format dd.mm.yyyy to Date type in SQL?
select convert(date,dateField,104)
from tab
Try this Datetime to Varchar
SELECT CONVERT(VARCHAR(20),GETDATE(),104)
Varchar to Datetime
SELECT CONVERT(DATETIME,yourfeild,104)
Refer this link
Try this.
DECLARE #String VARCHAR(100)= '21.11.2016';
SELECT CONVERT(DATE,#String,103)
If Your date field is not in proper datetime format then this type of Out of range value error will occurred.
If your date field is in proper datetime format then you can easily convert varchar field to datetime format.
see example:
select CONVERT(datetime,'2016-11-21 01:02:12')
select CONVERT(datetime,'2016-11-31 01:02:12')
select CONVERT(datetime,'2016-11-21 00:00:01 PM')
In above Example,First query get easily converted to datetime
but second and third query will give error as out of range value. because 31 Nov is invalid date. and 00:00:00 PM is also wrong date format.
IF your field is in correct format
then simply try any one of the below query
DECLARE #String VARCHAR(100)= '21.11.2016';
SELECT CONVERT(DATETIME,#String,104)
SELECT CONVERT(DATETIME,#String,103)
Hope so, you get what I am trying to say.

How do I strip the date off of a datetime string in SQL SSIS?

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.