nvarchar to time conversion - sql-server-2008-r2

I have to develop system which is accept data (in database) from another system. The problem I face is all DATE and TIME data is in NVARCHAR. I have to convert to DATE and TIME in order to calculate the duration. I am using SQL Server 2008 R2.
Data example:
STR_YMD STR_HMS
--------------------
20150101 151000
20090807 123009
20130113 145602
20090515 061700
How could I convert this data into following DATETIME data?
STR_DT
--------------------
2015-01-01 15:10:00
2009-08-07 12:30:09
2013-01-13 14:56:02
2009-05-15 06:17:00
Hopefully someone could help me. Thanks in advance
Bet

Something like this ?
SELECT CAST(SUBSTRING(STR_YMD, 1, 4) + '/' + SUBSTRING(STR_YMD, 5, 2) + '/' +
SUBSTRING(STR_YMD, 7, 2) + ' ' + SUBSTRING(STR_HMS, 1, 2) + ':' +
SUBSTRING(STR_HMS, 3, 2) + ':' + SUBSTRING(STR_HMS, 5, 2) AS DateTime)
EDIT : You can use LEFT function to be compatible with short time formats :
SELECT CAST(SUBSTRING(STR_YMD, 1, 4) + '/' + SUBSTRING(STR_YMD, 5, 2) + '/' +
SUBSTRING(STR_YMD, 7, 2) + ' ' + SUBSTRING(LEFT(STR_HMS+'000000', 6), 1, 2) + ':' +
SUBSTRING(LEFT(STR_HMS+'000000', 6), 3, 2) + ':' +
SUBSTRING(LEFT(STR_HMS+'000000', 6), 5, 2) AS DateTime)

Related

Add Symbol () in report ssrs

I have code :
CourseName + ' ' + isnull(GradeLevel,'')) as CourseName
The result :
Permit Working High 3
How to make result like this :
Permit Working High (3)
CourseName + ' ' +
CASE
WHEN GradeLevel IS NULL THEN ''
ELSE '(' + GradeLevel + ')'
END
AS CourseName

INET_ATON equivalent in AWS Redshift

I can't find any documentation on the web whether there is something like INET_ATON in AWS Redshift. So I guess it isn't there yet but I am wondering if there can be a workaround in some way. BTW I am using max_mind data.
For a workaround you can use:
SELECT ipAddr,
SPLIT_PART(ipAddr, '.', 1)* 16777216::bigint +
SPLIT_PART(ipAddr, '.', 2)* 65536::bigint +
SPLIT_PART(ipAddr, '.', 3)* 256::bigint +
SPLIT_PART(ipAddr, '.', 4)::bigint AS addressRange
FROM <your_table) LIMIT 5
addressRange should match to maxmind startIpNum < addressRange < endIpNum.
I ended up creating a wrapper in Python (which is the main language I am using) which translates IP string into a number and then use it.
I loaded the country blocks and locations CSVs in homonymous tables and joined with the following query
INSERT INTO dim.geoip_country
SELECT
SPLIT_PART(first_ip, '.', 1) * 16777216::BIGINT
+ SPLIT_PART(first_ip, '.', 2) * 65536::BIGINT
+ SPLIT_PART(first_ip, '.', 3) * 256::BIGINT
+ SPLIT_PART(first_ip, '.', 4)::BIGINT AS ip_inf,
SPLIT_PART(first_ip, '.', 1) * 16777216::BIGINT
+ SPLIT_PART(first_ip, '.', 2) * 65536::BIGINT
+ SPLIT_PART(first_ip, '.', 3) * 256::BIGINT
+ SPLIT_PART(first_ip, '.', 4)::BIGINT
+ POW(2, 32 - mask_bits)::BIGINT AS ip_sup,
network,
isocode2,
name,
continent_code,
continent_name,
is_anonymous_proxy,
is_satellite_provider
FROM (
SELECT
b.network,
SPLIT_PART(b.network, '/', 1) AS first_ip,
SPLIT_PART(b.network, '/', 2)::INTEGER AS mask_bits,
l.country_iso_code AS isocode2,
l.country_name AS name,
l.continent_code AS continent_code,
l.continent_name AS continent_name,
b.is_anonymous_proxy::BOOLEAN AS is_anonymous_proxy,
b.is_satellite_provider::BOOLEAN AS is_satellite_provider
FROM ext.geoip2_country_blocks_ipv4 b
JOIN ext.geoip2_country_locations_en l
ON b.geoname_id = l.geoname_id
)

query throwing error in web report writer

I have a query/view that is created to be used in a report. When I run in the SMS and in SSRS it run fine. But when I connect the view to the tool that generates our reports it throws the following error. Incorrect syntax near '.8'. When I contact the support for this product they say it has to do with how we calculate the 8thgradyear . I have placed the code below. Any suggestions.
SELECT
dbo.studemo.suniq,
dbo.studemo.ident,
dbo.studemo.lastname,
dbo.studemo.firstname,
dbo.studemo.emailaddr AS stuemail,
dbo.studemo.birthdate,
dbo.track.schoolc,
dbo.school.schname,
dbo.stustat.graden,
dbo.stustat.edate,
dbo.zstustat.descript AS status,
RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname AS advisor,
dbo.track.schyear,
SUM(8) - dbo.stustat.graden + dbo.track.schyear AS [8thgradyear],
sf.Email, LOWER(sf.Username) AS [user],
LOWER(RIGHT(SUM(8) - dbo.stustat.graden + dbo.track.schyear, 2) + LEFT(dbo.studemo.firstname, 1) + REPLACE(REPLACE(REPLACE(dbo.studemo.lastname, '-', ''), ' ', ''), '''', '') + RIGHT(dbo.studemo.ident, 3)) AS newuser,
CONVERT(varchar(8), dbo.studemo.birthdate,1) AS password,
'STUDENTS' + '/' + (CASE WHEN track.schoolc IN ('19', '43', '17', '23') THEN 'Middle' ELSE 'Elementary' END) + '/' + dbo.school.schname AS neworg,
sf.OU, sf.LastLoginTime
FROM dbo.studemo INNER JOIN
dbo.stustat ON dbo.studemo.suniq = dbo.stustat.suniq INNER JOIN
dbo.track ON dbo.stustat.trkuniq = dbo.track.trkuniq INNER JOIN
dbo.zstustat ON dbo.stustat.stustatc = dbo.zstustat.stustatc INNER JOIN
dbo.facdemo ON dbo.stustat.funiq = dbo.facdemo.funiq LEFT OUTER JOIN
dbo.vw_google_OU AS sf ON sf.Firstname = dbo.studemo.firstname AND sf.Lastname = dbo.studemo.lastname INNER JOIN
dbo.school ON dbo.school.schoolc = dbo.track.schoolc
WHERE (dbo.stustat.stustatc IN
(SELECT stustatc
FROM dbo.zstustat AS zstustat_1
WHERE (snstatus IN ('A', 'M', 'P')))) AND (dbo.stustat.xdate IS NULL OR
dbo.stustat.xdate < dbo.stustat.edate) AND (dbo.track.schoolc NOT IN ('P34', 'P24', '802', '801'))
GROUP BY dbo.studemo.suniq, dbo.studemo.ident, dbo.studemo.lastname, dbo.studemo.firstname, dbo.studemo.birthdate, RIGHT(dbo.studemo.ident, 3), dbo.track.schoolc,
dbo.stustat.graden, dbo.zstustat.descript, RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname, dbo.stustat.edate, dbo.studemo.gradyear, dbo.track.schyear,
sf.Email, CONVERT(varchar(8), dbo.studemo.birthdate, 1), sf.Username, dbo.school.schname, sf.OU, dbo.studemo.emailaddr, sf.LastLoginTime
SUM function was introduced in SQL Server 2008 and your tool must be using and older version of SQL Server.

How to format manipulated DateTime fields in dynamic TSQL

I have a query that looks basically like this, wrapped in a dynamic query to accommodate table names that can change. I got the date functions in the middle converted but it doesn't like the LoadedDateTime and CallPlacedTime at the end. I've tried every conversion and combination of quoting those lines that I can think of. How can I accomplish this?
DECLARE #sql_TotalDialsNewLeads nvarchar(1000) = N'
SELECT COUNT(*)
FROM ' + #tbl_CH + ' ch, ' + #tbl_CL + ' cl, ' + #tbl_DA + ' da
WHERE ch.IDENTITY = cl.IDENTITY
AND cl.CRMID = da.CRMID
AND CallPlacedTime BETWEEN ''' + CONVERT(varchar(30),DATEADD(HOUR,-#TimezoneOffset,#StartDate),126) + '''
AND ''' + CONVERT(varchar(30),DATEADD(HOUR,-#TimezoneOffset,#EndDate),126) + '''
AND Product = ''' + #Product + '''
AND Country = ''' + #Country + '''
AND DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,LoadedDateTime))
= DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,CallPlacedTime))'
EXECUTE(#sql_TotalDialsNewLeads);
Thanks,
Sean
Try to use sp_executesql with parameters instead of making string to execute manually:
...
AND CallPlacedTime BETWEEN DATEADD(HOUR,-#TimezoneOffset,#StartDate)
AND DATEADD(HOUR,-#TimezoneOffset,#EndDate)
...
and execute it with the following statement:
exec sp_executesql #sql_TotalDialsNewLeads,
N'#StartDate datetime, #EndDate datetime',
#StartDate,#EndDate;
Here is a MSDN guide. Also you can do it for other parameters in your SQL statement.
Instead of using the conversion outside use it inside the dynamic query.
something like this. (Not tested)
DECLARE #sql_TotalDialsNewLeads nvarchar(1000) = N'
SELECT COUNT(*)
FROM ' + #tbl_CH + ' ch, ' + #tbl_CL + ' cl, ' + #tbl_DA + ' da
WHERE ch.IDENTITY = cl.IDENTITY
AND cl.CRMID = da.CRMID
AND CallPlacedTime BETWEEN CONVERT(varchar(30),DATEADD(HOUR,'''+ -#TimezoneOffset +''','''+ #StartDate +'''),126)
AND CONVERT(varchar(30),DATEADD(HOUR,'''+ -#TimezoneOffset +''','''+ #EndDate +'''),126)
AND Product = ''' + #Product + '''
AND Country = ''' + #Country + '''
AND DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,LoadedDateTime))
= DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,CallPlacedTime))'
EXECUTE(#sql_TotalDialsNewLeads);

How to add dynamic SQL delimiter of single quote?

I am developing a dynamic SQL using SQL Server 2008 T-sql code. So I want to return values that look like "Jan'11".
My code looks something like:
left(datename(month, SGD_SIGNOFF_DATE), 3) + ' + '''' +
' RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear
But this is not working. Either I get output to look like "Jan11" or I get error messages. What is proper syntax for my solution?
SELECT LEFT(datename(month, SGD_SIGNOFF_DATE), 3) + '''' + RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear
This should work:
left(datename(month, SGD_SIGNOFF_DATE), 3) + '''' + RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear
I got it! Here is the code now that works!
left(datename(month, SGD_SIGNOFF_DATE), 3) + '''''''' +
RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear