Reporting Services - Excel column with number format - sql

I am using the following function to format a calculation in Reporting Services 2012:
=Format(DateAdd("s", (DateDiff(DateInterval.Second, Fields!FechaInicio.Value, Fields!FechaFinal.Value)), "00:00:00"), "HH:mm:ss")
When I export the file to Excel it shows me the column with the General data type. Is there a way for the column to be exported with the Numeric data type?

You have two options; Cast and Convert. See the code below and adapt to your needs
--Drop Table t
SELECT *
INTO t
FROM (VALUES
(45, 'alex'),
(98, 'diana'),
(32, 'peter'),
(98, 'daniel'),
(45, 'alex'),
(23, 'bob'),
(98, 'peter')
)
v (id, name)
Select *
From t
SELECT CAST(id AS INT) FROM t;
GO
SELECT CONVERT(INT,id) FROM t;
GO
Here is a link for more info.
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15

Related

H2 DB SQL, how to make a variable of the yersterday's date and call it after

Here is example how can i select the day before
SELECT DATEADD(day, -1, CAST(Current_date AS date)) AS YesterdayDate;
Here is my insert, how can I make YesterdayDate to hold a real date in this example?
INSERT INTO dish(id, name, date_added, price, restaurant_id)
VALUES (14, 'SomeDish', YesterdayDate, 120,2);
You need to use a user-defined variable:
SET #YesterdayDate = CURRENT_DATE - INTERVAL '1' DAY;
INSERT INTO dish(id, name, date_added, price, restaurant_id)
VALUES (14, 'SomeDish', #YesterdayDate, 120, 2);
These variables aren't persisted and they are visible only in the current session. They are prefixed with the # symbol.
If you need to use the variable in other sessions, you can create a constant instead:
CREATE CONSTANT YesterdayDate VALUE CURRENT_DATE - INTERVAL '1' DAY;
INSERT INTO dish(id, name, date_added, price, restaurant_id)
VALUES (14, 'SomeDish', YesterdayDate, 120, 2);
-- After use
DROP CONSTANT YesterdayDate;
Constants are persisted into database and they're visible in all sessions. Their names are used as is.
Values of user-defined variables and constants will not be updated automatically for the next day.
These variables and constants aren't portable across different DBMS.
INSERT INTO dish(id, name, date_added, price, restaurant_id)
VALUES (14, 'SomeDish', DATEADD(day, -1, CAST(Current_date AS date)), 120,2);
Hope, you have tried this

I would like to insert datetime into SQL table as 13 digit number by sql query

The table column assignedDate value is a 13 digit number like 1536346340276, format is 2014-12-31 15:17:24.736
Once I insert the data with query
INSERT INTO Test (id, assignedDate)
VALUES (1, (SELECT CAST(CONVERT(DATETIME, '2014-12-31 15:17:24.736') AS NUMERIC)))
The result is 42003
How to convert 13 digit number using by SQL query?
If we assume 1536346340276 doesn't actually translate to 2014-12-31 15:17:24.736, but that the 13 digit number is simply an example of the data currently in the table and that the date provided is an example of how the input format will be, it looks like it could actually be Unix time
SELECT DATEADD(second, 1536346340276/1000 ,'1970/1/1') -- is 2018-09-07 18:52:20.000
SELECT DATEDIFF_BIG(MILLISECOND,{d '1970-01-01'}, getdate()), GETDATE() -- is 1537195014053 at 2018-09-17 14:36:54.053
If you're on SQL Server 2016 or higher, the new insert would look like:
INSERT INTO Test (id, assignedDate)
VALUES (1, (SELECT DATEDIFF_BIG(MILLISECOND,{d '1970-01-01'}, getdate())))

Convert from nvarchar to decimal and filter by range

I would like to perform a query on these string values:
Record ID / Value
1 / 10.5
2 / 12
3 / 8
4 / 19.25
5 / 16.4
6 / 14
The value column is a nvarchar column. I would like to filter the results between two string values inserted by users (for example 10 and 15). I tried to use:
SELECT Value
FROM Table
WHERE CONVERT(decimal(10, 2), Value)
BETWEEN CONVERT(decimal(10, 4), '10')
AND CONVERT(decimal(10, 4), '15')
But I have the following error:
Error converting data type nvarchar to numeric.
What is wrong with the data types?
You could use TRY_CONVERT (SQL Server 2012 and newer):
SELECT Value
FROM Table
WHERE TRY_CONVERT(decimal(10, 2),Value) BETWEEN TRY_CONVERT(decimal(10, 4), '10')
AND TRY_CONVERT(decimal(10, 4), '15')
Better approach is to add computed column:
CREATE TABLE tab(
value VARCHAR(100),
value_as_decimal AS (TRY_CONVERT(decimal(10, 2),Value)) PERSISTED
);
CREATE INDEX tab_ix ON tab(value_as_decimal) INCLUDE(Value);
SELECT Value
FROM Table
WHERE value_as_decimal BETWEEN TRY_CONVERT(decimal(10, 4), '10')
AND TRY_CONVERT(decimal(10, 4), '15');
DBFiddle Demo
Ideal solution is to store value as DECIMAL(10,4) and forget about casting.

SQLite Parsing TEXT column with date info

I'am using SQLite DB and I have "Date" column that is VARCHAR
I need to extract data between 2 dates...
this is what I tried....
SELECT * FROM Table1 WHERE Date BETWEEN '14/03/2017 17:00:10' AND '16/03/2018 17:00:12'
SELECT * FROM Table1 WHERE strftime('%d/%m/%y %H:%M:%S', Date) BETWEEN strftime('%d/%m/%y %H:%M:%S','15/07/2016 20:00:09') AND strftime('%d/%m/%y %H:%M:%S','16/07/2017 21:00:09')
SELECT * FROM Table1 WHERE strftime('%d/%m/%y %H:%M:%S', Date) BETWEEN '2017/07/15 20:00:09' AND '2017/07/17 21:00:09'
Any idea what I am doing wrong ?
If you have a date/time column, then just do:
SELECT t1.*
FROM Table1 t1
WHERE t1.Date >= '2017-03-14 17:00:10' AND
t1.Date < '2018-03-16 17:00:12';
Use ISO/ANSI standard date formats for constants!
I strongly discourage you from using between with date/time values. Here is a blog post on the subject, which although for SQL Server applies to all databases.
You can't use SQLite's strftime because it's formatting function, it can not parse input string.
Basically you have two options:
try to parse string using builtin functions
create user defined function
If you can rely on the fixed positions, you can easily parse your string and format it back to comply with one of supported SQLite DateTime formats. In your case it might look like this:
SELECT [Date] FROM Table1 WHERE
DATETIME(printf('%04d-%02d-%02d %02d:%02d:%02d',
substr([Date], 7, 4), substr([Date], 4, 2), substr([Date], 1, 2),
substr([Date], 12, 2), substr([Date], 15, 2), substr([Date], 18, 2)))
BETWEEN '2017-07-15 20:00:05' AND '2017-07-17 21:00:09'
Please note you have to change also syntax of BETWEEN to match one of supported DATETIME formats.

I get Error messages when entering date values into column with Date data type

I used the following query to insert values. The DateOfLoan data type is date. There is an extra column in my table called DateDueBack and this column is a computed column which adds 14 days to the date in the DateOfLoan column.
Insert CurrentLoans(LoanID, BookISBN, MemberID, DateOfLoan)
Values (101, 'MB00001', 2, '20/10/2014'),
(102, 'AO00001', 5, '13/10/2014'),
(103, 'AH00002', 5, '13/10/2014'),
(104, 'DK10100', 2, '23/10/2014'),
(105, 'EP00666', 1, '12/10/2014'),
(106, 'HH10189', 4, '01/01/2014');
I get the following error message:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
When I remove the quotes around the date values I get an error message saying Int is incompatible with Date.
Can someone explain whats wrong with the code above and provide a solution.
Thanks in advance for any help offerd.
Date format Should be use in sql-server like below format.
YYYY-MM-dd or MM/dd/YYYY
So, you have to try like this.
Insert CurrentLoans(LoanID, BookISBN, MemberID, DateOfLoan)
Values (101, 'MB00001', 2, '10/20/2014')
or
Insert CurrentLoans(LoanID, BookISBN, MemberID, DateOfLoan)
Values (101, 'MB00001', 2, '2014-10-20')
Or use Sql Convert
Insert CurrentLoans(LoanID, BookISBN, MemberID, DateOfLoan)
Values (101, 'MB00001', 2,convert(date,'20/10/2014',103))
Try this:-
Insert into CurrentLoans(LoanID,BookISBN,MemberID,DateOfLoan)
Values (101, 'MB00001', 2, '2014-10-20'),
(102, 'AO00001', 5, '2014-10-13'),
(103, 'AH00002', 5, '2014-1013'),
(104, 'DK10100', 2, '2014-10-23'),
(105, 'EP00666', 1, '2014-10-12'),
(106, 'HH10189', 4, '2014-01-01');
Your default settings appear to be MONTH-DAY-YEAR, so SQL Server is trying to convert '20/10/2014' into the 10th day of the 20th month, and there are only 12 months so you are getting an error.
A simple fix would be to explictly set your DATEFORMAT, this works fine:
SET DATEFORMAT DMY;
SELECT CONVERT(DATE, '20/10/2014');
A better solution would be to use a culture invariant format, the other answers are suggesting the ISO format yyyy-MM-dd, but although this will work in your case I do not advocate it.
The reason is for whatever reason the ISO format is not culture invariant for the DATETIME and SMALLDATETIME datatypes. The following yeilds a conversion error:
SET DATEFORMAT DMY;
SELECT CONVERT(DATETIME, '2015-02-20');
It is only invariant for the new DATE and DATETIME2 datatypes. I frequently see this suggested because it is the ISO format, and it is still easily readible for the human eye, and if you happen to work on servers with default settings of MDY most of the time you will get away with it for the most part.
But if you want things to work all the time, not most of the time then the only invariant format for DATETIME and SMALLDATETIME is yyyyMMdd, as such I would advise using this, as it is invariant for all types.
For further reading Aaron Bertrand has written a great article on it: Bad habits to kick : mis-handling date / range queries
The default date format SQL Server can vary by location. Out of the box, it is mm/dd/yyyy, although other formats (such as yyyy-mm-dd can be processed, as suggested in the question comments).
KB article 173907 explains this.
With a date string of '20/10/2014', SQL thinks you are specifying a date of the 10th day of 20th month of 2014, which isn't valid, so it rejects your input.
However, you can use the CONVERT function to be more explicit in your code, which takes defaults and location out of the equation.
-- format 103 = dd/mm/yyyy
Insert CurrentLoans(LoanID, BookISBN, MemberID, DateOfLoan)
Values (101, 'MB00001', 2, convert(datetime, '20/10/2014', 103)),
(102, 'AO00001', 5, convert(datetime, '13/10/2014', 103)),
(103, 'AH00002', 5, convert(datetime, '13/10/2014', 103)),
(104, 'DK10100', 2, convert(datetime, '23/10/2014', 103)),
(105, 'EP00666', 1, convert(datetime, '12/10/2014', 103)),
(106, 'HH10189', 4, convert(datetime, '01/01/2014', 103));