A column of varchar data type whose values looks like:
01:23:21
00:45:00
10:00:00
05:01:04
need summing up. But as I do, I get the error:
"Operand data type varchar is invalid for sum operator."
How do I sum the above value while still retaining the format 00:00:00?
Thank you
Time does not support sum but datetime does
select convert(time, CONVERT(datetime, '00:45:00') + CONVERT(datetime, '01:00:04'))
Related
Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);
I want to convert a String to datetime.
So I'm building this code:
SELECT CONVERT(datetime, '23:00', 103)
If I try to execute this code I have this:
1900-01-01 23:00:00.000
But if I try to execute this convert:
SELECT CONVERT(datetime, '24:00', 108)
I have this error:
Converting a varchar data type to datetime generated a value not within the range of allowed values.
Datetime type does not support "24th hour". Its time range is 00:00:00 through 23:59:59.997.
Try use instead
SELECT CONVERT(datetime, '00:00', 108)
For more details check datetime (Transact-SQL) page.
I am looking for the correct syntax to test in a TSQL WHERE clause if a
datetime2(7) type is equal to another.
WHERE (CAST(modifiedDate AS DATETIME) = '9/29/2016 3:24:24 PM')
I also tried
WHERE (CAST(modifiedDate AS DATETIME) LIKE '9/29/2016 3:24:24 PM')
And
WHERE (CAST(modifiedDate AS datetime2) = CAST('09/29/2016 3:24:24 PM' AS datetime2))
I believe I have the right hand side stated incorrectly, but that is the exact value in the database.
I am looking for all records that match that datetimestamp.
To be clear I did try to search for other results..'
I thought this was a little flaky for a search result on this site.
DB field type...
To compare DATETIME values, you need DATETIME values to compare. You can use the TSQL CONVERT function to convert a string to the DATETIME datatype. For example:
CONVERT(DATETIME, '2016-09-28 15:34:00', 20)
Note that the third argument is the "style". Example above uses style 20, the ODBC Canonical style YYYY-MM-DD HH:MI:SS (24 hour clock). There are several other styles available, maybe you find one that matches your string format. (If you can't find a match, then you will need to use some string manipulation functions to reformat your string into a format where a style is available.)
As the second argument, you can use a string literal (as shown in the example above), or you can use a reference to a CHAR or VARCHAR column.
Reference: CAST and CONVERT (Transact-SQL)
On a different, but related, note: Why are date time values being stored as strings in the database, and not DATETIME datatype?
If the datatype of the column is DATETIME2(7), then I think you would want to compare DATETIME2(7) datatypes.
If we do an "equal to" comparison, that's going to be an exact match, including the fractional seconds. If you want to match DATETIME2(7) values from a given second, you could use a range comparison:
WHERE t.my_col_datetime2_7 >= '2016-09-29 15:24:24'
AND t.my_col_datetime2_7 < '2016-09-29 15:24:25'
Note the format of the string literals allowed for comparison to DATETIME2 are YYYY-MM-DD HH:MI:SS (24 hour clock) with optional fractional seconds .nnnnnnn.
Reference: Supported String Literal Formats for datetime2
Not sure where you are having issues. I guess it depends how you are storing your date value. If it's datetime2, this works fine.
DECLARE #testtable TABLE(
ID INT
, modifieddate DATETIME2)
INSERT INTO #testtable
(id, modifieddate)
VALUES
(1, '9/29/2016 3:24:24 PM'),
(2, '2/01/2016 3:24:24 PM'),
(3, '6/25/2016 3:24:24 PM')
SELECT *
FROM #testtable
SELECT *
FROM #testtable
WHERE CAST(modifieddate AS DATETIME2) = '09/29/2016 3:24:24 PM'
SELECT *
FROM #testtable
WHERE modifieddate = '9/29/2016 3:24:24 PM'
SELECT *
FROM #testtable
WHERE modifieddate = CAST('09/29/2016 3:24:24 PM' AS DATETIME2)
What's the easiest way to update a table that contains a DATETIME column on TSQL with RANDOM value between 2 dates?
I see various post related to that but their Random values are really sequential when you ORDER BY DATE after the update.
Assumptions
First assume that you have a database containing a table with a start datetime column and a end datetime column, which together define a datetime range:
CREATE DATABASE StackOverflow11387226;
GO
USE StackOverflow11387226;
GO
CREATE TABLE DateTimeRanges (
StartDateTime DATETIME NOT NULL,
EndDateTime DATETIME NOT NULL
);
GO
ALTER TABLE DateTimeRanges
ADD CONSTRAINT CK_PositiveRange CHECK (EndDateTime > StartDateTime);
And assume that the table contains some data:
INSERT INTO DateTimeRanges (
StartDateTime,
EndDateTime
)
VALUES
('2012-07-09 00:30', '2012-07-09 01:30'),
('2012-01-01 00:00', '2013-01-01 00:00'),
('1988-07-25 22:30', '2012-07-09 00:30');
GO
Method
The following SELECT statement returns the start datetime, the end datetime, and a pseudorandom datetime with minute precision greater than or equal to the start datetime and less than the second datetime:
SELECT
StartDateTime,
EndDateTime,
DATEADD(
MINUTE,
ABS(CHECKSUM(NEWID())) % DATEDIFF(MINUTE, StartDateTime, EndDateTime) + DATEDIFF(MINUTE, 0, StartDateTime),
0
) AS RandomDateTime
FROM DateTimeRanges;
Result
Because the NEWID() function is nondeterministic, this will return a different result set for every execution. Here is the result set I generated just now:
StartDateTime EndDateTime RandomDateTime
----------------------- ----------------------- -----------------------
2012-07-09 00:30:00.000 2012-07-09 01:30:00.000 2012-07-09 00:44:00.000
2012-01-01 00:00:00.000 2013-01-01 00:00:00.000 2012-09-08 20:41:00.000
1988-07-25 22:30:00.000 2012-07-09 00:30:00.000 1996-01-05 23:48:00.000
All the values in the column RandomDateTime lie between the values in columns StartDateTime and EndDateTime.
Explanation
This technique for generating random values is due to Jeff Moden. He wrote a great article on SQL Server Central about data generation. Read it for a more thorough explanation. Registration is required, but it's well worth it.
The idea is to generate a random offset from the start datetime, and add the offset to the start datetime to get a new datetime in between the start datetime and the end datetime.
The expression DATEDIFF(MINUTE, StartDateTime, EndDateTime) represents the total number of minutes between the start datetime and the end datetime. The offset must be less than or equal to this value.
The expression ABS(CHECKSUM(NEWID())) generates an independent random positive integer for every row. The expression can have any value from 0 to 2,147,483,647. This expression mod the first expression gives a valid offset in minutes.
The epxression DATEDIFF(MINUTE, 0, StartDateTime) represents the total number of minutes between the start datetime and a reference datetime of 0, which is shorthand for '1900-01-01 00:00:00.000'. The value of the reference datetime does not matter, but it matters that the same reference date is used in the whole expression. Add this to the offset to get the total number of minutes between the reference datetime.
The ecapsulating DATEADD function converts this to a datetime value by adding the number of minutes produced by the previous expression to the reference datetime.
You can use RAND for this:
select cast(cast(RAND()*100000 as int) as datetime)
from here
Sql-Fiddle looks quite good: http://sqlfiddle.com/#!3/b9e44/2/0
Help me Stackoverflow, I'm close to going all "HULK SMASH" on my keyboard over this issue. I have researched carefully but I'm obviously not getting something right.
I am working with a Julian dates referenced from a proprietary tool (Platinum SQL?), though I'm working in SQL 2005. I can convert their "special" version of Julian into datetime when I run a select statement. Unfortunately it will not insert into a datetime column, I get the following error when I try:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
So I can't setup datetime criteria for running a report off of the Stored Procedure.
Original Value: 733416
Equivalent Calendar Value: 01-09-2009
Below is my code... I'm so close but I can't quite see what's wrong, I need my convert statement to actually convert the Julian value (733416) into a compatible TSQL DATETIME value.
SELECT
org_id,
CASE WHEN date_applied = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_applied-729960,convert(datetime, '07-25-99')),101)
END AS date_applied,
CASE WHEN date_posted = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_posted-729960,convert(datetime, '07-25-99')),101)
END AS date_posted
from general_vw
SELECT
org_id,
CASE WHEN date_applied = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_applied-729960,convert(datetime, '07-25-99'))
END AS date_applied,
CASE WHEN date_posted = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_posted-729960,convert(datetime, '07-25-99'))
END AS date_posted
from general_vw
You're casting to char but want a datetime so that's one easy fix.
You were also using '00-00-00' as your minimum date, but the minimum TSQL date is '1753-01-01'. Alternatively you could use something like ('1900-01-01') but that would need a change to the "less than" date_applied comparer.
I've added a "less than" date_applied comparer too. I calculated this as "SELECT 729960 + datediff(day,convert(datetime, '07-25-99'), convert(datetime,'1753-01-01'))". Any number less than this would cause a date underflow.